首页 > 数据库 > MySQL count(*) 与 count(col) 查询效率比较

MySQL count(*) 与 count(col) 查询效率比较

2009年03月31号
查看评论 发表评论 1,561次浏览

优化总结:

1.任何情况下SELECT COUNT(*) FROM xxx 是最优选择;
2.尽量减少SELECT COUNT(*) FROM xxx WHERE COL = ‘xxx’ 这种查询;
3.杜绝SELECT COUNT(COL) FROM tablename WHERE COL = ‘xxx’ 的出现。(其中COL非主键)

环境:
MySQL版本:5.0.45
OS:Windows XP SP3

数据表一:sphinx
+—-+——+–+–+—+——+
| Field | Type | Null | Key | Default | Extra |
+—-+——+–+–+—+——+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| til | varchar(100) | NO | | | |
| content | text | NO | | | |
| dataline | int(11) | NO | | | |
+—-+——+–+–+—+——+

记录数:1120100

查询一:

mysql> select count(*) as totalnum from sphinx;
+—-+
| totalnum |
+—-+
| 1120100 |
+—-+
1 row in set (0.00 sec)

查询二:

mysql> select count(*) as totalnum from sphinx where id>1000;
+—-+
| totalnum |
+—-+
| 1119100 |
+—-+
1 row in set (2.17 sec)

查询三:

mysql> select count(*) as totalnum from sphinx where id>1000;
+—-+
| totalnum |
+—-+
| 1119100 |
+—-+
1 row in set (0.61 sec)

查询四:

mysql> select count(*) as totalnum from sphinx where id>1000;
+—-+
| totalnum |
+—-+
| 1119100 |
+—-+
1 row in set (0.61 sec)

查询五:

mysql> select count(id) as totalnum from sphinx;
+—-+
| totalnum |
+—-+
| 1120100 |
+—-+
1 row in set (0.00 sec)

查询六:

mysql> select count(til) as totalnum from sphinx where id>1000;
+—-+
| totalnum |
+—-+
| 1119100 |
+—-+
1 row in set (1 min 38.61 sec)

查询七:

mysql> select count(id) as totalnum from sphinx where id>11000;
+—-+
| totalnum |
+—-+
| 1109100 |
+—-+
1 row in set (0.61 sec)

查询八:

mysql> select count(id) as totalnum from sphinx;
+—-+
| totalnum |
+—-+
| 1120100 |
+—-+
1 row in set (0.03 sec)

结论:

在 select count() 没有 where 条件的时候 select count(*) 和 select count(col) 所消耗的查询时间相差无几。
在 select count() 有 where 条件的时候 select count(col) 所消耗的查询时间 比 select count(*) 明显多出数量级的时间。

类别数据库 标签
  1. admin
    发表于 2009年03月31号 09时35分21秒 | 1楼

    一直以为select count(col) 比 select count(*) 好,晕了!

  2. Bzvkhrsi
    发表于 2009年06月24号 03时57分05秒 | 2楼

    y7Ll37 comment6 ,

  3. miu miu bag
    发表于 2010年06月16号 00时17分24秒 | 3楼

    wow powerful!

  4. mbt chapa shoes
    发表于 2010年06月20号 05时33分36秒 | 4楼

    What an inspiration you are to us all.

  5. air max sneakers
    发表于 2010年06月25号 05时23分48秒 | 5楼

    All I can say is WOW!

  6. five fingers vibram
    发表于 2010年06月27号 01时39分36秒 | 6楼

    Good points

  7. buy air max
    发表于 2010年06月30号 10时38分15秒 | 7楼

    Lovely thoughts!! My sentiments exactly!

  8. nike air max 2010
    发表于 2010年06月30号 13时51分33秒 | 8楼

    Thank you my friend, you brought a tear to my eye.

  9. miu miu handbag sale
    发表于 2010年07月10号 13时53分54秒 | 9楼

    Lovely Story.. More important are the comments.. so beautifully interpreted !!! God Bless You All !!

  10. nike air force one low
    发表于 2010年07月11号 04时08分08秒 | 10楼

    Thank you, enjoyed the stories, your comments and pictures very, very much.

  11. mbt kisumu sandals
    发表于 2010年07月19号 14时33分37秒 | 11楼

    Its really weird.

  12. Ralph Lauren Bikini
    发表于 2010年07月19号 20时59分04秒 | 12楼

    i like your attitude. i agree, it is so stylish just to be yourself.

  13. lacoste men shoes
    发表于 2010年07月20号 18时49分59秒 | 13楼

    Reading that quote makes me reflect that a magic moment is not only a moment in a day.

  14. kobe iv
    发表于 2010年07月21号 12时50分49秒 | 14楼

    I love your imagination

  15. lacoste Shoes
    发表于 2010年07月24号 21时28分16秒 | 15楼

    Huh, okay )) Special

评论页数:
1 2 109
  1. 目前没有通告
你必需 登陆 才能发表评论.