Insert unique fails, still increments ID in a lookup table

Поиск
Список
Период
Сортировка
От Mark Felegyhazi
Тема Insert unique fails, still increments ID in a lookup table
Дата
Msg-id 813829.42582.qm@web54408.mail.yahoo.com
обсуждение исходный текст
Ответы Re: Insert unique fails, still increments ID in a lookup table
Re: Insert unique fails, still increments ID in a lookup table
Список pgsql-general
Hi,

Could you please give me some hints how to optimize this in my DB? I have a main table and a lookup table as follows:

create table main (
      id bigserial primary key;
   item_id bigint references lookup (item_id);
);

create table lookup (
  item_id bigserial primary key;
  item_name text unique not null;
);

When a new item arrives (in a temp table), I'm checking if it's already in the lookup table and insert it if not. I do
thiswith a trigger function using the following snippet: 
------
  begin
    insert into lookup values (default,NEW.item_name) returning item_id into itid;
  exception
    when unique_violation then
    select into itid item_id from lookup where item_name=NEW.item_name;
  end;
  NEW.item_id := itid;
------

The problem is that the uniqueness check apparently increases the serial counter and hence I burn through the bigint
IDsmuch faster. It's a waste for 100m+ records... 

An example result for the main table where the second item arrives at the 4th record:

id | item_id
----------------
1 | 1
2 | 1
3 | 1
4 | 4
5 | 5
...

the lookup table becomes:

item_id | item_name
----------------------------
1 | apple
4 | orange
5 | banana
...

Any thoughts?

Thanks,
Mark

PS: I'd like to keep the unique property, because it makes the insert check fast and simple.





В списке pgsql-general по дате отправления:

Предыдущее
От: "Scot Kreienkamp"
Дата:
Сообщение: Re: VMWare file system / database corruption
Следующее
От: Scott Ribe
Дата:
Сообщение: Re: Insert unique fails, still increments ID in a lookup table