Mysql SELECT COUNT(*) 还是 SELECT 1?磷酸二氢钾

发布于 2024-12-17 11:46:30 字数 786 浏览 0 评论 0原文

众所周知,PDO 不支持 COUNT(*) 并且像下面这样的查询会失败,因为它不返回任何受影响的行,

$q = $dbc -> prepare("SELECT COUNT(*) FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();

经过一些研究,我发现您还可以使用其他计数方法来获取行数,根本不使用计数,例如以下查询应该与上面相同,但会返回正确的 PDO,

$q = $dbc -> prepare("SELECT 1 FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();

互联网上有各种来源声称;

"SELECT COUNT(*)
"SELECT COUNT(col)
"SELECT 1

彼此都是相同的(有一些差异)那么为什么使用 mysql 的 PDO 不能正确返回真实的计数,是否

"SELECT 1 

有效?

计数方法讨论

为什么 Select 1 更快比选择计数(*)?

It has long been known that PDO does not support COUNT(*) and a query like below would fail as it doesn't return any affected rows,

$q = $dbc -> prepare("SELECT COUNT(*) FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();

Doing some research I found that you can also get the row count using other methods of count and not using count at all, for example the following query is supposed be the same as above but will return correct for PDO,

$q = $dbc -> prepare("SELECT 1 FROM table WHERE id = ?");
$q -> execute(array($id));
echo $q -> rowCount();

There are various sources on the internet claiming that;

"SELECT COUNT(*)
"SELECT COUNT(col)
"SELECT 1

Are all the same as each other (with a few differences) so how come using mysql which PDO cannot properly return a true count, does

"SELECT 1 

work?

Methods of count discussion

Why is Select 1 faster than Select count(*)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇女薄笑 2024-12-24 11:46:30

PDO 不支持 COUNT(*)

WTF?当然PDO支持COUNT(*),你使用它的方式是错误的。

$q = $dbc->prepare("SELECT COUNT(id) as records FROM table WHERE id = ?");
$q->execute(array($id));    
$records = (int) $q->fetch(PDO::FETCH_OBJ)->records;

如果您使用的是 MySQL 以外的驱动程序,则可能必须首先测试 rowCount,如下所示。

$records = (int) ($q->rowCount()) ? $q->fetch(PDO::FETCH_OBJ)->records : 0;

PDO does not support COUNT(*)

WTF? Of course PDO supports COUNT(*), you are using it the wrong way.

$q = $dbc->prepare("SELECT COUNT(id) as records FROM table WHERE id = ?");
$q->execute(array($id));    
$records = (int) $q->fetch(PDO::FETCH_OBJ)->records;

If you are using a driver other than MySQL, you might have to test rowCount first, like this.

$records = (int) ($q->rowCount()) ? $q->fetch(PDO::FETCH_OBJ)->records : 0;
你在看孤独的风景 2024-12-24 11:46:30

哦。你把一切都搞混了。

  1. PDO 不干扰 SQL 查询。它支持 SQL 支持的一切。
  2. 在执行 COUNT(*) 时,您根本不应该使用 rowcount,因为它没有任何意义。您必须检索查询结果。
  3. 不知道您在谈论什么“各种来源”,但是 COUNT(*)COUNT(col) (甚至 COUNT(1))是当您本身不需要记录时,这是相同且获取记录计数的唯一正确方法

COUNT 是一个聚合函数,它为您计算行数。因此,它已经返回结果,不再需要计数。它仅返回单行中的标量值。因此,在这一行上使用 rowcount 没有任何意义

SELECT 1 与上面不同,因为它只选择文字 1 对于表中找到的每一行。因此,如果数据库中有数千行,它将返回一千个1。因此,行计数将为您提供结果,但这将极大地浪费服务器资源。

有一个简单的规则需要遵循:

始终只请求您需要的数据。

如果您需要行数 - 请求行数。没有一千个 1 供以后数它们。
听起来有道理吗?

Oh. You are confusing everything.

  1. PDO do not interfere with SQL queries. It support EVERYTHING supported by SQL.
  2. When doing COUNT(*) you shouldn't use rowcount at all, as it just makes no sense. You have to retreive the query result instead.
  3. Dunno what "various sources" you are talking about but COUNT(*) and COUNT(col) (and even COUNT(1)) are the same and the only proper way to get count of records when you need no records themselves.

COUNT is an aggregate function, it counts rows for you. So, it returns the result already, no more counting required. Ad it returns just a scalar value in the single row. Thus, using rowcount on this single row makes no sense

SELECT 1 is not the same as above, as it selects just literal 1 for the every row found in the table. So, it will return a thousand 1s if there is a thousands rows in your database. So, rowcount will give you the result but it is going to be an extreme waste of the server resources.

there is a simple rule to follow:

Always request the only data you need.

If you need the count of rows - request count of rows. Not a thousand of 1s to count them later.
Sounds sensible?

陌若浮生 2024-12-24 11:46:30

我认为测试数据库中是否存在一行的最佳方法是执行。

SELECT 1 FROM table WHERE condition LIMIT 1

如果它找到一行,它会停下来并告诉您有一行。如果没有,并且您的 where 子句列上有索引,它也会非常快地看到没有可用的索引。

Best way I think to test if a line exist in your database is to perform.

SELECT 1 FROM table WHERE condition LIMIT 1

If it find a row it will stop and tell you there is a line. If it don't and you have an index on your where clause column it will also goes very fast to see there are none available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文