Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

rebuild index worsens query performance Expand / Collapse
Author
Message
Posted Friday, November 16, 2012 10:34 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 10:23 AM
Points: 42, Visits: 240
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
Post #1385783
Posted Friday, November 16, 2012 10:50 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
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
Post #1385787
Posted Friday, November 16, 2012 12:08 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 10:23 AM
Points: 42, Visits: 240
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)


  Post Attachments 
query_plan1c - Copy.sqlplan (24 views, 241.53 KB)
Post #1385819
Posted Friday, November 16, 2012 9:48 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:26 AM
Points: 22,479, Visits: 30,162
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.



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)
Post #1385901
Posted Saturday, November 17, 2012 6:29 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 10:23 AM
Points: 42, Visits: 240
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
Post #1385935
Posted Saturday, November 17, 2012 6:39 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 6:46 AM
Points: 35,951, Visits: 30,236
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."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1385938
Posted Saturday, November 17, 2012 10:52 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:26 AM
Points: 22,479, Visits: 30,162
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.



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)
Post #1385956
Posted Saturday, November 17, 2012 10:56 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:26 AM
Points: 22,479, Visits: 30,162
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?



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)
Post #1385958
Posted Saturday, November 17, 2012 11:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 10:23 AM
Points: 42, Visits: 240
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 = ...
Post #1385959
Posted Saturday, November 17, 2012 11:02 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 28, 2013 10:23 AM
Points: 42, Visits: 240
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
Post #1385960
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse