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(*)
基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在多列上分布任意行 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
