Mysql计数vs mysql SELECT,哪一个更快? [英] Mysql count vs mysql SELECT, which one is faster?
问题描述
如果我想检查一个名称,我想查看在users表下的username列中有多少行/名称。我想说的是数千...我应该使用:
-
count(name),
li>
-
count(*)或
-
SELECT username FROM users where username ='name'/ li>
哪一个更合适?
感谢各位,我找到了
<$ c $ c> SELECT COUNT(username)
FROM users
WHERE`username` ='tim'
COUNT(*)
和 COUNT(Name)
产生不同的值。 COUNT
不会包含NULL值,因此如果有 Name 的任何实例等于NULL,则不会计入。
COUNT(*)
也会优于 Count(Name)
。通过指定 COUNT(*)
,您将使优化程序可以随意使用其所需的任何索引。通过指定 COUNT(Name)
,您将强制查询引擎使用该表,或至少一个包含NAME列的索引。
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(*) or
SELECT username FROM users where username = 'name'
Which one is the more appropriate? Or they will give same result in term of speed/response?
EDIT:
Thanks guys, I found the answer, count() will definitely faster
Is this query correct
SELECT COUNT( username )
FROM users
WHERE `username` = 'tim'
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(*)
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,哪一个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!