Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
If I do this:SELECT name, description, ( SELECT COUNT(1)FROM imagesWHERE setid = image_sets.id) AS image_count FROM image_setsWHERE id = $VARIABLEDoes it execute the subquery with the count on every row in the database before comparing the WHERE statement? In which case, it would be more efficient to do this:SELECT name, description, ( SELECT COUNT(1)FROM imagesWHERE setid = sets.id) AS image_count FROM (SELECT id, name, description, FROM image_setsWHERE id = $VARIABLE)AS sets
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2006-09-11 : 00:55:30
Try this
SELECT $VARIABLE ID, is.Name, is.Description, SUM(CASE WHEN i.setid is null then 0 else 1 end) image_countFROM image_sets isLEFT JOIN images i ON i.setid = is.idWHERE is.id = $VARIABLEGROUP BY is.Name, is.Description
Peter LarssonHelsingborg, Sweden
LoztInSpace
Aged Yak Warrior
940 Posts
Posted - 2006-09-11 : 19:36:38
Try looking at the query plans for each variation - that will tell you.