專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)岳陽免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
下文給大家?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è)資訊板塊。