Is EXISTS more efficient than COUNT(*)>0?

后端 未结 5 1585
孤城傲影
孤城傲影 2020-12-08 13:48

I\'m using MySQL 5.1, and I have a query that\'s roughly of the form:

select count(*) from mytable where a = \"foo\" and b = \"bar\";

In my

5条回答
  •  伪装坚强ぢ
    2020-12-08 14:49

    This might be an approach too.

    select 1 from mytable where a = "foo" and b = "bar" limit 1;
    

    This would not traverce all records that meet the where condition but rather return '1' after first 'hit'. The drawback is you need to check for result, because there might be empty record set in return.

提交回复
热议问题