EXISTS比COUNT(*)> 0更有效吗?

编程入门 行业动态 更新时间:2024-10-19 23:34:22
本文介绍了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更有效吗?

本文发布于:2023-11-25 22:52:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更有效   COUNT   EXISTS   gt   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!