mysql count()的使用解析
檢視錶結構:
mysql> show create table coupon_use_test \G *************************** 1. row *************************** Table: coupon_use_test Create Table: CREATE TABLE `coupon_use_test` ( `id` int(11) NOT NULL DEFAULT '0', `user_id` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `coupon_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '', `status` varchar(2) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '00', `use_time` datetime DEFAULT NULL, `remark1` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `remark2` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `remark3` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `create_user_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, KEY `idx_create_time` (`create_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
檢視create_time欄位為空的行數
mysql> select * from coupon_use_test where create_time is null; Empty set (0.00 sec)
把id為1的記錄create_time改為空
mysql> update coupon_use_test set create_time = null where id = 1; Query OK, 1 row affected (6.56 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select count(*) from coupon_use_test where create_time is null; +----------+ | count(*) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec)
count(*)
mysql> select count(*) from coupon_use_test; +----------+ | count(*) | +----------+ | 1800000 | +----------+ 1 row in set (0.69 sec) mysql> explain select count(*) from coupon_use_test; +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | coupon_use_test | NULL | index | NULL | idx_create_time | 5 | NULL | 1771323 | 100.00 | Using index | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
可以看到count(*)走了create_time欄位的索引idx_create_time
count(1)
mysql> select count(1) from coupon_use_test; +----------+ | count(1) | +----------+ | 1800000 | +----------+ 1 row in set (0.63 sec) mysql> explain select count(1) from coupon_use_test; +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | coupon_use_test | NULL | index | NULL | idx_create_time | 5 | NULL | 1771323 | 100.00 | Using index | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
count(create_time)
mysql> select count(create_time) from coupon_use_test; +--------------------+ | count(create_time) | +--------------------+ | 1799999 | +--------------------+ 1 row in set (0.73 sec) mysql> explain select count(create_time) from coupon_use_test; +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ | 1 | SIMPLE | coupon_use_test | NULL | index | NULL | idx_create_time | 5 | NULL | 1771323 | 100.00 | Using index | +----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+---------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
count(*)和 count(1)可以查詢全表總行數, count(create_time)查詢到的行數不包括null。
count(1) 與 count(*) 比較 :
1> 如果資料表沒有主鍵,那麼 count(1) 比 count(*) 快
2> 如果有主鍵的話,那主鍵 (聯合主鍵) 作為 count條件也比 count(*) 要快
3> 如果你的表只有一個欄位的話那 count(*) 就是最快
在不加 WHERE 限制條件的情況下,COUNT(*) 與 COUNT(COL) 基本可以認為是等價的,但是在有 WHERE 限制條件的情況下,COUNT(*) 會比 COUNT(COL) 快非常多
COUNT(*) 通常是對主鍵進行索引掃描,而COUNT(COL)就不一定了,另外前者是統計表中的所有符合的紀錄總數,而後者是計算表中所有符合的COL的紀錄數
count(*) 與 count(1) 兩者比較,主要還是要取決於 count(1) 所相對應的資料欄位,如果count(1)是聚索引 id 那肯定是count(1)快,但是差的很小,因為 count(*) 自動會優化指定到那一個欄位,所以沒必要去count(?)用count(*)sql會自動完成優化
1> 任何情況下 SELECT COUNT(*) FROM tablename 是最優選擇
2> 儘量減少 SELECT COUNT(*) FROM tablename WHERE COL = 'value’ 這種查詢
3> 杜絕 SELECT COUNT(COL) FROM tablename 的出現
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30135314/viewspace-2654494/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL的COUNT語句--count(*)、 count(常量)、 count(列名)MySql
- 解析Count函式函式
- MySQL:count(*) count(欄位) 實現上區別MySql
- 圖解MySQL:count(*) 、count(1) 、count(主鍵欄位)、count(欄位)哪個效能最好?圖解MySql
- MySQL:SELECT COUNT 小結MySql
- 解析MySQL中INSERT INTO SELECT的使用MySql
- MySQL優化COUNT()查詢MySql優化
- mysql 大表中count() 使用方法以及效能優化.MySql優化
- laravel mysql聚合函式使用方法(count,sum,max,min,avg)LaravelMySql函式
- count(*)、count(1)和count(列名)的區別
- MySQL:COUNT(*) profile optimizing階段慢MySql
- 百萬資料 mysql count(*)優化MySql優化
- 有關mysql中ROW_COUNT()的小例子MySql
- MySQL裡的found_row()與row_count()MySql
- 【Numpy學習】np.count_nonzero()用法解析
- MySQL中count(*)函式原理詳解MySql函式
- MySQL 5.7 建立使用者報錯 ERROR 1805 (HY000): Column count of mysql.user is wrongMySqlError
- MySQL中的FOUND_ROWS()與ROW_COUNT()函式MySql函式
- 16、MySQL Case-索引key對select count(*)的影響MySql索引
- MYSQL count標量子查詢改left joinMySql
- Mysql報錯注入原理分析(count()、rand()、group by)MySql
- 深度解讀GaussDB(for MySQL)與MySQL的COUNT查詢並行最佳化策略MySql並行
- count (*) 和 count (1) 和 count (列名) 區別
- count(*) 和 count(1)和count(列名)區別
- SQL Server中count(*)和Count(1)的區別SQLServer
- MySQL統計總數就用count(*),別花裡胡哨的《死磕MySQL系列 十》MySql
- MySQL 複製全解析 Part 11 使用xtrabackup建立MySQL複製MySql
- MongoDB中如何使用統計count()方法?MongoDB
- MySQL的count(*)的優化,獲取千萬級資料表的總行數MySql優化
- node express 在使用mysql執行SELECT count(*) from xx獲取總數取值格式問題ExpressMySql
- mysql count函式與分頁功能極限優化MySql函式優化
- [20180727]再論count(*)和count(1).txt
- 7.65 COUNT
- 解析JDBC使用查詢MySQL【非流式、流式、遊標】JDBCMySql
- mysql count函式與分頁功能極限最佳化MySql函式
- C# 中List中的Count和Count(),有什麼區別C#
- mysql的使用MySql
- MySQL 的 crash-safe 原理解析MySql