Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

The Numbers Table Expand / Collapse
Author
Message
Posted Monday, November 24, 2008 6:49 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:14 AM
Points: 35,371, Visits: 31,915
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #607495
Posted Monday, November 24, 2008 7:10 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, October 16, 2014 3:45 AM
Points: 338, Visits: 1,431
Performance isn't always an issue as in my previous example.
Post #607508
Posted Monday, November 24, 2008 7:17 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 11:46 AM
Points: 332, Visits: 2,252
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.
Post #607512
Posted Monday, November 24, 2008 7:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 3, 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.
Post #607516
Posted Monday, November 24, 2008 7:33 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 7:27 AM
Points: 387, Visits: 435
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"
Post #607524
Posted Monday, November 24, 2008 7:38 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 8:16 AM
Points: 83, Visits: 743
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.
Post #607528
Posted Monday, November 24, 2008 7:59 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, September 25, 2014 9:01 PM
Points: 1,113, Visits: 705
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
SQL Server MVP
SQLblog.com: THE SQL Server Blog Spot on the Web
Post #607541
Posted Monday, November 24, 2008 8:05 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 3, 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.
Post #607548
Posted Monday, November 24, 2008 8:18 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, September 25, 2014 9:01 PM
Points: 1,113, Visits: 705
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
Post #607570
Posted Monday, November 24, 2008 8:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 3, 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.
Post #607586
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse