April 16, 2010 at 3:18 am
Hello all,
Probabaly this question might seem very novice, but was hanging in my mind for some time..
Assume i use
SELECT TOP N * FROM TABLE_A join TABLE_B
The question is:
Will the engine filter the result set with N rows as soon as the rows start appearing in the result set
or
will the engine first find out all the rows (say N+X rows) producing out of that join and then outputs TOP N rows?
April 16, 2010 at 4:16 am
Looking at the exec plan, you will see that the TOP operator is applied as last.
Looking at the estimated row counts, you'll see that it can actually be applied as first, if no ORDER BY clause is present.
Hope this helps
Gianluca
-- Gianluca Sartori
April 16, 2010 at 5:10 am
Thanks Gianluca.. what if i dont have a order by clause? still the top operator is processed only after getting the result set?
Do u think SET ROWCOUNT will be better than TOP in this scenario?
April 16, 2010 at 5:33 am
The in-memory representation will contain every record from the tabel and then display only TOP N records.This is how TOP works.
Still would advice to use @@ROWCOUNT to avoid un-necessary records to scan
Raunak J
April 16, 2010 at 5:46 am
Thanks Mate!!
Can u provide me a sample of using @@ROWCOUNT to filter the result set?
April 16, 2010 at 6:07 am
COldCoffee (4/16/2010)
Thanks Gianluca.. what if i dont have a order by clause? still the top operator is processed only after getting the result set?Do u think SET ROWCOUNT will be better than TOP in this scenario?
SET ROWCOUNT has the same effect as TOP on the query plan. The only benefit I can see is that you can apply it to client queries without changing the query text.
-- Gianluca Sartori
April 16, 2010 at 6:09 am
Sure,
SET ROWCOUNT 10
--Followed by SQL squery...the query execution will stop once 10 records are scanned
Raunak J
April 16, 2010 at 6:41 am
Raunak Jhawar (4/16/2010)
SET ROWCOUNT 10
Oh ok, u were talking about SET ROWCOUNT only!! OK, i thot u were mentioning about @@ROWCOUNT 😉
Thanks for your time and reply guys... I will tail-gate your replies 😀
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply