惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - gavin_l

我们为什么需要另一个“桌面助手”? MongoDB 多实例安装成服务 Hyper-v 中 CentOS 连接外网之有线网卡 VMware下 CentOS 连接外网问题(笔记) Mongo.exe 无法定位程序输入点terminate于动态链接库 *.dll的解决办法 MySql5.7.* 多实例安装部署 词组查询以及多值映射等SolrNet使用中的细节问题 (转)前端不为人知的一面--前端冷知识集锦 (转)jQuery插件开发精品教程,让你的jQuery提升一个台阶 在ASP.NET MVC中,使用Bundle来打包压缩js和css(转) 设计模式 ASP.NET应用程序与页面生命周期 .Net内存泄露原因及解决办法 C#反射Assembly 详细说明 (转)面向程序员的数据库访问性能优化法则 ab 压测工具使用 svn使用教程及常见问题解决方案 日期格式转换 (原)使用 memcache 使用过程中可能遇到的问题
你可能不知道的 10 条 SQL 技巧,涨知识了!
gavin_l · 2017-12-06 · via 博客园 - gavin_l

转自:http://mp.weixin.qq.com/s?__biz=MjM5NzM0MjcyMQ==&mid=2650076293&idx=1&sn=38f6acc759dfabc77a61bdfdc0c03ef8&chksm=bedb2feb89aca6fdb976b2108430d883d1edb7617cb31fb91ea3d0931969d3c7a7f584a1517b&mpshare=1&scene=23&srcid=12060KtNa7JeCXzMtejRHcRc#rd

这几天在写索引,想到一些有意思的TIPS,希望大家有收获。

一、一些常见的SQL实践

(1)负向条件查询不能使用索引 

  • select * from order where status!=0 and stauts!=1 

not in/not exists都不是好习惯 

可以优化为in查询: 

  • select * from order where status in(2,3)

(2)前导模糊查询不能使用索引

  • select * from order where desc like '%XX'

而非前导模糊查询则可以:

  • select * from order where desc like 'XX%'

(3)数据区分度不大的字段不宜使用索引

  • select * from user where sex=1

原因:性别只有男,女,每次过滤掉的数据很少,不宜使用索引。

经验上,能过滤80%数据时就可以使用索引。对于订单状态,如果状态值很少,不宜使用索引,如果状态值很多,能够过滤大量数据,则应该建立索引。

(4)在属性上进行计算不能命中索引

  • select * from order where YEAR(date) < = '2017'

即使date上建立了索引,也会全表扫描,可优化为值计算:

  • select * from order where date < = CURDATE()

或者:

  • select * from order where date < = '2017-01-01'

二、并非周知的SQL实践

(5)如果业务大部分是单条查询,使用Hash索引性能更好,例如用户中心

  • select * from user where uid=?

  • select * from user where login_name=?

原因:

B-Tree索引的时间复杂度是O(log(n))

Hash索引的时间复杂度是O(1)

(6)允许为null的列,查询有潜在大坑

单列索引不存null值,复合索引不存全为null的值,如果列允许为null,可能会得到“不符合预期”的结果集

  • select * from user where name != 'shenjian'

如果name允许为null,索引不存储null值,结果集中不会包含这些记录。

所以,请使用not null约束以及默认值。

(7)复合索引最左前缀,并不是值SQL语句的where顺序要和复合索引一致

用户中心建立了(login_name, passwd)的复合索引

  • select * from user where login_name=? and passwd=?

  • select * from user where passwd=? and login_name=?

都能够命中索引

  • select * from user where login_name=?

也能命中索引,满足复合索引最左前缀

  • select * from user where passwd=?

不能命中索引,不满足复合索引最左前缀

(8)使用ENUM而不是字符串

ENUM保存的是TINYINT,别在枚举中搞一些“中国”“北京”“技术部”这样的字符串,字符串空间又大,效率又低。

三、小众但有用的SQL实践

(9)如果明确知道只有一条结果返回,limit 1能够提高效率

  • select * from user where login_name=?

可以优化为:

  • select * from user where login_name=? limit 1

原因:

你知道只有一条结果,但数据库并不知道,明确告诉它,让它主动停止游标移动

(10)把计算放到业务层而不是数据库层,除了节省数据的CPU,还有意想不到的查询缓存优化效果

  • select * from order where date < = CURDATE()

这不是一个好的SQL实践,应该优化为:

$curDate = date('Y-m-d');

$res = mysql_query(

    'select * from order where date < = $curDate');

原因:

释放了数据库的CPU

多次调用,传入的SQL相同,才可以利用查询缓存

(11)强制类型转换会全表扫描

  • select * from user where phone=13800001234

你以为会命中phone索引么?大错特错了,这个语句究竟要怎么改?

末了,再加一条,不要使用select *(潜台词,文章的SQL都不合格 =_=),只返回需要的列,能够大大的节省数据传输量,与数据库的内存使用量哟。

思路比结论重要,希望你有收获。