Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
During all my training and code reviews etc. we are always reminded to avoid Record Sets in our DB code because it hampers performance.

Firstly, I would like to know why.

Secondly, does it have the same performance drain if I create a Record Set on a table variable? (e.g. @myTable). I'd think no since the data being accessed is located in the RAM (And in my case it's only a hand full of records, so the amount of RAM used should be very small)

Comments?
Posted

Try it. If you aren't sure about timings, create two versions of the same code and use the Stopwatch class[^] to time them over say, 1000 iterations.
Then you will have numbers to base a decision on, which you can bring up at your next code review...
 
Share this answer
 
I'm not sure what you mean by record sets. Do you mean with in the DB ? Subqueries do hamper performance, b/c they create a nested loop, so each iteration on the outer loop, runs through the inner loop again. But, no matter what, your records live in RAM. And yes, if your tables are not going to be very big, ever, then the problem is probably non-existant. But, never make the mistake of testing with smaller data sets when your client will end up with large ones.
 
Share this answer
 
Comments
MatthysDT 4-Jul-11 6:30am    
Well, best way to describe what I mean is by code. The following code is in a Stored Procedure on the DB:

DECLARE rst CURSOR LOCAL
FOR
SELECT a.field, b.field, c.field from myATable a
join.... -- join for myBTable
join.... -- join for myCTable
join.... etc
WHERE..... AND.... AND....
ORDER BY........ --(get the picture?)
OPEN rst
FETCH NEXT FROM rst INTO @a, @b, @c
WHILE (@@fetch_status = 0)
BEGIN
Print @a + @c + @b
FETCH NEXT FROM rst INTO @a, @b, @c
END

In this example, would it be faster to create a table variable, say, myABCTable and populate it with the required data, and use @myABCTable in the record set instead of the DB directly. (This means all WHERE clauses and JOINS are moved out of the record set's select)

I avoid sub-queries where possible and try to make all queries work with JOINS instead.

Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900