sql优化(b站黑马程序员一)
Posted 健身小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql优化(b站黑马程序员一)相关的知识,希望对你有一定的参考价值。
索引
#查看表索引
show index from tb_inport;
#*出现全表扫描
select * from tb_inport where operateperson='admin';
SELECT * from tb_inport;
#explain *出现全表扫描
explain select * from tb_inport where operateperson='admin';
#explain *出现范围查询,运算 索引失效 字符串不加‘'索引失效
explain select operateperson from tb_inport where inpprice <=40;
explain select operateperson from tb_inport where operateperson like '%admin';
select operateperson from tb_inport where operateperson='admin';
select operateperson from tb_inport where operateperson like '%admin';
select operateperson from tb_inport where operateperson like '%min%';
explain select operateperson from tb_inport where operateperson like '%min%';
#查看数据库执行的相关sql频率
show status like 'Com_%';
#测试order by 关注extra
explain select operateperson,inpprice from tb_inport where operateperson='admin' order by inpprice;
#profiling 分析sql
SELECT @@have_profiling;
#profiling 分析sql 1为已经开启
SELECT @@profiling;
#查询sql的执行时间
show PROFILES;
#找到需要的id 一般sedingdata 慢 ,指查询和返回给客户端的时间
show PROFILE all for QUERY 543;
#trace 分析优化器的执行计划 查看sql会优化成什么样子
#https://blog.csdn.net/FMSCOTT/article/details/99100083
#开启开关 设置日志文件大小 出错后修改为 select version(), @@sql_mode; sql_mode 的值为 'NO_ENGINE_SUBSTITUTION' 这是一次性的
#https://blog.csdn.net/rockage/article/details/79522211?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&utm_relevant_index=4
select version(), @@sql_mode;
set sql_mode='NO_ENGINE_SUBSTITUTION';
#1打开trace 设置格式为json
set optimizer_trace="enabled=on",end_markers_in_json=on;
SET GLOBAL OPTIMIZER_TRACE="enabled=on",END_MARKERS_IN_JSON=on;
SET optimizer_trace_offset=-1, optimizer_trace_limit=1;
#2设置trace最大使用内存
set optimizer_trace_max_mem_size=10000000;
#数据
select * from tb_inport where inpprice <=40;
#检查 information_schema.optimizer_trace;
#观察发现sql 最后优化为 select `tb_inport`.`id` AS `id`,`tb_inport`.`inptime` AS `inptime`,`tb_inport`.`operateperson` AS `operateperson`,`tb_inport`.`number` AS `number`,`tb_inport`.`remark` AS `remark`,`tb_inport`.`inpprice` AS `inpprice`,`tb_inport`.`providerid` AS `providerid`,`tb_inport`.`goodsid` AS `goodsid` from `tb_inport` where (`tb_inport`.`inpprice` <= 40)"
select * from information_schema.optimizer_trace;
select `tb_inport`.`id` AS `id`,`tb_inport`.`inptime` AS `inptime`,`tb_inport`.`operateperson` AS `operateperson`,`tb_inport`.`number` AS `number`,`tb_inport`.`remark` AS `remark`,`tb_inport`.`inpprice` AS `inpprice`,`tb_inport`.`providerid` AS `providerid`,`tb_inport`.`goodsid` AS `goodsid` from `tb_inport` where (`tb_inport`.`inpprice` <= 40)
sql优化:
1、insert语句
insert into tb_test values(1,’tom‘)
insert into tb_test values(1,’tom‘)
chang
合并
insert into tb_test values(1,’tom‘),(2,’tom‘)
事务的手动提交,并保证主键有序
strart transaction
insert into tb_test values(1,’tom‘)
insert into tb_test values(1,’tom‘)
commit;
2、order by
SELECT [column1],[column2],…. FROM [TABLE] ORDER BY [sort];
我目前知道的是:加索引
SELECT [column1],[column2],…. FROM [TABLE] WHERE [columnX] = [value] ORDER BY [sort];
3、group by
order by null; 结尾加入这样就不会执行底层排序
加索引 同order by
https://blog.csdn.net/qq_27037443/article/details/93876706
4、or
保证or的所有字段有索引
使用union : select * from emp id=1 or id=10; range
select * from emp id=1 union select * from emp id=10 ; const
select * from emp id=1 or age=10; index_merg
select * from emp id=1 union select * from emp age=10 ; ref
5、分页
1、先组件排序,根据主键返回原表查询
select id from tb_item oder by id limit 20000,10; all
select id from tb_item t,(select id from tb_item oder by id limit 20000,10) a where a.id =t.id index
总结
1.在表中建立索引,优先考虑where.group by使用到的字段。
2.查询条件中,一定不要使用select ,因为会返回过多无用的字段会降低查询效率。应该使用具体的字段代替,只返回使用到的字段。
3.不要在where条件中使用左右两边都是%的like模糊查询,如:
SELECT * FROM t_order WHERE customer LIKE ‘%zhang%’
这样会导致数据库引擎放弃索引进行全表扫描。
优化:尽量在字段后面使用模糊查询。如下:
SELECT * FROM t_order WHERE customer LIKE ‘zhang%’
4.尽量不要使用in 和not in,会造成全表扫描。如下:
SELECT * FROM t_order WHERE id IN (2,3)
SELECT * FROM t_order1 WHERE customer IN (SELECT customer FROM t_order2)
优化:
对于连续的数值,能用 between 就不要用 in ,如下:SELECT * FROM t_order WHERE id BETWEEN 2 AND 3
对于子查询,可以用exists代替。如下:SELECT * FROM t_order1 WHERE EXISTS (SELECT * FROM t_order2 WHERE t1.customer = t2.customer)
5.尽量不要使用or,会造成全表扫描。如下:
SELECT * FROM t_order WHERE id = 1 OR id = 3
优化:可以用union代替or。如下:
SELECT * FROM t_order WHERE id = 1
UNION
SELECT * FROM t_order WHERE id = 3
6.尽量不要在 where 子句中对字段进行表达式操作,这样也会造成全表扫描。如:
select id FROM t_order where num/2=100
应改为:
select id FROM t_order where num=100*2
7.where条件里尽量不要进行null值的判断,null的判断也会造成全表扫描。如下:
SELECT * FROM t_order WHERE score IS NULL
优化:
给字段添加默认值,对默认值进行判断。如:
SELECT * FROM t_order WHERE score = 0
8.尽量不要在where条件中等号的左侧进行表达式.函数操作,会导致全表扫描。如下:
SELECT * FROM t_order2 WHERE score/10 = 10
SELECT * FROM t_order2 WHERE SUBSTR(customer,1,5) = ‘zhang’
优化:
将表达式.函数操作移动到等号右侧。如下:
SELECT * FROM t_order2 WHERE score = 10*10
SELECT * FROM t_order2 WHERE customer LIKE ‘zhang%’
9.尽量不要使用where 1=1的条件
有时候,在开发过程中,为了方便拼装查询条件,我们会加上该条件,这样,会造成进行全表扫描。如下:
SELECT * FROM t_order WHERE 1=1
优化:
如果用代码拼装sql,则由代码进行判断,没where加where,有where加and
如果用mybatis,请用mybatis的where语法。
10.程序要尽量避免大事务操作,提高系统并发能力。
11.一个表的索引数最好不要超过6个,如果索引太多的话,就需要考虑一下那些不常使用到的列上建的索引是否有必要。
**使用sql提示: **
以上是关于sql优化(b站黑马程序员一)的主要内容,如果未能解决你的问题,请参考以下文章