Hey Everyone,
I have a SQL Query that I am executing on MS SQL Server 2008 (with compatibility mode). This query uses 3 CTEs and returns data anywhere in the range of 5000 to 40K rows.
The query roughly looks like this:
Here @lowerBound and @upperBound hold the same value for all the CTEs and CTE_2 & CTE_3 are pretty much the same except that the where clause checks for different values in a column of table2 (I did not use just one CTE with an in clause cause I believe that it would have made the CTE a little slower in execution).
The query suffices my purpose, however, I am seeing different execution time for the same query, with same params and same result, every time I run it. For example, certain parameter values result in say 35K rows...then for the first run it takes anywhere between 15-20 secs where as for second it could be 10-15 secs, then for third it could be 5-10 secs.
Any ideas as to why this difference in execution time! Again, this is on MS SQL Server 2008 (with compatibility mode).
Any suggestions for query optimization are also welcome (although, I do hope that the rough query provides enough info for such suggestions] :-D
I have a SQL Query that I am executing on MS SQL Server 2008 (with compatibility mode). This query uses 3 CTEs and returns data anywhere in the range of 5000 to 40K rows.
The query roughly looks like this:
Code:
WITH CTE_1 (col1, col2...) as (
select something, something, ...
from table1
--some inner joins
where something_in_table1 between @lowerBound and @upperBound
),
CTE_2 (col1, col2...) as (
Select something, something, ...
from table2
-- some inner joins
where something_in_table2_1 = @something1
and something_in_table2_2 between @lowerBound and @upperBound
),
CTE_3 (col1, col2...) as (
Select something, something, ...
from table2
-- some inner joins
where something_in_table2_1 = @something2
and something_in_table2_2 between @lowerBound and @upperBound
)
select CTE_1.*, CTE_2.col2, CTE_3.col2
from CTE_1
LEFT OUTER JOIN
CTE_2 ON CTE_1.col2 = CTE_2.col2
LEFT OUTER JOIN
CTE_3 ON CTE_1.col2 = CTE_3.col2
Here @lowerBound and @upperBound hold the same value for all the CTEs and CTE_2 & CTE_3 are pretty much the same except that the where clause checks for different values in a column of table2 (I did not use just one CTE with an in clause cause I believe that it would have made the CTE a little slower in execution).
The query suffices my purpose, however, I am seeing different execution time for the same query, with same params and same result, every time I run it. For example, certain parameter values result in say 35K rows...then for the first run it takes anywhere between 15-20 secs where as for second it could be 10-15 secs, then for third it could be 5-10 secs.
Any ideas as to why this difference in execution time! Again, this is on MS SQL Server 2008 (with compatibility mode).
Any suggestions for query optimization are also welcome (although, I do hope that the rough query provides enough info for such suggestions] :-D