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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 are cursor's still slow??

Author  Topic 

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 04:14:09
I know with SQL cursors have been cursed with being slow, i'm wondering what is the best alternative, is it still this one:

http://www.sql-server-performance.com/dp_no_cursors.asp

or has someone thought of anything else...

I just wanna know a simple alternative in general i know it matter which application but im curious in general...

or should a temp table be used...?

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-11-23 : 04:29:26
A set-based solution is always going to give you the best performance. Just ask yourself repeatedly "do i really need to do this row-by-row?"

Mark
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 04:33:54
im new at this , what exactly is a set based solution?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-23 : 04:41:35
Set-based solution = NOT (procedural row-by-row solution)

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 04:44:23
any examples of a simple cursor side by side to set based solution, or something that explains it a little more then harsh's example?
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 04:52:17
like for example if u wanted to create a new field and assign each row a new value which is incremented by one, surely u cant do this set based
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-23 : 05:00:48
Plenty of such examples are there, but I will give you a simple one!

Lets say there is table called Acct_tran (Account Transactions) and has following structure:

create table acct_tran
(
acct_no int,
tran_dt datetime,
cr_bal numeric(10,2),
dr_bal numeric(10,2)
)


and has following data:

insert into acct_tran
select 1, '20060101', 100, 0 union all
select 1, '20060102', 0, 50 union all
select 1, '20060103', 160, 0 union all
select 2, '20060101', 0, 80 union all
select 2, '20060102', 0, 570 union all
select 2, '20060103', 700, 0 union all
select 3, '20060101', 860, 0 union all
select 3, '20060102', 0, 80


Now we want to know the running balance for each account like this:

acct_no     cr_bal       dr_bal       balance                                  
----------- ------------ ------------ ----------------------------------------
1 100.00 .00 100.00
1 .00 50.00 50.00
1 160.00 .00 210.00
2 .00 80.00 -80.00
2 .00 570.00 -650.00
2 700.00 .00 50.00
3 860.00 .00 860.00
3 .00 80.00 780.00


Then we can have following query to calculate running balance:

select 
acct_no, cr_bal, dr_bal,
(select sum(cr_bal - dr_bal) from acct_tran b where a.acct_no = b.acct_no and b.tran_dt <= a.tran_dt) balance
from acct_tran a
order by acct_no, tran_dt

rather than looping through entire table to calculate running balance.


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-11-23 : 05:16:52
quote:
like for example if u wanted to create a new field and assign each row a new value which is incremented by one, surely u cant do this set based
You might want to look up IDENTITY before writing that cursor!

Mark
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-23 : 05:18:32
Or create a CURSOR based on Acct and TranDate?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 05:24:32
wow thats good, now gotta try understand that :)

Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 05:51:53
just a questions , uve set a and b... what is their purpose, and excuse my silly question but what are they considered (e.g variable?)
Go to Top of Page

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2006-11-23 : 06:04:10
These are aliases. Writing
SELECT a.Column1, a.Column2 FROM MyLongNamedTableTable a
is a shorthand way of writing
SELECT a.Column1, a.Column2 FROM MyLongNamedTableTable AS a
and is more succinct than writing
SELECT MyLongNamedTableTable.Column1, MyLongNamedTableTable.Column2 FROM MyLongNamedTableTable
It's a good habit to get into as it saves repeating long table names all over the place, and improves maintainability.
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 06:11:49
excellent, man this forum is good... (fast too)

okay one more thing

whats the purpose of this

"b.tran_dt <= a.tran_dt"

why are u making sure date is less then or equal to?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-23 : 06:13:52
So that no other transactions are calculated (future ones, regarding to current transactiondate).


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-11-23 : 06:53:15
Also make sure that you know SQL

http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2006-11-23 : 08:42:28
SET-BASED.......use a large spoon to put sugar in a tea-cup.
CURSOR-BASED....use a large tweezers to put sugar in a tea-cup...grain by grain!!!
Go to Top of Page

rnbguy
Constraint Violating Yak Guru

293 Posts

Posted - 2006-11-23 : 08:46:59
yeah i understand the meaning i just cant get my mind around understanding the logic, how the computer is working in set based examples such as one mensioned above...? isnt it still looking threw eachother field, i think i just need to visualise the steps better, that the computer is taking
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-23 : 09:08:05
Set-based solution is better because we let SQL server handles the way it want to perform the processing...In some case, it may even process two steps in parallel.

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page
   

- Advertisement -