简体   繁体   中英

Fastest way of checking whether a record exists

What is the fastest way of checking whether a record exists, when I know the primary key? select , count , filter , where or something else?

You can check yourself via EXPLAIN QUERY PLAN which will tell you the cost & what it intends to do for a particular query.

Costs don't directly compare between runs, but you should get a decent idea of whether there are any major differences.

That being said, I would expect COUNT(id) FROM table WHERE table.id="KEY" is probably the ideal, as it will take advantage of any partial lookup ability (particularly fast in columnar databases like amazon's redshift) and the primary key indexing.

When you use count , the database has to continue the search even if it found the record, because a second one might exist. So you should search for the actual record, and tell the database to stop after the first one.

When you ask to return data from the record, then the database has to read that data from the table. But if the record can be found by looking up the ID in an index, that table access would be superfluous. So you should return nothing but the ID you're using to search:

SELECT id FROM MyTable WHERE id = ? LIMIT 1;

Anyway, not reading the actual data and the limit are implied when you are using EXISTS, which is simpler in peewee:

SELECT EXISTS (SELECT * FROM MyTable WHERE id = ?);
MyTable.select().where(MyTable.id == x).exists()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM