16

For those who are not familiar with an auxiliary 'numbers' or 'tally' table in a database, it's essentially just a single-column table that stores a sequence integers - usually from 0 to whatever large-ish number is suitable (e.g a few hundred thousand to a couple million). For such a simple concept, these are surprisingly useful to have kicking around your database; generating a sequence of dates boils down to 3 LOC

select getdate() - n.Number
from dbo.Numbers n
where n < 180;

as opposed to the traditional eyesore

declare @Dates table ([Value] datetime);
declare @iter int = 0;

while @iter < 180
begin

  insert into @Dates ([Value]) 
  values (getdate() - @iter);

  set @iter += 1;

end;

select [Value]
from @Dates;

Given some of the more sophisticated features available on the SEDE, such as execution plans, colored graphs, the ability to create stored procedures, etc..., I was a little surprised to find a numbers table missing from sys.tables. To me, implementing this feature seems like a no-brainer:

  • It should take like 45 seconds for someone to create; just dump some integers into a table and throw a couple of indexes on it
  • Little-to-no maintenance; it's values don't need to be updated, deleted, etc... over time
  • Requires a trivial amount of storage space
  • Incredibly useful

How do we feel about this?


I ran a quick benchmark comparing a simple operation using a numbers table and a recursive CTE (YRMV). The former approach yielded a much simpler execution plan and required a radically lower number of logical reads. While these measurements are certainly not the be-all end-all justification for such a feature, I think the results are fairly compelling.

10
  • 1
    Or use a CTE: stackoverflow.com/a/3236510/1663001
    – DavidG
    Jan 12, 2016 at 11:24
  • 2
    I frequently do, but having a properly indexed persistent table is much more efficient than every user generating the data on the fly every time they need it.
    – nrussell
    Jan 12, 2016 at 11:28
  • 1
    So why did you post a loop instead of a CTE?
    – DavidG
    Jan 12, 2016 at 11:29
  • 5
    It was just the first thing that came to mind. It's irrelevant though because using a loop or using a recursive CTE to accomplish this are both vastly inferior alternatives to a numbers table.
    – nrussell
    Jan 12, 2016 at 11:31
  • 4
    "vastly inferior" is a subjective term. However, I think you oversimplify just how easy it is to change the SEDE database, remember that it gets rebuilt constantly. Personally I don't think it's worth them touching it for this when it's so simple and not really a big resource drain to use a CTE (or even a loop)
    – DavidG
    Jan 12, 2016 at 11:36
  • 6
    Sure, I may be over simplifying to some extent, but I would like to get input from someone with backend knowledge of the SEDE database. I am truly skeptical that having this table could be any significant drain on hardware resources or developer time, but I'm not going to argue about it further if we are both just speculating at this point.
    – nrussell
    Jan 12, 2016 at 11:45
  • 2
    In SEDE, I've been using master.dbo.spt_values where name is null. That contains 2048 sequential values. In my other environments, I have a CLR table-valued GetRange() function which creates an arbitrary range.
    – canon
    Jan 12, 2016 at 19:28
  • 1
    @canon Thanks for pointing that out! I wasn't aware that we were able to access the master database.
    – nrussell
    Jan 12, 2016 at 20:16
  • 1
    I've notified Tim Stone, the SEDE dev.
    – ArtOfCode
    Jan 12, 2016 at 21:46

1 Answer 1

2

Each site has its own database. That database is recreated from scratch every Sunday. There is one extra common database (called Data), but that one holds critical system data, including users. The identity that runs your query doesn't have access to that schema.

While I admit that it is trivial to add such number table, I don't expect Stack Exchange to be willing to add this to their database creation script.

You've been offered several alternatives in the comments and told how to create such a table when the need arises. I've had the need once or twice and could happily achieve my goal with the workarounds presented in the comments.

If I needed such a table today, I would use the ROW_NUMBER function:

select top 40000
row_number() over (order by id) [number]
from postswithdeleted

The PostsWithDeleted table is large enough to facilitate wide ranges

Generating 40,000 numbers gives me these stats:

Table 'PostsWithDeleted'.  
Scan count 1, 
logical reads 4819,   
read-ahead reads 8859, 

 SQL Server Execution Times:
   CPU time = 47 ms,  elapsed time = 61 ms

I removed the stats that were zero. This timing seems better then the stats I saw in your Gist. Only the reads are significantly worse.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .