When querying with MSSQL what is the most efficient way to grab a single column from a computed table and connect it to a result set.
Tables: mytable = 20k rows index on mytable.col1, othertable = 30k rows, no index on othertable.col1
Inline Query – Runs an query in the select statement
SELECT * FROM (
SELECT col1, col2, col3,
col4 = (SELECT min(col5) FROM othertable o WHERE m.col1 = o.col1)
row = ROW_NUMBER() OVER(ORDER BY somerow)
FROM mytable m
) as paged
WHERE row BETWEEN 1 AND 25
Join Query – Joins our table onto the computed table
SELECT * FROM (
SELECT col1, col2, col3, o2.col5again
row = ROW_NUMBER() OVER(ORDER BY somerow)
FROM mytable m
JOIN (SELECT col1, min(col5) as col5again FROM othertable o GROUP BY col1) as o2 ON o2.col1 = m.col1
) as paged
WHERE row BETWEEN 1 AND 25
My gut instinct was the JOIN was faster. Yet, upon testing the inline query would finish in an average of 7 seconds while the other query would take >30 seconds when executed in MSSQL studio.
- Is using an inline select query really the best way to inject the single column?
- Does the query optimized wait to run the inline
SELECT()statement until after the results have been paged, would that explain the different in running time?
FYI: In my specific example we added an index to othertable.col1 and it reduced the query time to 0s, but this question focuses more on whether the JOIN versus SELECT() is better.
Performance tuning is an art that involves understanding why plans are created the way they are, what each piece in the plan does, and how you can influence a more efficient path to be chosen.
Missing covering indexes will cause scans (excessive reads). Too many indexes will slow down your DUI’s (Deletes, Updates, and Inserts). Outdated or missing statistics will cause the wrong join algorithm to be used and/or an inaccurate estimated row count (which could lead to paging).
Run both queries in the same window and include the actual execution plan. This will split the plans up and tell you which one is more expensive. It will give you missing index hints. As you get better at reading plans, you’ll learn how to improve your query performance.