COUNT(id) vs. COUNT(*) in MySQL(MySQL 中的 COUNT(id) 与 COUNT(*))
问题描述
假设表中有一个主要字段id"(如速度等),以下查询之间是否存在差异?
Is there a difference between the following queries, assuming there is a primary field 'id' in the table (as in speed, etc)?
SELECT COUNT(id)
FROM table
对比
SELECT COUNT(*)
FROM table
推荐答案
我知道问题是关于 MySQL,但就其价值而言,建议将 count(*) 用于 Oracle:这表明这是特定于数据库的(请参阅上面来自 BalusC 的评论).由于许多数据库(MS-SQL、MySQL)都有保存各种类型元数据的信息模式表,如果一种语法只是查找一个容易获得的值,而另一种则直接访问该表,那么肯定会有差异.在一天结束时:尝试不同的选项,看看 EXPLAIN 告诉你在幕后发生了什么.
I know the question is about MySQL, but for what it's worth, count(*) is recommended for Oracle: which goes to show that this is database specific (see comment above from BalusC). Since a lot of databases (MS-SQL, MySQL) have information schema tables that hold various types of metadata, there are bound to be differences if one syntax is simply looking up a readily-available value, and another is going straight to the table. At the end of day: try different options, an see what EXPLAIN is telling you is going on behind the scenes.
这篇关于MySQL 中的 COUNT(id) 与 COUNT(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 中的 COUNT(id) 与 COUNT(*)
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
