Re: tricky query
От | Merlin Moncure |
---|---|
Тема | Re: tricky query |
Дата | |
Msg-id | 6EE64EF3AB31D5448D0007DD34EEB3415C2BF5@Herge.rcsinc.local обсуждение исходный текст |
Ответ на | tricky query ("Merlin Moncure" <merlin.moncure@rcsonline.com>) |
Список | pgsql-performance |
> Merlin Moncure wrote: > > > I need a fast way (sql only preferred) to solve the following problem: > > I need the smallest integer that is greater than zero that is not in the > > column of a table. > > > > I've already worked out a query using generate_series (not scalable) and > > pl/pgsql. An SQL only solution would be preferred, am I missing > > something obvious? > Probably not, but I thought about this "brute-force" approach... :-) > This should work well provided that: > > - you have a finite number of integers. Your column should have a biggest > integer value with a reasonable maximum like 100,000 or 1,000,000. > #define YOUR_MAX 99999 [...] :-) generate_series function does the same thing only a little bit faster (although less portable). generate_series(m,n) returns set of integers from m to n with time complexity n - m. I use it for cases where I need to increment for something, for example: select now()::date + d from generate_series(0,355) as d; returns days from today until 355 days from now. Merlin
В списке pgsql-performance по дате отправления: