I’m trying to find the most efficient way to determine if a table row exists.
I have in mind 3 options:
-
SELECT EXISTS(SELECT 1 FROM table1 WHERE some_condition);
-
SELECT 1 FROM table1 WHERE some_condition LIMIT 0,1;
-
SELECT COUNT(1) FROM table1 WHERE some_condition;
It seems that for MySQL the first approach is more efficient:
Best way to test if a row exists in a MySQL table
Is it true in general for any database?
UPDATE:
I’ve added a third option.
UPDATE2:
Let’s assume the database products are mysql, oracle and sql-server.
I would do
But I don’t think it makes a significant difference unless you call it a lot (in which case, I’d probably use a different strategy).