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

Guidance on table valued types. Expand / Collapse
Author
Message
Posted Monday, March 4, 2013 1:57 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, June 23, 2014 8:04 AM
Points: 83, Visits: 174
Thanks everybody for your responses. I could not post the table structure etc because of policies. Here is what I found out after more testing.
From the execution plans, it looks like the SQL server has to use all the records from the tvp anyways as it does index seeks against the main physical table. That is why having an index on tvp does not matter.
There is one thing to notice here and that is how do you join the tvp against the physical table:
1. With the non-indexed tvp, when I use where exists() to filter records from the physical table, then the SQL Server does add Sort() in the execution plan after the Table scan of the tvp . If I convert it inner join then it removes that Sort().
2. With the indexed tvp, it does need that Sort() even if where exists() is used.
So when inner join is used, both non-indexed and indexed tvps perform same.

So I think the only advantage that an index gives is the uniqueness of the records coming in. The "Ordered" part of the index does not come in handy. I tried this out with the inserts as well and found out the same thing.
Let me know what do you guys thing.

Thanks,
Mayur
Post #1426457
Posted Monday, March 4, 2013 2:09 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:57 PM
Points: 7,094, Visits: 12,583
mayur birari (3/4/2013)
Thanks everybody for your responses. I could not post the table structure etc because of policies. Here is what I found out after more testing.
From the execution plans, it looks like the SQL server has to use all the records from the tvp anyways as it does index seeks against the main physical table. That is why having an index on tvp does not matter.
There is one thing to notice here and that is how do you join the tvp against the physical table:
1. With the non-indexed tvp, when I use where exists() to filter records from the physical table, then the SQL Server does add Sort() in the execution plan after the Table scan of the tvp . If I convert it inner join then it removes that Sort().
2. With the indexed tvp, it does need that Sort() even if where exists() is used.
So when inner join is used, both non-indexed and indexed tvps perform same.

So I think the only advantage that an index gives is the uniqueness of the records coming in. The "Ordered" part of the index does not come in handy. I tried this out with the inserts as well and found out the same thing.
Let me know what do you guys thing.

Thanks,
Mayur

We said the unique constraint helps defend against duplicates from entering the mix due to sloppy development and gives the optimizer more information with which to base its decisions. In your specific case maybe uniqueness is all you will benefit from initially however an index comes as a package deal with a unique constraint like a primary key. In your specific case you may not see a benefit with or without the index in your join however if the plan ever changes due to changes in the data distribution, or possibly the introduction of a new index definition, then sorting requirements may also change on either side of the join and at that point the index on the TVP may come in handy in guiding the engine into a consistent data access pattern across concurrent requests. This is where you will help the engine avoid deadlocks.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1426461
Posted Monday, March 4, 2013 2:23 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, June 23, 2014 8:04 AM
Points: 83, Visits: 174
Thanks opc.
I missed the point with the deadlock in-case of inserts. This is an important factor in case of high transaction environment.
Thanks again.
Post #1426464
Posted Monday, March 4, 2013 7:40 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 12:17 AM
Points: 36,800, Visits: 31,261
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?


Like Craig said, it depends but you do have to look.


--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 #1426564
Posted Wednesday, March 6, 2013 7:36 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, June 23, 2014 8:04 AM
Points: 83, Visits: 174
Hi @opc,

I did some testing with the indexed tvps interms of deadlocks. I did not find any difference between non-indexed and indexed versions. I created a simple application which spaws n number of threads and then each thread would insert m number of items into the tables. The surrogate PK in our database is Guid and we generate it in .Net with Guid.NewGuid(). The deadlocks that occur are index locks mainly because of the foreign checks.

Regards,
Mayur
Post #1427400
Posted Wednesday, March 6, 2013 8:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:57 PM
Points: 7,094, Visits: 12,583
mayur birari (3/6/2013)
Hi @opc,

I did some testing with the indexed tvps interms of deadlocks. I did not find any difference between non-indexed and indexed versions. I created a simple application which spaws n number of threads and then each thread would insert m number of items into the tables. The surrogate PK in our database is Guid and we generate it in .Net with Guid.NewGuid(). The deadlocks that occur are index locks mainly because of the foreign checks.

Regards,
Mayur

OK, so you could not create a case where a deadlock occurred, but they can happen more often when TVPs are used if the underlying table type does not have a clustered index. You'll find this case documented (I am on my phone so don't have easy way to look it up) mostly when the TVP participates in a MERGE statement but I could also see it being common when it participates in any JOIN within a transaction too.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1427431
Posted Wednesday, March 6, 2013 8:04 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, June 23, 2014 8:04 AM
Points: 83, Visits: 174
Thanks for the quick reply. What I meant to say is I was able to produce deadlocks even with the indexed tvps.
Post #1427434
Posted Wednesday, March 6, 2013 8:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 6:57 PM
Points: 7,094, Visits: 12,583
mayur birari (3/6/2013)
Thanks for the quick reply. What I meant to say is I was able to produce deadlocks even with the indexed tvps.

There is guarantee it will remove them. If you have two procs that acces the same resources in a different order, both in a transaction, nothing is going to save you. All I am saying is that having a clustered index on the table type will reduce deadlocks in some situations when compared to an equivalent setup minus the clustered index.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1427443
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse