Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check value if exists in column

Tags:

I'd like to know how to maximize speed when querying for the presence of a varchar value in a column in a specific table. I don't need to know where it is, or how many occurrences there are, I just want a true/false. Of course, the column has an index.

Now, I have this:

SELECT exists (SELECT 1 FROM table WHERE column = <value> LIMIT 1); 
like image 901
Perlos Avatar asked Nov 16 '11 09:11

Perlos


People also ask

How do you check if cell value exists in column Excel VBA?

FYI, you can do much easier than the match function: =countif(A:A,12345)>0 will return True if the number is found, false if it isn't.


1 Answers

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); 
like image 115
Mike Sherrill 'Cat Recall' Avatar answered Oct 06 '22 22:10

Mike Sherrill 'Cat Recall'