溫馨提示×

溫馨提示×

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

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

使用前綴索引對MySQL優(yōu)化的方法

發(fā)布時(shí)間:2020-05-27 15:54:16 來源:網(wǎng)絡(luò) 閱讀:373 作者:三月 欄目:MySQL數(shù)據(jù)庫

下文給大家?guī)碛嘘P(guān)使用前綴索引對MySQL優(yōu)化的方法內(nèi)容,相信大家一定看過類似使用前綴索引對MySQL優(yōu)化的方法的文章。我們給大家?guī)淼挠泻尾煌??一起來看看正文部分吧,相信看完你一定?huì)有所收獲。


1.查看表結(jié)構(gòu)

(root@localhost) [prod_db]> show create table t_file_info\G; *************************** 1. row ***************************       Table: t_file_info Create Table: CREATE TABLE `t_file_info` (  `id` varchar(36) NOT NULL DEFAULT '',  `devid` varchar(64) DEFAULT NULL,  `areaid` int(11) DEFAULT NULL,  `fileid` varchar(256) NOT NULL,  `filename` varchar(256) DEFAULT NULL,  `filesize` int(11) DEFAULT NULL,  `filemd5` varchar(40) DEFAULT NULL,  `extend` varchar(4000) DEFAULT NULL,  `status` int(11) NOT NULL DEFAULT '0',  `createdate` datetime DEFAULT NULL,  `fileurl` varchar(256) DEFAULT NULL,  `businessid` bigint(20) NOT NULL DEFAULT '0',  PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.fileid是我們查詢的一個(gè)條件,正常是需要?jiǎng)?chuàng)建索引的。

 select char_length('63f2a078018649ca9948f5469550bf2a/group1/M00/00/DA/wKgj2FcMquGAVuJcAAAI4FL7ZCA388.jpg') ;
+-----------------------------------------------------------------------------------------------------+
| char_length('63f2a078018649ca9948f5469550bf2a/group1/M00/00/DA/wKgj2FcMquGAVuJcAAAI4FL7ZCA388.jpg') |
+-----------------------------------------------------------------------------------------------------+
|                                                                                                  84 |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
--經(jīng)過和開發(fā)溝通了解,前32位相當(dāng)于uuid可以確定唯一值。

3.這樣的字段,我們怎么創(chuàng)建索引,是不是有規(guī)律可循。繼續(xù)查看

--查看選擇率
select count(distinct(fileid))/count(*) AS Selectivity from t_file_info;

select count(distinct left(fileid,32))/count(*) from t_file_info;

(root@localhost) [prod_db]> select count(distinct(fileid))/count(*) from t_file_info;
+----------------------------------+
| count(distinct(fileid))/count(*) |
+----------------------------------+
|                           1.0000 |
+----------------------------------+
1 row in set (0.17 sec)

(root@localhost) [prod_db]> select count(distinct left(fileid,32))/count(*) from t_file_info;
+------------------------------------------+
| count(distinct left(fileid,32))/count(*) |
+------------------------------------------+
|                                   0.9999 |
+------------------------------------------+
1和0.9999幾乎可以等同,其實(shí)這里因?yàn)辄c(diǎn)特殊情況,正常應(yīng)該都是1才對的。

4.查看無索引的執(zhí)行計(jì)劃

explain select id,fileid from prod_db.t_file_info where fileid='63f2a078018649ca9948f5469550bf2a/group1/M00/00/DA/wKgj2FcMquGAVuJcAAAI4FL7ZCA388.jpg';

(root@localhost) [prod_db]> explain select id,fileid from prod_db.t_file_info where fileid='63f2a078018649ca9948f5469550bf2a/group1/M00/00/DA/wKgj2FcMquGAVuJcAAAI4FL7ZCA388.jpg';
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows  | Extra       |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
|  1 | SIMPLE      | t_file_info | ALL  | NULL          | NULL | NULL    | NULL | 35109 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.00 sec)

5.創(chuàng)建前綴索引,查看執(zhí)行計(jì)劃

alter table `prod_db`.`t_file_info` add index idx_t_file_info_fileid(fileid(32));

(root@localhost) [prod_db]> explain select id,fileid from prod_db.t_file_info where fileid='63f2a078018649ca9948f5469550bf2a/group1/M00/00/DA/wKgj2FcMquGAVuJcAAAI4FL7ZCA388.jpg';
+----+-------------+-------------+------+------------------------+------------------------+---------+-------+------+-------------+
| id | select_type | table       | type | possible_keys          | key                    | key_len | ref   | rows | Extra       |
+----+-------------+-------------+------+------------------------+------------------------+---------+-------+------+-------------+
|  1 | SIMPLE      | t_file_info | ref  | idx_t_file_info_fileid | idx_t_file_info_fileid | 98      | const |    1 | Using where |
+----+-------------+-------------+------+
--返回1行才是我們想看到的

6.創(chuàng)建索引

(root@localhost) [prod_db]> alter table `prod_db`.`t_file_info` add index idx_t_file_info_fileid(fileid(32));
Query OK, 0 rows affected (5 min 36.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

創(chuàng)建索引觀察系統(tǒng)資源使用情況,內(nèi)存機(jī)會(huì)沒有變化,但是CPU單核幾乎跑滿

(root@localhost) [prod_db]> select count(fileid) from t_file_info;
+---------------+
| count(fileid) |
+---------------+
|      12299419 |
+---------------+
1 row in set (14.94 sec) --千萬行

小結(jié):
1.了解前綴索引的實(shí)用場景。
2.要和開發(fā)溝通,了解業(yè)務(wù),才能創(chuàng)建最合適的索引。
3.創(chuàng)建索引對系統(tǒng)性能會(huì)有很大的影響,要選擇一個(gè)合適的時(shí)間點(diǎn)去創(chuàng)建,評估好影響。任何事情不要想當(dāng)然,當(dāng)你沒經(jīng)驗(yàn),還想當(dāng)然的時(shí)候很容易出問題。

對于上文關(guān)于使用前綴索引對MySQL優(yōu)化的方法,大家覺得是自己想要的嗎?如果想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

 

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI