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 12»»

Covering index is too long, but needed ... what to do? Expand / Collapse
Author
Message
Posted Thursday, August 14, 2008 10:53 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 1:17 PM
Points: 236, Visits: 474
Hello,

I have a stored procedure which is running slowly. Looking at the execution plan in the query analyzer I saw that the subtree cost is 3.5 and there is a bookmark lookup that takes up 65% of the cost.

I added a covering index, and surely the subtree cost dropped to 1.75 (although logical reads increased, which is another puzzle).

The problem is that the covering index I added includes many columns [4 varchar and a few ints] (had no choice, since the query is using all of them, and I need to cover them all with an index), and they add up to ~ 1200 bytes(?). Although the index was allowed to be added, the warning message came us saying that if the length of the index exceeds 900, inserts might fail. I believe they will fail if the data inserted in a row will exceed 900 bytes (as row cannot be split between 2 pages I believe).

So, although I don't expect any data entered to exceed 900 ... but who knows - it seems unsafe to have this index.

What do people do in this case? Just don't add an index and live with higher query costs?

Please advise
Thanks in advance!
Post #552923
Posted Saturday, August 23, 2008 10:33 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 11:24 AM
Points: 1,386, Visits: 6,194
I personally wouldn't risk adding the index. Unless the documentation tells it never can exceed 900 bytes ( odd, when the fields are larger).
Post #557777
Posted Saturday, August 23, 2008 1:51 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 36,612, Visits: 31,056
So... after you added the index, did you get index seeks or just scans? Also, never trust the execution plan by itself... did you turn statistics on or run profiler against the query before and after? If not, you have no concrete proof that adding the index actually helped overall performance or not.


--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."

(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 #557799
Posted Monday, August 25, 2008 9:29 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 1:17 PM
Points: 236, Visits: 474
Thanks for all the suggestions.

I was able to fix the problem without adding an index, but just re-writing the query.

It was strange how a very simple change in the query made it much faster.

Thanks again!
Post #558210
Posted Monday, August 25, 2008 9:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 9:56 PM
Points: 2,107, Visits: 3,579
Glad to hear that the rewrite helped.

Very much agree with Jeff in that looking at Execution plans and query costs alone can be dangerous especially when changing things bring the cost down and IO up. It's one thing when those data pages are in memory but if you are having to go to disk to get data, that is always going to drastically impact performance. So, less reads with less cost is always nicer. :)


David

@SQLTentmaker
SQL Tentmaker
“He is no fool who gives what he cannot keep to gain that which he cannot lose” - Jim Elliot
Post #558220
Posted Monday, August 25, 2008 10:20 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 36,612, Visits: 31,056
sql_er (8/25/2008)
Thanks for all the suggestions.

I was able to fix the problem without adding an index, but just re-writing the query.

It was strange how a very simple change in the query made it much faster.

Thanks again!


Heh... two way steet here. :D What was the "very simple change in the query" you made?


--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."

(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 #558583
Posted Wednesday, August 27, 2008 11:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 1:17 PM
Points: 236, Visits: 474
Thanks for all the comments and suggestions.

My approach was as such:

1. Run Server Side Tracing for 15 mins on 5 almost identical dedicated sql servers (Subscribers in Transactional replication). This resulted in ~35 executions of the stored procedure in question
2. Change sp to its new (optimized) version
3. Run Server Side Tracing for 15 mins again, similarly

On 4 out of 5, there was a drastic improvement in both - average duration and average reads. I think 5th one had some IO issues at the time, so we ignored it.

The change was basically an INNER JOIN order. I had 3 tables in a query, and there were 2 ways to JOIN between them. When the query was originally written , I did not pay attention to it. I just chose one way randomly. However, as I found out now, joining another would would allow to use an INDEX on one of the biggest tables of the 3, thereby totally changing the execution plan, bringing down the sub-tree cost from 3.5 to 0.1 and the logical reads from 110,000 to less than 10,000.

Thank you!
Post #559850
Posted Thursday, August 28, 2008 6:37 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:39 AM
Points: 4,311, Visits: 6,100

It was strange how a very simple change in the query made it much faster.


YOU may think so, but the regulars here won't. :)


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #560341
Posted Thursday, August 28, 2008 7:14 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 36,612, Visits: 31,056
sql_er (8/25/2008)
I was able to fix the problem without adding an index, but just re-writing the query.

It was strange how a very simple change in the query made it much faster.


I agree with what Kevin said... the "regulars" won't think it strange at all,. Heh... it's what they usually recommend!


--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."

(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 #560935
Posted Thursday, August 28, 2008 7:15 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 36,612, Visits: 31,056
sql_er (8/27/2008)
Thanks for all the comments and suggestions.

My approach was as such:

1. Run Server Side Tracing for 15 mins on 5 almost identical dedicated sql servers (Subscribers in Transactional replication). This resulted in ~35 executions of the stored procedure in question
2. Change sp to its new (optimized) version
3. Run Server Side Tracing for 15 mins again, similarly

On 4 out of 5, there was a drastic improvement in both - average duration and average reads. I think 5th one had some IO issues at the time, so we ignored it.

The change was basically an INNER JOIN order. I had 3 tables in a query, and there were 2 ways to JOIN between them. When the query was originally written , I did not pay attention to it. I just chose one way randomly. However, as I found out now, joining another would would allow to use an INDEX on one of the biggest tables of the 3, thereby totally changing the execution plan, bringing down the sub-tree cost from 3.5 to 0.1 and the logical reads from 110,000 to less than 10,000.

Thank you!


Very cool feed back... thanks a ton! :)


--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."

(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 #560936
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse