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)
|
I modified one index as shown below Hi Scot! Great to see you enter the competition. I will test this suggestion when I have clean access to preliminary test machine.
Nov 03 '09 at 06:00 AM
Peso
My index seek still has a lot of executions compared to Dave's entry. If I have time this weekend I will see if I can improve on it
Nov 03 '09 at 03:27 PM
ScotHauder_
You have time. Deadline is set to November 16.
Nov 03 '09 at 04:54 PM
Peso
(comments are locked)
|
|
What do you think? Please comment here what you think. Wow, have I been there, Peso. It's probably Phil's call, but my feeling is NO. I say this knowing that I may be in your position too in the near future (hopefully :-)). The problem is that no matter how fair you are, sooner or later someone is going to feel cheated because you were both a competitor and the Question Asker (or worse, Tester/Judge). What I would recommend is just throwing you solution in at the end, in the final measurements, as an example non-competing entry. I.e., non-eligible to win.
Oct 23 '09 at 05:41 PM
RBarryYoung
I respect that. If this is the consensus, I will post my set-based solution very last before deadline so that no-one can steal my thunder :-)
Oct 23 '09 at 05:51 PM
Peso
Oh boy! Someone already voted this as an unhelpful post grin
Oct 23 '09 at 05:59 PM
Peso
I think the series would be more fun if we disallow the current winner from entering, since it gives other people a chance! Actually, I think we should be allowed to steal Peso's thunder too at some point!
Oct 24 '09 at 05:23 AM
Phil Factor
Well, I'm upvoting it, so they should cancel out, ;-)
Oct 24 '09 at 03:24 PM
RBarryYoung
(comments are locked)
|
|
I can't find the Dave 3.a version in answer list. 3.a has been superseded , 3.d is now the latest
Nov 11 '09 at 09:23 AM
dave ballantyne
(comments are locked)
|
|
Here is my cursor solution as counterweight to all set-based solutions :-)
(comments are locked)
|
|
Ok, well I would post a pure set based solution, except that the site is refusing to let me post the whole thing!! Lame. Anyway, the SQL is just
It runs in about 6.3 seconds on my box. There are two source files, one for the support class (mostly the same guts as the CLR type version, except for the fact that IBinarySerialize support is removed, and it is now class, rather than struct based). FIFOStock Procedure:
StockStack class:
(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?