MySQL: check if Exist Row (s) matches the condition

I have been doing this for quite some time:

SELECT COUNT(*) FROM Table WHERE Condition = *Condition*;

Since I'm not interested in the total number of rows returned, I'm wondering if there is a more efficient way to check if there are any rows that match the condition, preventing MySQL from scanning the entire table.

+5
source share
2 answers
SELECT CASE
         WHEN EXISTS(SELECT *
                     FROM   YourTable
                     WHERE  Condition = '*Condition*') THEN 1
         ELSE 0
       END  
+5
source

Try

SELECT COUNT(*) FROM SmallTable
WHERE EXISTS(SELECT * FROM Table WHERE Condition = *Condition*)
+6
source

All Articles