SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculating interest query


Calculating interest query

Author
Message
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35958 Visits: 11361
Chris,

Turns out all that is needed to get rid of the index spool is to define the clustered index as UNIQUE. Given that information, a better plan is produced (without the extra index spool), and the recursive CTE now starts to return results after four seconds on my laptop. Well done!

Paul



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
ChrisM@home
ChrisM@home
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5270 Visits: 10608
Paul White (2/27/2010)
Chris,

Turns out all that is needed to get rid of the index spool is to define the clustered index as UNIQUE. Given that information, a better plan is produced (without the extra index spool), and the recursive CTE now starts to return results after four seconds on my laptop. Well done!

Paul


Heh nice work Paul!
I figured it would be sufficient to bring the time down to a few seconds - it only took half an hour with Jeff's modified sample generator - but this is the icing on the cake. I'll try this on the pagination query at work. Thanks!


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217284 Visits: 41991
Paul White (2/27/2010)
This is a method called 'set-based iteration' by MVP Hugo Kornelis, which was first brought to my attention here in a discussion of one of Jeff's fine articles.


Heh... that's why I listed cursors as the "third fastest". I even helped Hugo with a tweak on his fine method.

I have shamelessly re-used Jeff's fine test script, up to the point marked 'Solution to the problem starts here'. The following code runs from that point, and again draws heavily on Jeff's work. I hope he won't mind.


I never mind but this one was all provided by the OP. :-) He did a really fine job of providing test tables and data especially for a newbie to the forum.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217284 Visits: 41991
Paul White (2/27/2010)
Chris,

Turns out all that is needed to get rid of the index spool is to define the clustered index as UNIQUE. Given that information, a better plan is produced (without the extra index spool), and the recursive CTE now starts to return results after four seconds on my laptop. Well done!

Paul


Was that for the 100k rows, Paul?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
FarNorthDBA
FarNorthDBA
SSC-Addicted
SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)

Group: General Forum Members
Points: 433 Visits: 184
I'm overwhelmed with all the help. When I first posted this problem I figured it might be quite some time if ever that someone came up with a solution. Thank you all so much. I will be running these queries and puzzling out how they work on monday. I will let you all know how it goes. Once again thank you so much.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217284 Visits: 41991
nathan 7372 (2/27/2010)
I'm overwhelmed with all the help. When I first posted this problem I figured it might be quite some time if ever that someone came up with a solution. Thank you all so much. I will be running these queries and puzzling out how they work on monday. I will let you all know how it goes. Once again thank you so much.


You only have yourself to thank. :-) Thanks for taking the time to read the "etiquette" article I led you to... I just knew a bunch of great people would jump in on this one if they had a little data to work with. Well done on the data you posted, Nathan.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35958 Visits: 11361
Jeff Moden (2/27/2010)
Paul White (2/27/2010)
Chris,

Turns out all that is needed to get rid of the index spool is to define the clustered index as UNIQUE. Given that information, a better plan is produced (without the extra index spool), and the recursive CTE now starts to return results after four seconds on my laptop. Well done!

Paul


Was that for the 100k rows, Paul?

Sure was, yes.



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35958 Visits: 11361
Jeff Moden (2/27/2010)
nathan 7372 (2/27/2010)
I'm overwhelmed with all the help. When I first posted this problem I figured it might be quite some time if ever that someone came up with a solution. Thank you all so much. I will be running these queries and puzzling out how they work on monday. I will let you all know how it goes. Once again thank you so much.


You only have yourself to thank. :-) Thanks for taking the time to read the "etiquette" article I led you to... I just knew a bunch of great people would jump in on this one if they had a little data to work with. Well done on the data you posted, Nathan.

+1 :-)



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
FarNorthDBA
FarNorthDBA
SSC-Addicted
SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)SSC-Addicted (433 reputation)

Group: General Forum Members
Points: 433 Visits: 184
I just wanted to let everyone know how things went. I have to say I learned a lot about the different ways to solve this problem. All three queries ran exceptionally fast. I had to modify Chris' query a tad since it was off by one month on the interest rates used. Since the interest rates are so similiar it only had a minor effect on the outcome.

On an interesting note just by using the different datatypes between Jeff and Paul we get a difference of $374 out of a total around $4.5M. Just goes to show you the power of decimal places and rounding.

Thank you all for tackling this problem. I really feel I learned a lot and the accounting department is ecstatic to finally have the numbers so we can move forward. Thank you again.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)SSC Guru (217K reputation)

Group: General Forum Members
Points: 217284 Visits: 41991
Thanks for the feedback. I am a bit confused, though... I thought Paul and I both used DECIMAL(28,17).

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search