Check value if exists in column
EXISTS should normally return as soon as the subquery finds one row that satisfies its WHERE clause. So I think your query is as fast as you can make it.
I was a little surprised that LIMIT 1
seems to always speed up the query very slightly. I didn't expect that. You can see the effect with EXPLAIN ANALYZE
.
EXPLAIN ANALYZE
SELECT exists (SELECT 1 FROM table WHERE column = <value> LIMIT 1);
I have had recent success with the following in some cases:
SELECT count(1) > 0
WHERE column = <value>