Consider this SQL performance difference, where in the first, I select all 26,000 rows, and in the second, I just want the first 5.
SELECT tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.[timestamp]
vs
SELECT TOP (5) tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.[timestamp]
Without: CPU = 201 | Reads: 6880 | Writes: 0 | Duration: 451
With: CPU = 302439 | Reads: 7453199 | Writes: 3169 | Duration: 74188
This just doesn't make sense to me... Is there another way to go about this?
After Martin's suggestion of a REBUILD STATISTICS on all tables involved, there is a small improvement, but the trick with changing the TOP amount into a parameter works the best.
Before statistics rebuild:
CPU = 302439 | Reads: 7453199 | Writes: 3169 | Duration: 74188
After statistics rebuild:
CPU = 127734 | Reads: 4100436 | Writes: 2656 | Duration: 16880
With parameter:
CPU = 218 | Reads: 6899 | Writes: 0 | Duration: 83
Query with parameter:
DECLARE @TOP INT; SET @TOP=5;
SELECT TOP (@TOP) tw.*
FROM entity e
JOIN entity_tag et on et.entity_id = e.id
JOIN tag t on t.tag_id = et.tag_id
JOIN tagrelatedtweets trt on trt.FK_Tag_ID = t.tag_id
JOIN tweets tw on tw.PK_Tweet_ID = trt.FK_Tweet_ID
WHERE e.id = 765131
ORDER BY tw.timestamp desc
One last remark for those of you using Entity Framework; if you experience this behavior, you can simulate the same parameter-based behavior as follows:
.Take(100).ToList().Take(5)
I know it isn't pretty, but it's the only way to trigger the correct execution plan if you are using entity framework as far as I can tell.
Thanks a lot for pointing me in the correct direction Martin!
TOP 5
might have some nested loops where the other one doesn't? Also what if you tryDECLARE @TOP INT; SET @TOP=5; SELECT TOP (@TOP)...
– Martin Smith Sep 7 '11 at 13:02