|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 7:40 AM
Points: 43,
Visits: 86
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 7:40 AM
Points: 43,
Visits: 86
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 12:30 PM
Points: 32,893,
Visits: 26,770
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 7:40 AM
Points: 43,
Visits: 86
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 7:40 AM
Points: 43,
Visits: 86
|
|
| Thanks for the quick reply. What I meant to say is I was able to produce deadlocks even with the indexed tvps.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|