当前位置:网站首页>那些年我们一起优化的SQL
那些年我们一起优化的SQL
2022-04-22 13:47:00 【InfoQ】
select * from my_table where col_a=1 and col_b=1select * from my_table where col_a=1 and col_b=1 and col_c=1SELECT sum(col_c) FROM my_table where col_a=1 and col_b=1select * from my_table where col_b=1
select * from my_table order by col_b
select * from my_table order by col_a,col_bselect * from my_table where col_b=1 order by col_aselect * from my_table where col_a=1
select * from my_table where col_b=1603296000000
- col_a是字符类型,使用了数字类型进行查询。
- col_b是datetime类型,针对datetime/date/time类型,MySQL增删查改都要基于字符串形式日期去处理,否则MySQL就需要额外进行转换。(虽然底层储存的是数字类型,但是并不是存储时间戳,底层是处理是统一将外部传入的字符串进行转换,比如是date类型通过将 “2021-12-01” 字符串转数字 20211201 这种形式去存储)。
select * from my_table where col_a in (1,2) order by col_b- 如果col_a的过滤性不高,在组合索引中可以通过将col_b字段前置,将col_a移动到组合索引后面,只用于避免或减少回表。
- 如果col_a的过滤性高,过滤后的数据相对较少,则维持当前的索引即可,剩余不多的数据通过filesort进行排序。
- 如果存在大量数据,并且经过col_b过滤后还是存在大量数据,建议基于别的数据存储实现,比如Elasticsearch。
select * from table where col_a >'2021-12-01' and col_b=10select * from table where col_a=1 and col_b like '%name%'select * from table where col_a=1 or col_b=''select * from table where col_a=1 and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(col_b);
select * from table where col_a=1 and col_b+1=10
select * from table where col_a=1 and col_b not in (1,2)
select * from table where col_a=1 and col_b != 1
select * from table where col_a>'2017-10-22'select * from my_table where col_a=1 order by col_b desc,col_c asc- 组合索引左匹配原则
- 发生隐式转换
- 组合索引,in + order by in会阻断排序用索引
- 范围查询会阻断组合索引,索引涉及到范围查询的索引字段要放在组合索引的最后面。
- 前模糊匹配导致索引失效
- or查询,查询条件部分有索引,部分无索引,导致索引失效。
- 查询条件使用了函数运算、四则运算等。
- 使用了!=、not in
- 选择性过低
- asc和desc混用
select * from my_table where col_c=1 limit 1000,10
# 避免深分页
select * from my_table where id>上次查询的数据id值 limit 100
# 延迟关联 避免大量回表
SELECT * FROM my_table t1,(select id from my_table where col_c=1 limit 1000,100) t2 where t1.id=t2.id
select * from table where col_a=1 and col_b=2 order by id


select * from my_table where col_a="value" and col_b like "%value%"版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/423c7a39a3795e973a7958f7c
边栏推荐
- 回忆,这一年(华师918血泪贴)
- What saved me 60% of my coding time? Use MBG
- 【Zeekr_Tech】ROS/ROS 2介绍
- MySQL DNS解析和主机缓存
- PHP 零基础入门笔记(12):数组 array
- Redis persistence
- Database resource load management (Part 2)
- C# 7.0 使用下划线忽略使用的变量
- Brief analysis of Apache skywalking alarm dynamic configuration source code
- How to perform multi table associated query on Citrix SQL data
猜你喜欢
随机推荐
Understand efficientnet
产业园区数字化运营管理之“精准招商”篇
產業園區數字化運營管理之“精准招商”篇
东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
An article to quickly understand the oal of Apache skywalking
【Zeekr_Tech】ROS/ROS 2介绍
Statistical inference
Originally, this is the correct posture for developers to open world book day!
MySQL DNS解析和主机缓存
[fluent special topic] 91 illustration of future (II) yyds dry goods inventory of dart single thread asynchronous processing
Communication principle of SPI protocol
How does environment postprocessor do unit testing? Alibaba P7 answers
Brief analysis of Apache skywalking alarm dynamic configuration source code
智汀如何连接小米智能音箱?
【直播来袭】OneOS系统教程全面上线,邀您和ST、OneOS一起来学习啦!
Troubles of beautiful colleagues: how to configure Apache skywalking alarm?
Qt5 qdialog removes the question mark in the upper right corner and adds maximize and minimize buttons
11 standard library of in-depth C language and program operation principle: in-depth understanding of standard IO (learning notes)
微信小程序添加数据到数据库
Genesis创意漫画之【稳定通证】









