sql语句优化总结
sql语句优化总结
数据库优化的几个原则:
1.尽量避免在列上做运算,这样会导致索引失败;
2.使用join是应该用小结果集驱动大结果集,同时把复杂的join查询拆分成多个query。不然join的越多表,就会导致越多的锁定和堵塞。
3.注意like模糊查询的使用,避免使用%%,例如select * from a where name like '%de%';
代替语句:select * from a where name >= 'de' and name < 'df';
4.仅列出需要查询的字段,不要使用select * from ...,节省内存;
5.使用批量插入语句,节省交互;
insert into a (id ,name)
values(2,'a'),
(3,'s');
6.limit基数比较大时,使用between ... and ...
7.不要使用rand函数随机获取记录;
8.避免使用null ,这就需要在建表时,尽量设置为not null,提升查询性能;
9,不要使用count(id),而应该是count(*)
10.不要做无谓的排序,尽可能在索引中完成排序;
我们先来看一个sql:
select
ii.product_id,
p.product_name,
count(distinct pim.pallet_id) count_pallet_id,
if(round(sum(itg.quantity),2) > -1 && round(sum(itg.quantity),2) < 0.005, 0, round(sum(itg.quantity),2)) quantity,
round(ifnull(sum(itag.locked_quantity), 0.00000),2) locked_quantity,
pc.container_unit_code_name,
if(round(sum(itg.qoh),2) > -1 && round(sum(itg.qoh),2) < 0.005, 0, round(sum(itg.qoh),2)) qoh,
round(ifnull(sum(itag.locked_qoh), 0.00000),2) locked_qoh,
p.unit_code,
p.unit_code_name
from (select
it.inventory_item_id item_id,
sum(it.quantity) quantity,
sum(it.real_quantity) qoh
from
ws_inventory_transaction it
where
it.enabled = 1
group by
it.inventory_item_id
) itg
left join (select
ita.inventory_item_id item_id,
sum(ita.quantity) locked_quantity,
sum(ita.real_quantity) locked_qoh
from
ws_inventory_transaction_action ita
where
1=1 and ita.type in ('locked', 'release')
group by
ita.inventory_item_id
)itag on itg.item_id = itag.item_id
inner join ws_inventory_item ii on itg.item_id = ii.inventory_item_id
inner join ws_pallet_item_mapping pim on ii.inventory_item_id = pim.inventory_item_id
inner join ws_product p on ii.product_id = p.product_id and p.status = 'OK'
left join ws_product_container pc on ii.container_id = pc.container_id
//总起来说关联太多表,设计表时可以多一些冗余字段,减少表之间的关联查询;
where
ii.inventory_type = 'raw_material' and
ii.inventory_status = 'in_stock' and
ii.facility_id = '' and
datediff(now(),ii.last_updated_time) < 3 //违反了第一个原则
and p.product_type = 'goods'
and p.product_name like '%果%' // 违反原则3
group by
ii.product_id
having
qoh < 0.005
order by
qoh desc
上面的sql我们在from 中使用了子查询,这样对查询是非常不利的;
更好的一种做法是下面的语句:
select
t.facility_id,
f.facility_name,
t.inventory_status,
wis.inventory_status_name,
t.inventory_type,
t.product_type,
t.product_id,
p.product_name,
t.container_id,
t.unit_quantity,
p.unit_code,
p.unit_code_name,
pc.container_unit_code_name,
t.secret_key,
sum(t.quantity) quantity,
sum(t.real_quantity) real_quantity,
sum(t.locked_quantity) locked_quantity,
sum(t.locked_real_quantity) locked_real_quantity
from ( select
ii.facility_id,
ii.inventory_status,
ii.inventory_type,
ii.product_type,
ii.product_id,
ii.container_id,
ii.unit_quantity,
ita.secret_key,
ii.quantity quantity,
ii.real_quantity real_quantity,
sum(ita.quantity) locked_quantity,
sum(ita.real_quantity) locked_real_quantity
from
ws_inventory_item ii
inner join ws_inventory_transaction_action ita on ii.inventory_item_id = ita.inventory_item_id
where
ii.facility_id = '{$facility_id}' and
ii.inventory_status = '{$inventory_status}' and
ii.product_type = '{$product_type}' and
ii.inventory_type = '{$inventory_type}' and
ii.locked_real_quantity > 0 and
ita.type in ('locked', 'release')
group by
ii.product_id, ita.secret_key, ii.container_id, ita.inventory_item_id
having
locked_real_quantity > 0
) as t
inner join ws_product p on t.product_id = p.product_id
left join ws_facility f on t.facility_id = f.facility_id
left join ws_inventory_status wis on wis.inventory_status = t.inventory_status
left join ws_product_container pc on pc.container_id = t.container_id
group by
t.product_id, t.secret_key, t.container_id
注意:
1、from 语句中一定不要使用子查询;
2、使用更多的where加以限制,缩小查找范围;
3、合理利用索引;
4、通过explain查看sql性能;
相关优化可以借鉴:http://blog.csdn.net/csh624366188/article/details/8457749
sql语句优化总结的更多相关文章
- 优化数据库的方法及SQL语句优化的原则
优化数据库的方法: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化.(可以用Sybase的SQL Expert,可惜 ...
- oracle之sql语句优化
oracle之sql语句优化 sql语句的优化 1.在where子句中使用 is null 或 is not null 时,oracle优化器就不能使用索引了. 2.对于有连接的列,即使最有一个是静态 ...
- 数据库 基于索引的SQL语句优化之降龙十八掌(转)
一篇挺不错的关于SQL语句优化的文章,因不知原始出处,故未作引用说明! 1 前言 客服业务受到SQL语句的影响非常大,在规模比较大的局点,往往因为一个小的SQL语句不够优化,导致数据库性能急 ...
- 数据库的优化(表优化和sql语句优化)
在这里主要是分为表设计优化和sql语句优化两方面来实现. 首先的是表设计优化: 1.数据行的长度不要超过8020字节.如果是超过这个长度的话这条数据会占用两行,减低查询的效率. 2.能用数字类型就不要 ...
- MySQL常用SQL语句优化
推荐阅读这篇博文,索引说的非常详细到位:http://blog.linezing.com/?p=798#nav-3-2 在数据库日常维护中,最常做的事情就是SQL语句优化,因为这个才是影响性能的最主要 ...
- 浅谈mysql配置优化和sql语句优化【转】
做优化,我在这里引用淘宝系统分析师蒋江伟的一句话:只有勇于承担,才能让人有勇气,有承担自己的错误的勇气.有承担错误的勇气,就有去做事得勇气.无论做什么事,只要是对的,就要去做,勇敢去做.出了错误,承担 ...
- MySQL基础操作&&常用的SQL技巧&&SQL语句优化
基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t ...
- 关于索引的sql语句优化之降龙十八掌
1 前言 客服业务受到SQL语句的影响非常大,在规模比较大的局点,往往因为一个小的SQL语句不够优化,导致数据库性能急剧下降,小型机idle所剩无几,应用服务器断连.超时,严重影响业务的正 ...
- SQL系列 - SQL语句优化个人总结
关于SQL语句优化方法 有些是通用的(如避免Select *): 有些不同的数据库管理系统有所区别(如Where子句顺序): 然后必须根据实际环境进行调优,因为即使是相同的数据库和表,在数据量或其他环 ...
随机推荐
- var在PHP和JS中的使用
一,var在PHP中的使用 var在PHP中使用很少,只在类中声明成员变量时候,可以使用var,其相当于public,而且以后逐渐用public替代var,所以在PHP中尽量不使用var声明变量. 二 ...
- Metasploit域渗透测试全程实录(终结篇)
本文作者:i春秋签约作家——shuteer 前言 内网渗透测试资料基本上都是很多大牛的文章告诉我们思路如何,但是对于我等小菜一直是云里雾里.于是使用什么样的工具才内网才能畅通无阻,成了大家一直以来的渴 ...
- [XHR]——重新认识 XMLHttpRequest
细说XMLHttpRequest如何使用 先来看一段使用XMLHttpRequest发送Ajax请求的简单示例代码. function sendAjax() { //构造表单数据 var formDa ...
- Android 线刷小白教程
Android 线刷小白教程 再说一遍,绝不使用刷机精灵等软件. 一.概念 安卓系统一般把rom芯片分成7个区,如果再加上内置sd卡这个分区,就是8个: hboot分区----------负责启动. ...
- 深度解析CNN
[1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之:CNN卷积神经网络推导和实现 [4]Deep Learning模型之:CNN的反 ...
- 如何在CentOS 7上使用vsftpd设置ftp服务器
一.前言介绍 FTP(文件传输协议)是一种标准的客户机-服务器网络协议,允许用户在远程网络之间传输文件. 有几个开源的FTP服务器可用于Linux.最受欢迎和广泛使用的是pureftpd.proftp ...
- 细化Azure RBAC权限
Azure RBAC权限的细化一直是比较繁琐的事情,以下示例抛砖引玉,供大家参考 客户需求: 新用户在指定资源组下权限需求如下: 一.禁止以下权限 1. 调整虚拟机大小配置 2. 删除&停止虚 ...
- FJWC2019 直径
题目描述 你需要构造一棵至少有两个顶点的树,树上的每条边有一个非负整数边权.树上两点 i,j 的距离dis(i,j) 定义为树上连接i 和j 这两点的简单路径上的边权和. 我们定义这棵树的直径为,所有 ...
- Powershell cannot be loaded because running scripts is disabled on this system 解决办法
问题背景 第一次跑ps时,出现了下面的提示.这是因为windows不允许执行脚本而已,不要大惊小怪. 解决办法 这个需要管理员执行,不然会出现以下的情况 正常情况
- [Xamarin.Android] 如何使用Google Map V2 (转帖)
Google Map v1已經在2013年的3月開始停止支援了,目前若要在你的Android手機上使用到Google Map,就必須要使用 到Google Map v2的版本.在Xamarin要使用G ...