EXISTS比COUNT(*)> 0更有效吗? [英] Is EXISTS more efficient than COUNT(*)>0?

查看:291
本文介绍了EXISTS比COUNT(*)> 0更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆