您好,登錄后才能下訂單哦!
本篇內容主要講解“mysql中count()語句的用法介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“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 的出現
到此,相信大家對“mysql中count()語句的用法介紹”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。