Never trust the Execution Plan. Itβs very useful to let you know what the plan will be, but if you want real indicators, always include statistics
set statistics io on set statistics time on
.. and compare the actual performances. Statistics may say that the expected value is 15% / 85%, but the actual data will show you what it really means.
There is no silver bullet for performance tuning. Even the βbestβ queries may change over time as the form or distribution of your data changes.
The CTE will not differ much, and I'm not sure how you plan to make a PARTITION request on this, but you can try left join .
SELECT gi.GalleryImageId, gi.FbUserId, count(v.GalleryImageId) AS Votes FROM GalleryImage gi LEFT JOIN GalleryImageVote v ON v.GalleryImageId = gi.GalleryImageId GROUP BY gi.GalleryImageId, gi.FbUserId
RichardTheKiwi
source share