网站建设步骤 优帮云,南县建设局网站,广州建设网站外包,做的网站 为什么百度搜不到目录
一.使用索引优化
数据准备
避免索引失效应用-全值匹配
避免索引失效应用-最左前缀法则 避免索引失效应用-其他匹配原则
1、
2、
3、
4、
5、 一.使用索引优化
索引是数据库优化最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数的MySQL的性能优化…目录
一.使用索引优化
数据准备
避免索引失效应用-全值匹配
避免索引失效应用-最左前缀法则 避免索引失效应用-其他匹配原则
1、
2、
3、
4、
5、 一.使用索引优化
索引是数据库优化最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数的MySQL的性能优化问题。
数据准备
use world;create table tb_seller(sellerid varchar(100),name varchar(100),nickname varchar(50),password varchar(60),status varchar(1),address varchar(100),createtime datetime,primary key(sellerid)
);insert into tb_seller values(alibaba,阿里巴巴,阿里小店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(baidu,百度科技有限公司,百度小店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(huawei,华为科技有限公司,华为小店,e10adc3949ba59abbe057f20f883e,0,北京市,2088-01-01 12:00:00),(itcast,传智播客教育科技有限公司,传智播客,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(itheima,黑马程序员,黑马程序员,e10adc3949ba59abbe057f20f883e,0,北京市,2088-01-01 12:00:00),(luoji,罗技科技有限公司,罗技小店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(oppo,oppo科技有限公司,oppo官方旗舰店,e10adc3949ba59abbe057f20f883e,0,北京市,2088-01-01 12:00:00),(ourpalm,掌趣科技股份有限公司,掌趣小店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(qiandu,千度科技,千度小店,e10adc3949ba59abbe057f20f883e,2,北京市,2088-01-01 12:00:00),(sina,新浪科技有限公司,新浪官方旗舰店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00),(xiaomi,小米科技,小米官方旗舰店,e10adc3949ba59abbe057f20f883e,1,西安市,2088-01-01 12:00:00),(yijia,宜家家居,宜家官方旗舰店,e10adc3949ba59abbe057f20f883e,1,北京市,2088-01-01 12:00:00);-- 创建组合索引
create index index_seller_name_sta_addr on tb_seller(name,status,address);
避免索引失效应用-全值匹配
该情况下索引生效执行效率高。 -- 避免索引失效应用-全值匹配
-- 全值匹配和字段匹配成功即可和字段顺序无关
explain select * from tb_seller ts where name 小米科技 and status 1 and address 北京市;explain select * from tb_seller ts where status 1 and name 小米科技 and address 北京市;
避免索引失效应用-最左前缀法则
该情况下索引生效,执行效率高。
-- 避免索引失效应用-最左前缀法则
-- 如果索引了多列要遵守最左前缀法则指的是查询从索引的最左前列开始并且不跳过索引中的列
explain select * from tb_seller ts where name小米科技;-- key_lem:403
explain select * from tb_seller ts where name小米科技 and status 1;-- key_lem:410
explain select * from tb_seller ts where status 1 and name小米科技 ;-- key_lem:410依然跟顺序无关-- 违反最左前缀法则索引失效
explain select * from tb_seller ts where status 1;-- 违反最左前缀法则索引失效-- 如果符合最左前缀法则但是出现跳跃某一列只有最左列索引生效
explain select * from tb_seller where name小米科技 and address北京市;-- key_lem:403 避免索引失效应用-其他匹配原则
该情况下索引生效执行效率高。
1、 -- 避免索引失效应用-其他匹配原则
-- 范围查询右边的列不能使用索引
explain select * from tb_seller where name 小米科技 and status 1 and address北京市;-- key_lem:410,没有使用status这个索引
-- 不要在索引列上进行运算操作索引将失效。
explain select * from tb_seller where substring(name,3,2) 科技;-- 没有使用索引
-- 字符串不加单引号造成索引失效。
explain select * from tb_seller where name小米科技 and status 1 ;-- key_lem:403没有使用status这个索引
2、
explain中的extra列
extra 含义 using filesort说明mysq|会对数据使用一个外部的索引排序而不是按照表内的索引顺序进行读取称为“文件排序 ,效率低。using temporary需要建立临时表(temporary table)来暂存中间结果常见于order by和group by;效率低using index SQL所需要返回的所有列数据均在一棵索引树上避免访问表的数据行效率不错。 using where在查找使用索引的情况下需要回表去查询所需的数据using index condition查找使用了索引但是需要回表查询数据using indexusing where查找使用了索引但是需要的数据都在索引列中能找到所以不需要回表查询数据但是再加有个password 3、 4、 5、
如果MySQL评估使用索引比全表更慢则不使用索引。is NULL , is NOT NULL有时有效,有时索引失效。in走索引not in索引失效。单列索引和复合索引尽量使用符合索引验证 创建了单一的三个索引最后面where全使用了但explain显示只用了index_name 文章转载自: http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.hjwxm.cn.gov.cn.hjwxm.cn http://www.morning.rdkgw.cn.gov.cn.rdkgw.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.zxgzp.cn.gov.cn.zxgzp.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.trrhj.cn.gov.cn.trrhj.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.rhpgk.cn.gov.cn.rhpgk.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.nrftd.cn.gov.cn.nrftd.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn