|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:01 PM
Points: 32,899,
Visits: 26,779
|
|
jacroberts (11/24/2008) It depends how much performance is an issue, for a lot of queries it is not. For example, if there is a daily report that runs in batch it would not matter. I've tested the query on my development environment it generated and queried the table with 4000 rows in in less than 1 second.
That's pretty slow and performance should be always an issue...
--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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 6:46 AM
Points: 316,
Visits: 1,184
|
|
| Performance isn't always an issue as in my previous example.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Today @ 3:04 AM
Points: 287,
Visits: 1,901
|
|
| Performance might not always be critical, but it is and should always be an issue for anything but a known to be 'short' ad-doc operation. It is good practice to have several well optimized tools in your toolbox and since a number table or number function is so generic, I consider them one of my tools. There is no reason not to use an optimised generic tool over a non-optimized one, ever.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, January 03, 2011 8:48 AM
Points: 26,
Visits: 71
|
|
Thanks for all the responses and thanks yuvalpe for identifying the original source of that query. Like I said, I never claimed to be the author. And I looked for the original source.
MrAkki: The only problem I see with that is that if you are building this in the early stages of building a database you could end up with a smaller numbers table than you need. That can ALWAYS be solved by adding more cross joins, but I liked this simple approach and it performs quite well.
I wasn't attempting to list every possibly method for creating a numbers table, just a few different ways with some pros and cons of each.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 8:01 AM
Points: 313,
Visits: 366
|
|
The first place I came across using a numbers table was from a Joe Celko book. I take a slightly different approach, typically using a view (with the following CTE) to generate the numbers.
WITH I AS ( SELECT iNbr = 0 UNION SELECT iNbr = 1 UNION SELECT iNbr = 2 UNION SELECT iNbr = 3 UNION SELECT iNbr = 4 UNION SELECT iNbr = 5 UNION SELECT iNbr = 6 UNION SELECT iNbr = 7 UNION SELECT iNbr = 8 UNION SELECT iNbr = 9 ), X AS ( SELECT iNbr = I.iNbr * 10 FROM I ), C AS ( SELECT iNbr = I.iNbr * 100 FROM I ), M AS ( SELECT iNbr = I.iNbr * 1000 FROM I ), N AS ( SELECT iNbr = I.iNbr + X.iNbr + C.iNbr + M.iNbr FROM I CROSS JOIN X CROSS JOIN C CROSS JOIN M )
SELECT iNbr FROM N ORDER BY iNbr
I started running into places where I needed the numbers on different servers, with different ranges, etc. I ended up using something like this (above) instead of a table. I do not have any reference as to performance or speed, but it does work quickly in my environment.
Beer's Law: Absolutum obsoletum "if it works it's out-of-date"
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 9:15 AM
Points: 56,
Visits: 487
|
|
Jeff Moden (11/24/2008)
jacroberts (11/24/2008) ... queried the table with 4000 rows in in less than 1 second.That's pretty slow and performance should be always an issue...
With all due respect Jeff, it really does matter what work environment you're in. I have to agree with jacroberts in that 'query' performance indeed is not always an issue. In many cases, worker performance is more important than query performance. If my boss new I was trying to make a query run less than "less than a second", I'm guessing he'd be disappointed in my use of time.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:20 PM
Points: 1,137,
Visits: 667
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, January 03, 2011 8:48 AM
Points: 26,
Visits: 71
|
|
Adam Machanic (11/24/2008) Just throw the CTE into an inline TVF. In my tests it almost always performs equally fast, and sometimes is even faster, than using a table.
Adam Machanic: Have you ever tested the time it takes to build out the CTE with millions of numbers? I'm not sure how you use numbers tables, but we have had the need to have over 8M numbers. A physical table is faster.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:20 PM
Points: 1,137,
Visits: 667
|
|
CheeseMan316 (11/24/2008)
Adam Machanic (11/24/2008) Just throw the CTE into an inline TVF. In my tests it almost always performs equally fast, and sometimes is even faster, than using a table.Adam Machanic: Have you ever tested the time it takes to build out the CTE with millions of numbers? I'm not sure how you use numbers tables, but we have had the need to have over 8M numbers. A physical table is faster.
Yes, I've done some pretty massive tests. You seem to be thinking about the time to insert the numbers into a table, not the time for the engine to internally produce and use the numbers; these are not the same. But if you're routinely dealing with over 8,000,000 numbers, it sounds like you have much bigger problems than performance of a CTE vs. a physical table... What are you actually using these things for?
-- Adam Machanic SQL Server MVP SQLblog.com: THE SQL Server Blog Spot on the Web
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, January 03, 2011 8:48 AM
Points: 26,
Visits: 71
|
|
| I can't really get into what we use it for. But rest assured, I have the need for large numbers tables. I'm not saying it's every day practice, but we have used them.
|
|
|
|