Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


rebuild index worsens query performance


rebuild index worsens query performance

Author
Message
jgenovese
jgenovese
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 277
we have a query which runs several times a day

reads from several tables in one database ("W"), and two tables in another database ("P") (both db's reside on the same server)

the two tables in database "P" never had their indexes rebuilt or statistics updated, and were significantly fragmented

I purged some old information from another table in database "P" (about 50% worth), and shrank database "P" (to reclaim disk space which we were running out of)

I rebuilt the indexes/updated statistics in "P" immediately afterward, and now weekly, via a maintenance plan (rebuild index task [original amount of free space], update statistics task [column stats only, initially at 10%, then 50% weekly])

From that point forward the aforementioned query takes twice as long to run

Thoughts?

FYI:

the two tables in the query, and the table I purged, are subscribed tables in replication, the publisher being on another server in the same domain

I dropped replication on the purged table, purged it on both the publisher and subscriber, and re-created replicarecreatedthe "replication support only" option

the two tables in the query had replication active the whole time (purge of the third table, shrink, index rebuild, etc.) -- no information was flowing at that time

database "P" has not expanded since the shrink

a comparison (via RedGate Compare) between the database pre-purge/shrink/index rebuild and present show no structural differences between the two tables used by the query

Is it possible that the plan the query was using prior to the index rebuild was running faster despite the fragmentation? I would think that any reduction in index fragmentation would be cause for improvement in query performance

*** UPDATE (11-20-2012) -- it was determined that, on that day, I rebuilt indexes but NOT stats -- update stats came 2 weeks later and made no difference either way
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14375 Visits: 9729
Index defragmentation shouldn't cause a performance degradation, but it won't necessarily speed things up either. It depends on how the index is being used.

Single-row seeks, for example, aren't significantly impacted by fragmentation.

Before I can suggest anything about the issue, I'd need to see the execution plan at the very least. Table definitions and the query are usually also needed, but the execution plan is the bare minimum. Can you attach that as an sqlplan file? (Don't copy-and-paste the XML into the forum, in other words. Save the plan as a file and attach that to a post in the forum.)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
jgenovese
jgenovese
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 277
note: the emphasis is on what happened when the aforementioned operations were performed on database "P" such that the query doubled in run time, not so much on what code to change in order to improve performance

the 1st letter of the two databases in the plan will be either "W" or "P" (per opening post)
Attachments
query_plan1c - Copy.sqlplan (30 views, 241.00 KB)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24180 Visits: 37951
After completing the index rebuild, did you then rebuild all the statistics? If so did you use a sample size or full scan?

If you used a sample size on the statistics for the indexes you rebuilt, you made them worse. The statistics for the indexes are rebuilt when you rebuild the indexes and is done with a full scan, meaning the statistics were better before you rebuilt the statistics separately.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
jgenovese
jgenovese
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 277
I dont think I rebuilt stats that day,just the indexes.

I added a update stats task to the maint plan about a week later (after the index rebuild), however it updates COLUMN stats only -- my understanding is that index rebuild updates TABLE stats but not COLUMN stats, correct me if I'm wrong
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45058 Visits: 39903
I suppose that it's possible that an index rebuild could cause a slow down of this nature because of the stats rebuild that also occurs. That may have caused the execution plan to change. I've also see that adding an index (found this out in the code for one of my articles) can cause code to run much slower. The optimizer isn't magic... it was written by humans and it sometimes makes bad choices.

Whatever was the cause, I think you're now to the point where you have to treat it like a new query and tune it. If you want, check the article at the second link in my signature below for how to post what folks need to help you for such problems.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24180 Visits: 37951
I have to agree with Jeff at this point. I have looked at the estimated execution plan, but one of the things we really need is the actual execution plan from the slow running process. That is in addition to the information also mentioned in the article Jeff recommended you read.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24180 Visits: 37951
Taking a guess here, but is the table valued function you use in this process a multi-statement table valued function?


Also, looks like you may have several scalar functions being used, are this in the select list of any tables queried or in the where or join clauses between tables?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
jgenovese
jgenovese
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 277
that IS the actual plan:

select

(SELECT SUBSTRING(text, r.statement_start_offset/2,(CASE WHEN r.statement_end_offset = -1 THEN LEN(CONVERT(nvarchar(max), text)) * 2 ELSE r.statement_end_offset END - r.statement_start_offset)/2 ) FROM sys.dm_exec_sql_text(r.sql_handle) ) AS query_text
,
(SELECT convert(xml, query_plan) FROM sys.dm_exec_text_query_plan (r.plan_handle , r.statement_start_offset, r.statement_end_offset )) AS query_plan
--,
--(SELECT query_plan FROM sys.dm_exec_query_plan (r.plan_handle )) AS query_plan_batch
-- , qs.*

from sys.dm_exec_requests r
left join sys.dm_exec_query_stats qs
on r.statement_start_offset = qs.statement_start_offset
and r.sql_handle = qs.sql_handle

where 1=1
and session_id = ...
jgenovese
jgenovese
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 277
I believe it is multi-line

Note that the query did not change -- the emphasis is on what could have happened that weekend that caused the query to double in time
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search