Fastest check if row exists in PostgreSQL

前端 未结 8 1340
有刺的猬
有刺的猬 2020-11-28 19:20

I have a bunch of rows that I need to insert into table, but these inserts are always done in batches. So I want to check if a single row from the batch exists in the table

8条回答
  •  臣服心动
    2020-11-28 19:57

    as @MikeM pointed out.

    select exists(select 1 from contact where id=12)
    

    with index on contact, it can usually reduce time cost to 1 ms.

    CREATE INDEX index_contact on contact(id);
    

提交回复
热议问题