我正在使用MySQL 5.1,并且我有一个查询,其形式大致如下:
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 program, the only thing that it checks is whether this is zero or nonzero. If I convert this into:
select exists(select * from mytable where a = "foo" and b = "bar");MySQL是否足够聪明,足以在第一个出现时停止搜索?还是有其他与MySQL通讯的方式,我的目的仅仅是找出是否有任何记录与此匹配,而我不需要确切的计数?
is MySQL smart enough to stop searching when it hits the first one? Or is there some other way to communicate to MySQL that my intent is simply to find out if any records match this, and I don't need an exact count?
推荐答案是的,当使用Exists函数返回一行时,MySQL(据我所知实际上是所有数据库系统)都将停止处理.
Yes, MySQL (indeed all database systems as far as I'm aware) will stop processing when a row is returned when using an Exists function.
您可以在MySQL文档中阅读更多信息: 如果子查询完全返回任何行, EXISTS子查询为TRUE.
You can read more at the MySQL documentation: If a subquery returns any rows at all, EXISTS subquery is TRUE.
更多推荐
EXISTS比COUNT(*)> 0更有效吗?
发布评论