Re: [GENERAL] locking/insert into table and transactions

Поиск
Список
Период
Сортировка
От Lincoln Yeoh
Тема Re: [GENERAL] locking/insert into table and transactions
Дата
Msg-id 3.0.5.32.19991126170409.00841b20@pop.mecomb.po.my
обсуждение исходный текст
Ответ на Re: [GENERAL] drop/rename table and transactions  (Lincoln Yeoh <lylyeoh@mecomb.com>)
Список pgsql-general
Hi,

I'd like to prevent duplicate ids from being inserted into a table. I can
let the database enforce it by using UNIQUE or PRIMARY KEY. But assuming I
prefer to catch such things with the application, what would be the best
way of doing it?

The only way I figured to do it was to use:
begin;
lock table accounts;
select count(*) from accounts where id=$number;
 if count=0, insert into accounts (id,etc) values ($number,$etc)
commit;

Is this a good idea? Or is it much better and faster to let the database
catch things?

Is it faster to use "select count(*) from accounts" or "select id from
accounts"?

Apparently count(*) has some speed optimizations in MySQL. So wondering if
there are similar things in Postgres.

Thanks,

Link.


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

Предыдущее
От: Oleg Broytmann
Дата:
Сообщение: Re: [GENERAL] Postgres on Ultrasparc
Следующее
От: Mike Mascari
Дата:
Сообщение: Re: [HACKERS] Re: [GENERAL] drop/rename table and transactions