I have a table in a SQL 2005 database which is brand new. As part of our application deployment we load the table with about 2.6M rows. Once that is done, the indexes on the table are all rebuilt. Then the users are let into the system and queries against that table time out. I can then rebuild the indexes (using the same exact script that was used after the import) and the queries are lightning fast.
I’ve checked that there are no other major data changes to the table after the index rebuilds. Any ideas on what else might cause this behavior?
Here’s a sample of what the index rebuild script looks like:
DROP INDEX dbo.My_Table.Index1
DROP INDEX dbo.My_Table.Index2
ALTER INDEX PK_My_Table ON dbo.My_Table REBUILD
CREATE NONCLUSTERED INDEX Index1 ON dbo.My_Table (column_1 ASC)
CREATE NONCLUSTERED INDEX Index2 ON dbo.My_Table (column_2 ASC)
Statistics, probably, but not on the indexes
The optimiser will pick up the number of changed rows/no stats for the first query. It decides to rebuild/create stats.
However: there may be column level statistics that are not associated with an index.
The 2nd rebuild is irrelevant for stat purposes, because the column stats already exists, but it force the execution plans to be discarded and reevaluated
Edit:
SQLServerPedia: