JuhaKangas
JuhaKangas

Reputation: 883

Keep having to reindex sql tables

I have a database on SQL Server 2012 and am having some problems with a few tables that become slow after a while and the thing that helps is rebuilding the indexes. I was wondering if anyone has advice on what could be wrong in any of them, I will post their structure and indexes below. I have not built this structure myself but have full access to modify.

Table1

Indexes:

Table2

Indexes:

Table3

Indexes:

Upvotes: 0

Views: 2209

Answers (3)

cgsmks
cgsmks

Reputation: 121

A great tool that will analyze and, if necessary, rebuild indexes and update statistics is Ola Hallengren's Index and Statistics Maintenance tool/script. We run this thing nightly, along with the Integrity Check, to keep our databases healthy.

http://ola.hallengren.com/

Upvotes: 2

D Stanley
D Stanley

Reputation: 152654

One column that sticks out to me is this one:

pk_Name (Clustered) [Name, fkID]

Clustered keys determine the physical order of records in the database table. If Name is a string and values are inserted in "random" order (i.e. not always at the end of the table alphabetically) there could be performance problems as the database is always having to "insert" rows into the physical tables. This could cause the table data to become fragmented, which could reduce performance as well.

Re-building a clustered index also re-organizes the physical data which is likely why you're seeing improved performance afterwards. Recomputing statistics could also be a factor, but a primary key that results in non-consecutive inserts is usually a red flag.

Also your definition doesn't specify the columns that make up the clustered indices on tables 2 and 3, but based on the name I'm assuming they're indexed by ID.

Upvotes: 5

asafrob
asafrob

Reputation: 1858

I came across a similar problem. When you add data to your table the statistics gets updated only if u pass a specific change threshold as described here:

http://blog.sqlauthority.com/2010/04/21/sql-server-when-are-statistics-updated-what-triggers-statistics-to-update/

I have not found any good way to overcome this problem except for major changes to the way we work with our DB. In the meanwhile you can run

UPDATE STATISTICS <Table Name>

or

alter INDEX <Index Name> ON <Table Name> rebuild

Every night

Upvotes: 1

Related Questions