国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql中count(id) count(1) count(*)的用法區別

2024-07-24 12:37:06
字體:
來源:轉載
供稿:網友

在mysql中很多朋友在寫統計count時每次可能都不一樣如,count(id) count(1) count(*)這三個統計出來的結果是一樣的,但它們之間的性能有比較過嗎?下面我來給大家舉例說明一下.

表結構如下,代碼如下:

  1. mysql> show create table userG; 
  2. *************************** 1. row *************************** 
  3.        Tableuser 
  4. Create TableCREATE TABLE `user` ( 
  5.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
  6.   `namevarchar(50) NOT NULL
  7.   `pwd` varchar(50) NOT NULL
  8.   `email` varchar(100) NOT NULL
  9.   `phone` varchar(30) NOT NULL
  10.   `sex` enum('F','M','N'NOT NULL DEFAULT 'N'
  11.   `addres` varchar(100) NOT NULL
  12.   `tag` varchar(100) NOT NULL
  13.   PRIMARY KEY (`id`), 
  14.   KEY `name` (`name`) 
  15. ) ENGINE=InnoDB AUTO_INCREMENT=5000003 DEFAULT CHARSET=utf8 COMMENT='用戶表' 
  16. 1 row in set (0.00 sec) 

下面做一下explain,1、count(id),代碼如下:

  1. mysql> select count(id) from user
  2. +-----------+ 
  3. count(id) | 
  4. +-----------+ 
  5. |   5000002 | 
  6. +-----------+ 
  7. 1 row in set (1.93 sec)mysql> explain select count(id) from user
  8. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows    | Extra       |+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  9. |  1 | SIMPLE      | user  | index | NULL          | name | 152     | NULL | 4998401 | Using index |+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+1 row in set (0.05 sec)2、count(1)  
  10. mysql> select count(1) from user
  11. +----------+ 
  12. count(1) | 
  13. +----------+ 
  14. |  5000002 | 
  15. +----------+ 
  16. 1 row in set (0.90 sec)mysql> explain select count(1) from user
  17. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  18. | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows    | Extra       | 
  19. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  20. |  1 | SIMPLE      | user  | index | NULL          | name | 152     | NULL | 4998401 | Using index | 
  21. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  22. 1 row in set (0.00 sec)3、count(*)  
  23. mysql> select count(*) from user
  24. +----------+ 
  25. count(*) | 
  26. +----------+ 
  27. |  5000002 | 
  28. +----------+ 
  29. 1 row in set (0.87 sec)mysql> explain select count(*) from user
  30. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  31. | id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows    | Extra       | 
  32. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  33. |  1 | SIMPLE      | user  | index | NULL          | name | 152     | NULL | 4998401 | Using index | 
  34. +----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+ 
  35. 1 row in set (0.00 sec) 

比較三個查詢,explain的結果一模一樣,這說明這三個的效率是一樣的嗎?再看看下面三個操作,帶上where條件 sex='F',以下三個操作中間均會重啟mysql服務,代碼如下:

  1. 1、count(id)  
  2. mysql> select count(id) from user where sex='F'
  3. +-----------+ 
  4. count(id) | 
  5. +-----------+ 
  6. |   1681259 | 
  7. +-----------+ 
  8. 1 row in set (18.87 sec) 
  9. mysql> explain select count(id) from user where sex='F'
  10. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  11. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra       | 
  12. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  13. |  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL | 4998401 | Using where | 
  14. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  15. 1 row in set (0.00 sec)2、count(1)  
  16. mysql> select count(1) from user where sex='F'
  17. +----------+ 
  18. count(1) | 
  19. +----------+ 
  20. |  1681259 | 
  21. +----------+ 
  22. 1 row in set (4.81 sec) 
  23. mysql> explain select count(1) from user where sex='F'
  24. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  25. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra       | 
  26. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  27. |  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL | 4998401 | Using where | 
  28. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  29. 1 row in set (0.00 sec)3、count(*)  
  30. mysql> select count(*) from user where sex='F'
  31. +----------+ 
  32. count(*) | 
  33. +----------+ 
  34. |  1681259 | 
  35. +----------+ 
  36. 1 row in set (4.69 sec) 
  37. mysql> explain select count(*) from user where sex='F'
  38. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  39. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra       | 
  40. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+   //Vevb.com 
  41. |  1 | SIMPLE      | user  | ALL  | NULL          | NULL | NULL    | NULL | 4998401 | Using where | 
  42. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+ 
  43. 1 row in set (0.00 sec) 

以上三種查詢有一些差別,其中count(id)用時最長,count(*)比count(1)速度要稍微快一點.

兩組查詢,帶條件的都沒有使用到索引,掃描了全表,而沒有條件的則使用了索引name,所以在應用中盡量不使用count(*)和count(1),杜絕使用count(primary_key).

網上有很多資料說:

沒有主鍵,count(1)比count(*)快;

有主鍵的話,count(primary_key)最快,但是在上面的測試中發現,count(primary_key)是最慢的,難道是測試不準確?這個有待驗證。

如果表只有一個字段,則count(*)是最快的.

說明:

count(1)中的1并不是指第一個column;

count(*)和count(1)一樣,包括對值為NULL的統計;

count(column)不包括對值為NULL的統計,這里的column指的不是primary_key;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 喀喇| 乌恰县| 资兴市| 芦山县| 阿鲁科尔沁旗| 五大连池市| 藁城市| 泸定县| 华安县| 资源县| 军事| 民勤县| 利津县| 高唐县| 富阳市| 龙南县| 济宁市| 禹城市| 黄骅市| 普兰县| 吕梁市| 通海县| 藁城市| 酉阳| 兴安盟| 栾城县| 扎囊县| 会理县| 随州市| 百色市| 台安县| 永和县| 泾阳县| 台中县| 镇原县| 盐山县| 永嘉县| 农安县| 理塘县| 济宁市| 乌兰察布市|