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


Guidance on table valued types.


Guidance on table valued types.

Author
Message
mayurkb
mayurkb
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 238
We have 1 table valued parameter (tvp) coming to GetXXX stored procedures. It contains only 1 column of type uniqueidentifier. Currently, it does not have any index on it. For performance improvement, I was thinking of adding a primary key as clustered index on it. My initial testing showed if there less than 100 records in that tvp then it actually performs worse than the older one which doesn't have any index on it. I was hoping that it would perform same if not better for smaller datasets. Does anybody know if I'm missing anything here.

Thanks in advance.

Regards,
Mayur
SQLServerForum.nl
SQLServerForum.nl
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 129
Hi,

Did you check the query plan? I'm quessing the new index is not being used. Can you give an example on the query you are using on that table?

Regards!
mayurkb
mayurkb
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 238
From the plan, it can be seen that the new index is used. It also saves an extra Sort operation on the tvp.
Arjun Sivadasan
Arjun Sivadasan
SSC-Addicted
SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)

Group: General Forum Members
Points: 448 Visits: 976
Hmm, may not be worth it, you know. There is only one column in the table. If you had more columns and if filtering was performed on the key column - yes. As you said, the data will be sorted because of the clustered index, but the sorting happens probably when the SP call is made. You may not benefit from this.
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8239 Visits: 14368
I recommend having a clustered PK on all TVPs. I enforce this in code reviews too. 1) it keeps data transfer to a minimum by forcing sloppy development from passing more data than necessary and 2) having a clustered index on the TVP will help the engine (although not guarantee it can) maintain a consistent data access pattern which helps reduce deadlocks.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
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: 45069 Visits: 39905
mayur birari (2/28/2013)
From the plan, it can be seen that the new index is used. It also saves an extra Sort operation on the tvp.


HOW is the index ACTUALLY being used? Just because it may be an INDEX SEEK doesn't necessarily mean that it will be more effective. You have to look at the properties of the seek. How many times is the seek actually occuring? For example, 40,000 INDEX SEEKs behind the scenes can be quite a bit slower than a single scan at the leaf level.

--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
Arjun Sivadasan
Arjun Sivadasan
SSC-Addicted
SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)

Group: General Forum Members
Points: 448 Visits: 976
Jeff Moden (3/3/2013)<snip/> For example, 40,000 INDEX SEEKs behind the scenes can be quite a bit slower than a single scan at the leaf level.


Jeff, in this case, won't optimizer choose to scan rather than to seek? Will optimizer seek blindly because the index is covering? Or does it depend?
Orlando Colamatteo
Orlando Colamatteo
SSCrazy Eights
SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)SSCrazy Eights (8.2K reputation)

Group: General Forum Members
Points: 8239 Visits: 14368
One other question came to mind: In what order are the uniqueidentifiers being added to the TVP as they are sent to the SQL Server? If you're using .NET then is it safe to assume the rows are in "any old order" in your DataTable or can you confirm they are sorted?

And a bit more on this:

My initial testing showed if there less than 100 records in that tvp then it actually performs worse than the older one which doesn't have any index on it. I was hoping that it would perform same if not better for smaller datasets.

The point where the performance gain the proc has with the index in place outweighs the overhead the engine imposes having to sort the rows as they are added to the Table-type sounds like it is at ~100 rows. Like I said though, the hint you give the engine by having the clustered index in place in terms of which order to process the data when it comes to joins will help you avoid deadlocks in concurrency-scenarios.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5697 Visits: 7660
Arjun Sivadasan (3/3/2013)
Jeff Moden (3/3/2013)<snip/> For example, 40,000 INDEX SEEKs behind the scenes can be quite a bit slower than a single scan at the leaf level.


Jeff, in this case, won't optimizer choose to scan rather than to seek? Will optimizer seek blindly because the index is covering? Or does it depend?


It'll depend. The execution plan will compile according to assumptions based on statistics. If they're wrong... well, bad things happen.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Arjun Sivadasan
Arjun Sivadasan
SSC-Addicted
SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)SSC-Addicted (448 reputation)

Group: General Forum Members
Points: 448 Visits: 976
Thanks Craig, such has been my experience as well.
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