Re: tricky query
От | Cosimo Streppone |
---|---|
Тема | Re: tricky query |
Дата | |
Msg-id | 42C1A5EF.7070505@streppone.it обсуждение исходный текст |
Ответ на | Re: tricky query (John A Meinel <john@arbash-meinel.com>) |
Список | pgsql-performance |
John A Meinel wrote: > John A Meinel wrote: >> Merlin Moncure wrote: >> >>> I need the smallest integer that is greater than zero that is not in the >>> column of a table. In other words, if an 'id' column has values >>> 1,2,3,4,6 and 7, I need a query that returns the value of 5. >> >> [...] > > Well, I was able to improve it to using appropriate index scans. > Here is the query: > > SELECT t1.id+1 as id_new FROM id_test t1 > WHERE NOT EXISTS > (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1) > ORDER BY t1.id LIMIT 1; I'm very interested in this "tricky query". Sorry John, but if I populate the `id_test' relation with only 4 tuples with id values (10, 11, 12, 13), the result of this query is: cosimo=> create table id_test (id integer primary key); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'id_test_pkey' for table 'id_test' CREATE TABLE cosimo=> insert into id_test values (10); -- and 11, 12, 13, 14 INSERT 7457570 1 INSERT 7457571 1 INSERT 7457572 1 INSERT 7457573 1 INSERT 7457574 1 cosimo=> SELECT t1.id+1 as id_new FROM id_test t1 WHERE NOT EXISTS (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1) ORDER BY t1.id LIMIT 1; id_new -------- 15 (1 row) which if I understand correctly, is the wrong answer to the problem. At this point, I'm starting to think I need some sleep... :-) -- Cosimo
В списке pgsql-performance по дате отправления: