![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a table with a single index. (Unique on primary key, clustered) Each morning the index needs to be rebuilt for some reason. In my program which accesses the table, response time is about 15 seconds. After I rebuild the index, it becomes < 2 seconds. I created a maintenance plan to reindex. It is: USE [MYDB] GO ALTER INDEX [PK_MyField] ON [dbo].[MyTable] REBUILD WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, SORT_IN_TEMPDB = OFF, ONLINE = OFF ). The maint plan runs, but doesn't seem to work. If I rebuild by right-clciking the tables indices...rebuild, it works fine. Any help would be appreciated. -- stullhe104 |
#3
| |||
| |||
|
|
When you say the MP doesn't work, do you really mean to say that it doesn't improve your application response time? Check the stats date of the primary key index to see if it corresponds to the expected rebuid time: SELECT STATS_DATE(object_id, index_id) FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo.MyTable') AND name = N'PK_MyField' Back to your performance issue, I am surprised that a rebuild is needed so often and that the difference in query duration is so much. Perhaps the real issue is an inappropriate execution plan that gets removed from cache after the rebuild. Check the execution plan before/after the rebuild to see if that is the case. -- Hope this helps. Dan Guzman SQL Server MVP http://weblogs.sqlteam.com/dang/ "Herb" <stullhe104 (AT) newsgroup (DOT) nospam> wrote in message news:4BDE37D4-0E39-4506-8CF5-628A15EC599D (AT) microsoft (DOT) com... I have a table with a single index. (Unique on primary key, clustered) Each morning the index needs to be rebuilt for some reason. In my program which accesses the table, response time is about 15 seconds. After I rebuild the index, it becomes < 2 seconds. I created a maintenance plan to reindex. It is: USE [MYDB] GO ALTER INDEX [PK_MyField] ON [dbo].[MyTable] REBUILD WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, SORT_IN_TEMPDB = OFF, ONLINE = OFF ). The maint plan runs, but doesn't seem to work. If I rebuild by right-clciking the tables indices...rebuild, it works fine. Any help would be appreciated. -- stullhe104 |
#4
| |||
| |||
|
|
I have manually rebuilt the index since the MP ran, so your query shows the time of when I rebuilt it. If I run the MP manually (right-click Execute) it runs successfully, and the index is proper. Also the date/time is correct for when I ran it manually. So in answer to your question, the MP works but doesn't improve the response time. Is it possible that my MP needs to run as a different user? If so, how do I change that? |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Herb (stullhe104 (AT) newsgroup (DOT) nospam) writes: I have manually rebuilt the index since the MP ran, so your query shows the time of when I rebuilt it. If I run the MP manually (right-click Execute) it runs successfully, and the index is proper. Also the date/time is correct for when I ran it manually. So in answer to your question, the MP works but doesn't improve the response time. Is it possible that my MP needs to run as a different user? If so, how do I change that? The answer is that the whole affair with maintenance plan and reindexing is an overkill to the whole problem. Next time, you have this performance issue, try this: sp_recompile 'yourtable' This flushes all plans related to the table from the cache. What happens is related to something known as parameter sniffing. When SQL Server builds a query plan for the first time, it sniffs the input parameters, be that a stored procedure or parameterised ad hoc-statement. If the parameters for the first call are atypical, you will get a plan which is bad for the rest of your queries. Say that your application runs a query to load the most recent changes from the database, passing a high-water mark as parameter. But once a day, in the morning, your application performs a reload using the same query, now passing a 0 for the high-water mark. Assume then that for some reason there is no plan for the query in cache. The plan you will get will be a scan of the entire table, which is good for that first query, but really bad for reading the delta. This example may not at all be applicable to your application, but I wanted to give an example how this scenario could occur. -- Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx |
#7
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |