![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Many of my queries use correlated subqueries to pull unique history rows from the history table for each of a set of objects from the object table, for instance, pulling the earliest history row for each object in a set. These correlated subqueries reference the object table and return the primary key of the history table, e.g.: select * from lp_object l inner join lp_object_history h on h.lp_object_id = l.lp_id where l.lp_set_id = 'SOME_LITERAL' and h.lp_id = ( select top 1 lp_id from lp_object_history where lp_object_id = l.lp_id and lp_some_column > 0 order by lp_time_period) ... I'm considering reclustering by the (object id, time period) index, but then my queries will need an extra bookmark lookup step to get the row data from the identity value returned by the correlated subquery. I think it will still be faster, though, so I will probably build a copy of the table with the alternative clustering scheme to run some performance tests. What I'm wondering is, if I were to dispense with the identity column altogether and replace it with a composite primary key of (object id, time period), would I be still be able to use my correlated subqueries? Because then there wouldn't be a single column that uniquely identifies each row in the history table and I don't think SQL Server supports multicolumn correlated subqueries. |
#3
| |||
| |||
|
#4
| |||
| |||
|
#5
| |||
| |||
|
|
I reclustered the table and the query is of course much faster now. I need to keep both versions of the table around for a while to see how they compare for all the queries we run, so I have an insert trigger keeping them in sync. |
|
I tried the other permutations of the query you suggested, Erland, and they had no effect on the execution plan or performance. |
![]() |
| Thread Tools | |
| Display Modes | |
| |