Phil Factor SQL Speed Phreak Competition: No 2
This competition is now over, but the winner, Dave, got an Amazon Voucher for $60, and the privilege of being able to display the 'Phil Factor SQL Speed Phreak' award on their own siteIt was quite a struggle with some close competition from many of those who participated in this competition. However, Dave came up with a clever solution that produced an FIFO calculation from a million rows in a just a few seconds. The third Phil Factor Speed Phreak competition will soon be held on here. Watch out! (here is the original preamble.) This competition is to calculate current items in stock and current stock value in FIFO order. I have seen many different algorithms to do this and most of them involve a cursor.
Can it be done more quickly without, or even with, a cursor? In other words, what is the fastest way in SQL Server (any version) to provide this stock inventory report? Beware that the sample file includes the two extra columns; CurrentItems (INT) and CurrentValue (MONEY) ! The table is in this form (simplified from the way we'd do it in a real system of course).
You are welcome to change the two nonclustered indexes to suit your solution. You can download the complete sample data here. I have an idea of my own of the way to do this but I don’t know if it is the fastest. Explanation of FIFO rules (example, abbreviated)
a) First IN add 738 items (each $245.94) to the stock, for a total of $181,503.72 As mentioned before, the CurrentItems and CurrentValue columns in the sample data are only included for you to validate your routines. Here are some guidelines for your entries:1) Include a header in your suggestion. Make sure your name and date is present. I will allow you to use an existing tally number table (make sure it starts with 0). You can use any kind of object for this competition, except SQLCLR. If you are using a fixed tally number table, it has to be named dbo.TallyNumbers and the column named Number. The winner will be amongst the tied fastest entrants (generally there is a group of these) and it will be the one with the highest number of votes. We'll announce the winner in three week's time on 16th November. For a starter, here is a common cursor based solution that you will find in production in many places.
The above is already done. Here is the code to produce the final resultset.
Good luck to you all! Peter Larsson
(comments are locked)
|
## Herman, 30 Oct 2009 ##Hi there, I decided to give it a try since I solved this problem for the stock market once before, using a slow cursor solution, and I always wanted to write it as a Set based solution. This is my first attempt, and I am happy I got the answers right (I think), but I did not optimize it yet. It ran 86 secs on my laptop. I know I should how to do it different, but anyway decided to send this version. It's a good attempt Herman. However, I need you to add an ORDER BY to your final SELECT.
Oct 30 '09 at 02:26 PM
Peso
Thanks for the commenrt, I did forget it. Anyway my solution was a first try and is calculating much more than it is displaying, so I wil post a new solution.
Nov 01 '09 at 12:40 PM
Herman
(comments are locked)
|
|
Hey, all! I've implemented simple approach learned from Celko DW challenge. Here it is:
Regards, Andriy Zabavskyy Hi andriy! Looks like a godd suggestion. However I need to you add an ORDER BY at the end to follow the guidelines.
Oct 27 '09 at 09:47 AM
Peso
It runs very fast (7 second) but returns wrong value. It does return the correct Items.
Oct 27 '09 at 09:54 AM
Peso
I think that it is making the same mistake that I did at first, it seems to be using the most recent price for the OUT items. However, it should be applying the prices to the OUT in a FIFO manner, that is, applying the earliest prices to the earliest OUT items, until all of the IN,RET items at that price are consumed, and then it should start applying the next earliest prices, and so on...
Oct 27 '09 at 12:56 PM
RBarryYoung
Cool approach though, it really has me thinking...
Oct 27 '09 at 12:56 PM
RBarryYoung
Thanks, for the comments. Now I see my mistake. Will work on that..
Oct 27 '09 at 03:49 PM
Andriy Zabavskyy
(comments are locked)
|
|
Another try, with a quirky update. It doesn't perform as fast as the others, but it's another way to solve it quite quickly. This runs in 4.5 seconds, so I rounded it up to 5 seconds. We'll know better when Phil returns.
Nov 07 '09 at 03:42 AM
Peso
He has returned!! I'm just pasting it into the harness...
Nov 10 '09 at 04:58 PM
Phil Factor
(comments are locked)
|
Herman 1d, 9 nov 2009I was calculating much more than I was displaying, so I reconsidered my first algorithm to get to the requested result. I did some attempts to stop the RunningTotals when enough In transacrtions have been processed to cover the stock, but without success. I would never come up with Dave's top 1 (LastStockID) solution. I am using the same indexes:
And here is my 1d solution Thank you. I will try it later when I get clean access to my preliminary test machine.
Nov 02 '09 at 10:38 AM
Peso
(comments are locked)
|
|
Now the query should be correct:
Regards, Andriy Z. Andriy - still not quite... For article 10005 your query gives 1069 items @ 133818.95, whereas the reference query gives 1065 items @ 133316.75... Performance was 21.678 seconds on my box...
Oct 28 '09 at 10:38 AM
Matt Whitfield ♦♦
This is suggestion 1c?
Oct 28 '09 at 12:25 PM
Peso
(comments are locked)
|
« previous 1 2 3 4 5 6 next page »



Was there any particular reason you disallowed CLR functionality?
On an initial look, this would be the ideal scenario in which to use a CLR type...
Two things had us made this decision: 1) We have tried CLR before and there was no performance gain. 2) To test and verify, poster need to disclode the source code for the suggestion. But if you have a CLR routine that runs on 15 seconds or less, I think Phil will be interested in testing it.
Questions: 1) Is the Stock Transaction List referred to the one that will be used for Final Evaluation? If not, can you describe the differences? 2) What is the configuration that entries will be (finally) evaluated on?
Yes, the downloadable file is the data against all suggestions will be measured. Phil will drop or rename the two last columns so that no suggestion can use them. I guess Phil will be using same test harness as last time with 'Subscription List' competition.
Those filtered indexes will only work on SQL Server 2008, right?