Domanda

I have a database table that has more than 50 Million record and to improve searching i had to create a non clustered indexes, and once i create one it takes 5 ~ 10 minutes to be created so i guess in the background it sorts the data according to the index.

So for example before adding index to my table searching was awful and takes long time and when i added the non clustered index, searching was fast.

But that was only when i had 50 million records.

The question is, what if i defined the index at the very beginning when creating the table before adding any data to the table? Would it give the same search performance i am getting right now? or do i have to delete and recreate the index every now and then to sort the data regularly?

I am sorry if my question seemed stupid, i just started learning about indexes and it is a confusing topic for me.

È stato utile?

Soluzione

A non-clustered index keeps a copy of the indexed fields in a special structure optimised for searching. Creating an index on 50 million records obviously takes some time.

Once the index is created, it"s maintained automatically as records are added, deleted or updated, so you should only need to reindex if you've had a serious crash of the system or the disk.

So generally, it's best to create the index at the time you create the table.

There is an operation called 'updating statistics' which helps the query optimiser to improve its search performance. The details vary between database engines.

Altri suggerimenti

Databases indexes work like those in books.

It's actually a pointer to the right rows in your table, based and ordered on a specific key (the column for which you define the index).

So, basically, yes, if you create the index before inserting data, you should get the same search speed when you use it later on when the table is loaded with lots of records.

Although, since each time you insert (or delete, or update the specific key) a record the index needs to be updated, inserting (or deleting or updating) large amount of data will be a bit slower.

Indexes can get fragmented if you do a lot of insert and delete on the table. Thus, deleting and recreating them is usually part of a good maintenance plan.

Check out the free scripts from ola hallengren. One is on index maintenance and statisics.

General rule of thumb,

Index fragmentation between 10 and 30 pct, re-organize.

Fragmentation > = 30 pct, rebuild.

With a re-organize., you need to update your statistics.

The rebuild automatically does it.

Indexing is a huge part of optimizing query performance.

  • John

http://ola.hallengren.com/

Indexes can be created prior to data be inserted into the table in question. The index is simply updated every time rows are inserted or updated, assuming the update touches fields involved in the index in question.

when rows are inserted the index may become fragmented to allow the index to maintain the desired logical order or rows in the index. For instance, if the index has rows like A, B, and E and you added a row containing C or D the index would be split so the new row fits between B and E. This fragmentation can be repaired with Olla Hallengren's scripts as Crafty DBA mentioned in his answer, however depending on how your system storage is configured this may be doing work for nothing.

Do yourself a favor and look at http://www.brentozar.com/sql/index-all-about-sql-server-indexes/ for some excellent info on SQL Server indexing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top