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

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL count 之索引挑選

2024-07-24 12:34:52
字體:
供稿:網(wǎng)友
  覆蓋索引對于一些統(tǒng)計問題,如下:
 
  MySQL > show create table test1 /G
  *************************** 1. row ***************************
         Table: test1
  Create Table: CREATE TABLE `test1` (
    `id` bigint(16) NOT NULL AUTO_INCREMENT,
    `order_seq` bigint(16) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_id` (`id`),
    KEY `idx_id_ordseq` (`id`,`order_seq`)
  ) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4
  1 row in set (0.00 sec)
  MySQL > explain select count(*) from test1 where id>10000 and id<20000;
  +----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
  | id | select_type | table | partitions | type  | possible_keys                | key    | key_len | ref  | rows | filtered | Extra                    |
  +----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
  |  1 | SIMPLE      | test1 | NULL       | range | PRIMARY,idx_id,idx_id_ordseq | idx_id | 8       | NULL | 9999 |   100.00 | Using where; Using index |
  +----+-------------+-------+------------+-------+------------------------------+--------+---------+------+------+----------+--------------------------+
  1 row in set, 1 warning (0.00 sec)
  innodb存儲引擎選擇了id字段的輔助索引,而不是聚集索引來統(tǒng)計,更不是聯(lián)合索引。原因是輔助索引遠小于聚集索引,選擇輔助索引可以減少IO資源消耗。
 
  而另外一個統(tǒng)計場景:
 
  select count(*) from test1 where order_seq > 1502131212577 and order_seq< 202007080947244761;
 
  test1表建有id和 order_seq 字段的聯(lián)合索引。
 
  MySQL > show create table test1 /G
  *************************** 1. row ***************************
         Table: test1
  Create Table: CREATE TABLE `test1` (
    `id` bigint(16) NOT NULL AUTO_INCREMENT,
    `order_seq` bigint(16) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_id` (`id`),
    KEY `idx_id_ordseq` (`id`,`order_seq`)
  ) ENGINE=InnoDB AUTO_INCREMENT=15002212 DEFAULT CHARSET=utf8mb4
  1 row in set (0.00 sec)
  MySQL > explain select count(*) from test1 where order_seq > 1502131212577 and order_seq< 202007080947244761;
  +----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
  | id | select_type | table | partitions | type  | possible_keys | key           | key_len | ref  | rows     | filtered | Extra                    |
  +----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
  |  1 | SIMPLE      | test1 | NULL       | index | NULL          | idx_id_ordseq | 16      | NULL | 15068082 |    11.11 | Using where; Using index |
  +----+-------------+-------+------------+-------+---------------+---------------+---------+------+----------+----------+--------------------------+
  1 row in set, 1 warning (0.00 sec)
  這里使用條件 order_seq 查詢,一般情況下使用不了聯(lián)合索引的,但是這個案例中的查詢,利用到覆蓋索引的信息。possible_keys依然為null,但是key是idx_id_ordseq,extra里出現(xiàn)Using index,表示為覆蓋索引。

(編輯:武林網(wǎng))

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平利县| 乐安县| 东辽县| 靖远县| 鸡东县| 卫辉市| 洪洞县| 叙永县| 南岸区| 北碚区| 余江县| 海兴县| 曲靖市| 临安市| 台北市| 三门县| 威宁| 遂平县| 大港区| 蕲春县| 泾川县| 陕西省| 东辽县| 稷山县| 中卫市| 濉溪县| 营山县| 元谋县| 洪湖市| 元氏县| 象州县| 杂多县| 荔浦县| 新邵县| 肃北| 大宁县| 韶关市| 工布江达县| 五家渠市| 会东县| 江城|