中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL優化之聯合索引

發布時間:2020-07-14 09:16:15 來源:網絡 閱讀:740 作者:roidba 欄目:MySQL數據庫

1.表結構

(root@localhost) [test]> show create table t_demo\G;
*************************** 1. row ***************************
       Table: t_demo
Create Table: CREATE TABLE `t_demo` (
  `vin` varchar(17) NOT NULL DEFAULT '' COMMENT '底盤號',
  `lng` decimal(10,6) DEFAULT NULL COMMENT 'GPS經度',
  `lat` decimal(10,6) DEFAULT NULL COMMENT 'GPS緯度',
  `gps_time` datetime DEFAULT NULL,
  `crmkey` char(36) DEFAULT NULL ,
  `veh_model_desc` varchar(30) DEFAULT NULL ,
  `veh_series_desc` varchar(30) DEFAULT NULL ,
  `update_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' ,
  PRIMARY KEY (`vin`,`update_time`),
  KEY `idx_crmkey` (`crmkey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 
/*!50100 PARTITION BY RANGE (to_days(update_time))
(PARTITION p20160724 VALUES LESS THAN (736535) ENGINE = InnoDB,
 PARTITION p20160725_0731 VALUES LESS THAN (736542) ENGINE = InnoDB,
 PARTITION p20160801_0807 VALUES LESS THAN (736549) ENGINE = InnoDB,
 PARTITION p20160808_0814 VALUES LESS THAN (736556) ENGINE = InnoDB,
 PARTITION p20160815_0821 VALUES LESS THAN (736563) ENGINE = InnoDB,
 PARTITION p20160822_0828 VALUES LESS THAN (736570) ENGINE = InnoDB,
 PARTITION p20160829_0904 VALUES LESS THAN (736577) ENGINE = InnoDB,
 PARTITION p20160905_0911 VALUES LESS THAN (736584) ENGINE = InnoDB,
 PARTITION p20160912_0918 VALUES LESS THAN (736591) ENGINE = InnoDB,
 PARTITION p20160919_0925 VALUES LESS THAN (736598) ENGINE = InnoDB,
 PARTITION p20160926_1002 VALUES LESS THAN (736605) ENGINE = InnoDB,
 PARTITION p20161003_1009 VALUES LESS THAN (736612) ENGINE = InnoDB,
 PARTITION p20161010_1016 VALUES LESS THAN (736619) ENGINE = InnoDB,
 PARTITION p20161017_1023 VALUES LESS THAN (736626) ENGINE = InnoDB,
 PARTITION p20161024_1030 VALUES LESS THAN (736633) ENGINE = InnoDB,
 PARTITION p20161031_1106 VALUES LESS THAN (736640) ENGINE = InnoDB,
 PARTITION p20161107_1113 VALUES LESS THAN (736647) ENGINE = InnoDB,
 PARTITION p20161114_1120 VALUES LESS THAN (736654) ENGINE = InnoDB,
 PARTITION p20161121_1127 VALUES LESS THAN (736661) ENGINE = InnoDB,
 PARTITION p20161128_1204 VALUES LESS THAN (736668) ENGINE = InnoDB,
 PARTITION p20161205_1211 VALUES LESS THAN (736675) ENGINE = InnoDB,
 PARTITION p20161212_1218 VALUES LESS THAN (736682) ENGINE = InnoDB,
 PARTITION p20161219_1225 VALUES LESS THAN (736789) ENGINE = InnoDB,
 PARTITION p20161226_170101 VALUES LESS THAN (736796) ENGINE = InnoDB) */
1 row in set (0.00 sec)

2.查看執行計劃

(root@localhost) [test]> explain partitions SELECT COUNT(1)
    -> FROM t_demo tvghg
    -> WHERE tvghg.crmkey ='cf71ea00-65ff-4521-b336-fdc13846e2e2'
    ->   AND tvghg.update_time >=  '2016-07-19 06:00:00' 
    ->  AND tvghg.update_time < '2016-07-19 07:00:00' 
    -> AND (tvghg.veh_series_desc IN ( 'A6', 'A6L'));
+----+-------------+-------+------------+------+---------------+------------+---------+-------+---------+-------------+
| id | select_type | table | partitions | type | possible_keys | key        | key_len | ref   | rows    | Extra       |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+---------+-------------+
|  1 | SIMPLE      | tvghg | p20160724  | ref  | idx_crmkey    | idx_crmkey | 109     | const | 1961366 | Using where |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+---------+-------------+
1 row in set (0.00 sec)

3.查看選擇率

(root@localhost) [test]> select count(distinct(crmkey))/count(*) AS selectivity1,count(distinct(update_time))/count(*) AS selectivity2, count(distinct(veh_series_desc))/count(*) AS selectivity3 from t_demo;
+--------------+--------------+--------------+
| selectivity1 | selectivity2 | selectivity3 |
+--------------+--------------+--------------+
|       0.0001 |       0.0000 |       0.0000 |
+--------------+--------------+--------------+
1 row in set (43.95 sec)

select count(distinct(concat(crmkey,update_time)))/count(*) from t_demo;
+------------------------------------------------------+
| count(distinct(concat(crmkey,update_time)))/count(*) |
+------------------------------------------------------+
|                                               0.0136 |
+------------------------------------------------------+
1 row in set (52.66 sec)
4.查詢時間
(root@localhost) [test]> SELECT COUNT(1)
    -> FROM t_demo tvghg
    -> WHERE tvghg.crmkey ='cf71ea00-65ff-4521-b336-fdc13846e2e2'
    ->   AND tvghg.update_time >=  '2016-07-19 06:00:00' 
    ->  AND tvghg.update_time < '2016-07-19 07:00:00' 
    -> AND (tvghg.veh_series_desc IN ( 'A6', 'A6L'));
+----------+
| COUNT(1) |
+----------+
|     2695 |
+----------+
1 row in set (3.67 sec)

這里考慮索引怎么創建,涉及到多個字段,需要我們去做判斷,查看選擇率是我們創建索引的一個很重要的參考。這里的表是一個分區表,按照時間做的分區,查詢字段里也包含時間字段,索引創建索引我們肯定要有時間字段的哦。

5.創建索引

(root@localhost) [(none)]> create index idx_tvghg_crmkey_update_time on `test`.`t_demo`(crmkey,update_time);
Query OK, 0 rows affected (4 min 40.21 sec)
Records: 0  Duplicates: 0  Warnings: 0
(root@localhost) [test]> alter table `test`.`t_demo` drop index idx_crmkey;
Query OK, 0 rows affected (1.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

6.查詢,查看執行計劃

SELECT COUNT(1)
     FROM t_demo tvghg
     WHERE tvghg.crmkey ='cf71ea00-65ff-4521-b336-fdc13846e2e2'
       AND tvghg.update_time >=  '2016-07-19 06:00:00' 
      AND tvghg.update_time < '2016-07-19 07:00:00' 
     AND (tvghg.veh_series_desc IN ( 'A6', 'A6L'));
+----------+
| COUNT(1) |
+----------+
|     2695 |
+----------+
1 row in set (0.03 sec)

(root@localhost) [test]> explain partitions SELECT COUNT(1)
    ->      FROM t_demo tvghg
    ->      WHERE tvghg.crmkey ='cf71ea00-65ff-4521-b336-fdc13846e2e2'
    ->        AND tvghg.update_time >=  '2016-07-19 06:00:00' 
    ->       AND tvghg.update_time < '2016-07-19 07:00:00' 
    ->      AND (tvghg.veh_series_desc IN ( 'A6', 'A6L'));
+----+-------------+-------+------------+-------+------------------------------+------------------------------+---------+------+-------+-------------+
| id | select_type | table | partitions | type  | possible_keys                | key                          | key_len | ref  | rows  | Extra       |
+----+-------------+-------+------------+-------+------------------------------+------------------------------+---------+------+-------+-------------+
|  1 | SIMPLE      | tvghg | p20160724  | range | idx_tvghg_crmkey_update_time | idx_tvghg_crmkey_update_time | 114     | NULL | 12446 | Using where |
+----+-------------+-------+------------+-------+------------------------------+------------------------------+---------+------+-------+-------------+

創建索引絕對是一個技術活,在盡量占用少的磁盤空間,創建出合理的索引,還是要多了解業務,知己知彼。在上線前評估好,真的上線了,創建索引對系統也會有不小的影響。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

嘉善县| 精河县| 常熟市| 岚皋县| 友谊县| 宣城市| 方城县| 泌阳县| 兰溪市| 阜新| 周至县| 宁明县| 平顶山市| 马公市| 全州县| 河间市| 冕宁县| 晋城| 合川市| 迁西县| 丹棱县| 襄汾县| 咸丰县| 离岛区| 灵璧县| 鹤山市| 德令哈市| 平定县| 大庆市| 漯河市| 遵义县| 马边| 张掖市| 安塞县| 乌什县| 拉孜县| 柳州市| 长泰县| 枝江市| 奈曼旗| 伽师县|