北京SEO

mysql中count(1)与count(*)比较

2019/10/10/17:36:22  阅读:1989  来源:谷歌SEO算法  标签: 夫唯SEO视频教程

sql调优,主要是考虑降低:consistent gets和physical reads的数量.

count(1)与count(*)比较:

如果你的数据表没有主键,那么count(1)比count(*)快,如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快,如果你的表只有一个字段的话那count(*)就是最快的啦,count(*) count(1) 两者比较,主要还是要count(1)所相对应的数据字段.

如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的,因为count(*),自动会优化指定到那一个字段,所以没必要去count(?),用count(*),sql会帮你完成优化的.

count详解:count(*)将返回表格中所有存在的行的总数包括值为null的行,然而count(列名)将返回表格中除去null以外的所有行的总数,有默认值的列也会被计入.

distinct 列名,得到的结果将是除去值为null和重复数据后的结果

总结三条经验:

1.任何情况下SELECT COUNT(*) FROM tablename是最优选择;

2.尽量减少SELECT COUNT(*) FROM tablename WHERE COL = 'value’这种查询;

3.杜绝SELECT COUNT(COL) FROM tablename的出现。

国个找一文章不懂英文没译,代码如下:

  1. COUNT(*)vsCOUNT(col)
  2. LookingathowpeopleareusingCOUNT(*)andCOUNT(col)itlookslikemostofthemthinktheyaresynonymsandjustusingwhattheyhappentolike,whilethereissubstantialdifferenceinperformanceandevenqueryresult.
  3. Letslookatthefollowingseriesofexamples:
  4. CREATETABLE`fact`(
  5. `i`int(10)unsignedNOTNULL,
  6. `val`int(11)defaultNULL,
  7. `val2`int(10)unsignedNOTNULL,
  8. KEY`i`(`i`)
  9. )ENGINE=MyISAMDEFAULTCHARSET=latin1
  10. mysql>selectcount(*)fromfact;
  11. +———-+
  12. |count(*)|
  13. +———-+
  14. |7340032|
  15. +———-+
  16. 1rowinset(0.00sec)
  17. mysql>selectcount(val)fromfact;
  18. +————+
  19. |count(val)|
  20. +————+
  21. |7216582|
  22. +————+
  23. 1rowinset(1.17sec)
  24. mysql>selectcount(val2)fromfact;
  25. +————-+
  26. |count(val2)|
  27. +————-+
  28. |7340032|
  29. +————-+
  30. 1rowinset(0.00sec)

As this is MYISAM table MySQL has cached number of rows in this table. This is why it is able to instantly answer COUNT(*) and

COUNT(val2) queries, but not COUNT(val). Why ? Because val column is not defined as NOT NULL there can be some NULL values in it and so MySQL have to perform table scan to find out. This is also why result is different for the second query.

So COUNT(*) and COUNT(col) queries not only could have substantial performance performance differences but also ask different question.

MySQL Optimizer does good job in this case doing full table scan only if it is needed because column can be NULL.

Now lets try few more queries,代码如下:

  1. mysql>selectcount(*)fromfactwherei<10000;
  2. +———-+
  3. |count(*)|
  4. +———-+
  5. |733444|
  6. +———-+
  7. 1rowinset(0.40sec)
  8. mysql>explainselectcount(*)fromfactwherei<10000G
  9. ***************************1.row***************************
  10. id:1
  11. select_type:SIMPLE
  12. table:fact
  13. type:range
  14. possible_keys:i
  15. key:i
  16. key_len:4
  17. ref:NULL
  18. rows:691619
  19. Extra:Usingwhere;Usingindex
  20. 1rowinset(0.00sec)
  21. mysql>selectcount(val)fromfactwherei<10000;
  22. +————+
  23. |count(val)|
  24. +————+
  25. |720934|
  26. +————+
  27. 1rowinset(1.29sec)
  28. mysql>explainselectcount(val)fromfactwherei<10000G
  29. ***************************1.row***************************
  30. id:1
  31. select_type:SIMPLE
  32. table:fact
  33. type:range
  34. possible_keys:i
  35. key:i
  36. key_len:4
  37. ref:NULL
  38. rows:691619
  39. Extra:Usingwhere
  40. 1rowinset(0.00sec)
  41. mysql>selectcount(val2)fromfactwherei<10000;
  42. +————-+
  43. |count(val2)|
  44. +————-+
  45. |733444|
  46. +————-+
  47. 1rowinset(1.30sec)
  48. mysql>explainselectcount(val2)fromfactwherei<10000G
  49. ***************************1.row***************************
  50. id:1//phpfensi.com
  51. select_type:SIMPLE
  52. table:fact
  53. type:range
  54. possible_keys:i
  55. key:i
  56. key_len:4
  57. ref:NULL
  58. rows:691619
  59. Extra:Usingwhere
  60. 1rowinset(0.00sec)

As you can see even if you have where clause performance for count(*) and count(col) can be significantly different. In fact this example shows just 3 times performance difference because all data fits in memory, for IO bound workloads you frequently can see 10 and even 100 times performance difference in this case.

The thing is count(*) query can use covering index even while count(col) can’t. Of course you can extend index to be (i,val) and get query to be index covered again but I would use this workaround only if you can’t change the query (ie it is third party application) or in case column name is in the query for reason, and you really need count of non-NULL values.

It is worth to note in this case MySQL Optimizer does not do too good job optimizing the query. One could notice (val2) column is not null so count(val2) is same as count(*) and so the query could be run as index covered query. It does not and both queries have to perform row reads in this case.代码如下:

  1. mysql>altertablefactdropkeyi,addkey(i,val);
  2. QueryOK,7340032rowsaffected(37.15sec)
  3. Records:7340032Duplicates:0Warnings:0
  4. mysql>selectcount(val)fromfactwherei<10000;
  5. +————+
  6. |count(val)|
  7. +————+
  8. |720934|
  9. +————+
  10. 1rowinset(0.78sec)

广告内容

mysql中count(1)与count(*)比较 mysql中count(1)与count(*)比较 mysql中count(1)与count(*)比较

相关阅读

热门评论

Seven 绯闻SEO Seven 绯闻SEO

绯闻SEO,一个专注中小企业网站优化的SEO爱好者

总篇数179

精选文章

RMAN中catalog和nocatalog区别介绍 小技巧:为Linux下的文件分配多个权限 zimbra8.5.1安装第三方签名ssl证书的步骤 解决mysql不能远程连接数据库方法 windows服务器mysql增量备份批处理数据库 mysql中slow query log慢日志查询分析 JavaScript跨域问题总结 Linux下负载均衡软件LVS配置(VS/DR)教程 mysql中权限参数说明 MYSQL(错误1053)无法正常启动

SEO最新算法