Mysql count vs mysql SELECT, which one is faster?(mysql 计数 vs mysql SELECT,哪个更快?)
问题描述
如果我想检查一个名字,我想看看在用户表下的用户名"列中存在多少行/名字.让我们说数千...数十万,我应该使用:
If I want to do a check a name, I want to see how many rows/name exists in the "username" column under users table. Lets say thousands ... hundred of thousands, should I use:
计数(名称),
count(name),
count(*) 或
count(*) or
SELECT username FROM users where username = 'name'
SELECT username FROM users where username = 'name'
哪个更合适?或者他们会在速度/响应方面给出相同的结果?
Which one is the more appropriate? Or they will give same result in term of speed/response?
谢谢大家,我找到了答案,count() 肯定会更快
Thanks guys, I found the answer, count() will definitely faster
这个查询是否正确
SELECT COUNT( username )
FROM users
WHERE `username` = 'tim'
推荐答案
COUNT(*) 和 COUNT(Name) 可能会产生不同的值.COUNT 将不包括 NULL 值,因此如果 Name 的任何实例等于 NULL,它们将不会被计算在内.
COUNT(*) and COUNT(Name) might produce different values. COUNT will not include NULL values, so if there are any instances of Name that equal NULL they will not be counted.
COUNT(*) 也会比 Count(Name) 表现得更好.通过指定 COUNT(*),您可以让优化器自由使用它希望使用的任何索引.通过指定 COUNT(Name),您将强制查询引擎使用该表,或至少使用包含 NAME 列的索引.
COUNT(*) will also perform better than Count(Name). By specifying COUNT(*) you are leaving the optimizer free to use any index it wishes. By specifying COUNT(Name) you are forcing the query engine to use the table, or at least an index that contains the NAME column.
这篇关于mysql 计数 vs mysql SELECT,哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysql 计数 vs mysql SELECT,哪个更快?
基础教程推荐
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
