Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


The Numbers Table


The Numbers Table

Author
Message
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)SSC Guru (51K reputation)

Group: General Forum Members
Points: 51389 Visits: 40305
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jonathan AC Roberts
Jonathan AC Roberts
SSC-Addicted
SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)SSC-Addicted (470 reputation)

Group: General Forum Members
Points: 470 Visits: 1843
Performance isn't always an issue as in my previous example.
peter-757102
peter-757102
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 2551
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.
CheeseMan316
CheeseMan316
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 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.
DEK46656
DEK46656
SSC-Addicted
SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)SSC-Addicted (485 reputation)

Group: General Forum Members
Points: 485 Visits: 567
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"
thisisfutile
thisisfutile
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 977
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.
Adam Machanic
Adam Machanic
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1203 Visits: 714
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
CheeseMan316
CheeseMan316
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 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.
Adam Machanic
Adam Machanic
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1203 Visits: 714
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
CheeseMan316
CheeseMan316
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 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.
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