dense_rank()+hash提示改写优化SQL
数据库环境:SQL SERVER 2005
今天看到一条SQL,返回10条数据,执行了50多S。刚好有空,就对它进行了优化,优化后1S出结果。
先看下原始SQL
SELECT t1.line_no ,
MAX(sat100.confrim_date) confrim_date ,
sam63_lq.company_name ,
sat04.c_code ,
ctlm23.corr_name ,
MAX(sat04.l_date_d) l_date_d ,
SUM(sat05.qty_d_order) qty_d_order ,
sat100.tran_no_row
FROM sat100
INNER JOIN sat101 ON sat100.com_id = sat101.com_id
AND sat100.tran_no = sat101.tran_no
AND sat100.tran_row = sat101.tran_row
LEFT JOIN sat05 ON sat101.com_id = sat05.com_id
AND sat101.p_g_order_no = sat05.p_g_order_no
AND sat101.p_g_order_line = sat05.p_g_order_line
LEFT JOIN sat04 ON sat04.com_id = sat05.com_id
AND sat04.p_g_order_no = sat05.p_g_order_no
LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
AND sat05.item_no = ctlm22.item_no
LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
AND sat100.company_id = sam63_lq.company_id
LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
AND sat05.cx_item_no = sam60_lq.cx_item_no
LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no
AND sat100.company_id = sam65_lq.company_id
LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
AND sat05.sa_unit = ctlm17.unit
LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
AND sat04.c_code = salm02.client_id
LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
AND sat04.c_code = ctlm23.corr_id
LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
LEFT JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY t2.tran_no_row ) line_no ,
t2.tran_no_row
FROM ( SELECT DISTINCT
sat100.tran_no_row
FROM sat100
INNER JOIN sat101 ON sat100.com_id = sat101.com_id
AND sat100.tran_no = sat101.tran_no
AND sat100.tran_row = sat101.tran_row
LEFT JOIN sat05 ON sat101.com_id = sat05.com_id
AND sat101.p_g_order_no = sat05.p_g_order_no
AND sat101.p_g_order_line = sat05.p_g_order_line
LEFT JOIN sat04 ON sat04.com_id = sat05.com_id
AND sat04.p_g_order_no = sat05.p_g_order_no
LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
AND sat05.item_no = ctlm22.item_no
LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
AND sat100.company_id = sam63_lq.company_id
LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
AND sat05.cx_item_no = sam60_lq.cx_item_no
LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
AND sat05.sa_unit = ctlm17.unit
LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
AND sat04.c_code = salm02.client_id
LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
AND sat04.c_code = ctlm23.corr_id
LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
WHERE salm02.dept_id LIKE '%'
AND sat100.company_id = ''
AND sat100.corr_id LIKE 'A010131%'
AND sat04.l_date_d >= '2015/06/01 00:00:00'
AND sat04.l_date_d <= '2015/06/30 23:59:59'
) t2
) t1 ON sat100.tran_no_row = t1.tran_no_row
WHERE salm02.dept_id LIKE '%'
AND sat100.company_id = ''
AND sat100.corr_id LIKE 'A010131%'
AND sat04.l_date_d >= '2015/06/01 00:00:00'
AND sat04.l_date_d <= '2015/06/30 23:59:59'
GROUP BY t1.line_no ,
sam63_lq.company_name ,
sat04.c_code ,
ctlm23.corr_name ,
sat100.tran_no_row
ORDER BY t1.line_no ,
sat100.tran_no_row
下面,我来说下我的优化思路:
1.检查SQL的写法是否有问题
先看下子查询部分,发现和外部访问的表及过滤的条件都差不多,用BeyondCompare工具检查外部查询和子查询的差别,相对于外部查询,
子查询少访问了一个表sam65_lq,即少了这部分内容“LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no AND sat100.company_id
= sam65_lq.company_id”,恰巧字段veh_no和字段company_id是sam65_lq的联合主键,因此,这部分并没有影响查询的数据。
再看下子查询,它要实现的功能就是根据不重复的tran_no_row生成一个序号,因此,可以用dense_rank()替代子查询实现相同的功能。同时,
sat04有过滤条件,因而可以将left join sat04改成inner join sat04。
改写后的SQL如下:
SELECT line_no ,
MAX(confrim_date) confrim_date ,
company_name ,
c_code ,
corr_name ,
MAX(l_date_d) l_date_d ,
SUM(qty_d_order) qty_d_order ,
tran_no_row
FROM ( SELECT DENSE_RANK() OVER ( ORDER BY sat100.tran_no_row ) AS line_no ,
sat100.confrim_date ,
sam63_lq.company_name ,
sat04.c_code ,
ctlm23.corr_name ,
sat04.l_date_d ,
sat05.qty_d_order ,
sat100.tran_no_row
FROM sat100
INNER JOIN sat101 ON sat100.com_id = sat101.com_id
AND sat100.tran_no = sat101.tran_no
AND sat100.tran_row = sat101.tran_row
LEFT JOIN sat05 ON sat101.com_id = sat05.com_id
AND sat101.p_g_order_no = sat05.p_g_order_no
AND sat101.p_g_order_line = sat05.p_g_order_line
INNER JOIN sat04 ON sat04.com_id = sat05.com_id
AND sat04.p_g_order_no = sat05.p_g_order_no
LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
AND sat05.item_no = ctlm22.item_no
LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
AND sat100.company_id = sam63_lq.company_id
LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
AND sat05.cx_item_no = sam60_lq.cx_item_no
LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no
AND sat100.company_id = sam65_lq.company_id
LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
AND sat05.sa_unit = ctlm17.unit
LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
AND sat04.c_code = salm02.client_id
LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
AND sat04.c_code = ctlm23.corr_id
LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
WHERE salm02.dept_id IS NOT NULL
AND sat100.company_id = ''
AND sat100.corr_id LIKE 'A010131%'
AND sat04.l_date_d >= '2015/06/01 00:00:00'
AND sat04.l_date_d <= '2015/06/30 23:59:59'
) t
GROUP BY line_no ,
company_name ,
c_code ,
corr_name ,
tran_no_row
ORDER BY line_no ,
tran_no_row
执行后,检查数据,发现和原始SQL的数据一致,说明改写的语法没有问题。但是,执行的时间没多大改观。
2.检查是否有缺失索引
看了下执行计划,发现在表sat100和表sat04上都走了索引扫描,在sat100表上没有针对company_id和corr_id建和组合索引,sat04表上也没有
针对l_date_d字段建的索引,而恰巧这些列的选择性都和高,适合建索引。
建了索引后,再执行SQL,发现最快的还是要10S。不行,还得继续优化。
3.检查执行计划,看执行计划是否合理
sat05走了索引查找,它的查询开销达到88%。sat05表作为嵌套循环的内部表,外部表有5W多行记录,sat05表总共有50多W条记录,
且在sat05表上没有任何过滤条件。因此,可以判定,这里不应该走嵌套循环,应该使用哈希连接。所以,直接强制走哈希即可。
改写优化后的完整SQL如下:
SELECT line_no ,
MAX(confrim_date) confrim_date ,
company_name ,
c_code ,
corr_name ,
MAX(l_date_d) l_date_d ,
SUM(qty_d_order) qty_d_order ,
tran_no_row
FROM ( SELECT DENSE_RANK() OVER ( ORDER BY sat100.tran_no_row ) AS line_no ,
sat100.confrim_date ,
sam63_lq.company_name ,
sat04.c_code ,
ctlm23.corr_name ,
sat04.l_date_d ,
sat05.qty_d_order ,
sat100.tran_no_row
FROM sat100
INNER JOIN sat101 ON sat100.com_id = sat101.com_id
AND sat100.tran_no = sat101.tran_no
AND sat100.tran_row = sat101.tran_row
LEFT HASH JOIN sat05 ON sat101.com_id = sat05.com_id
AND sat101.p_g_order_no = sat05.p_g_order_no
AND sat101.p_g_order_line = sat05.p_g_order_line
INNER JOIN sat04 ON sat04.com_id = sat05.com_id
AND sat04.p_g_order_no = sat05.p_g_order_no
LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
AND sat05.item_no = ctlm22.item_no
LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
AND sat100.company_id = sam63_lq.company_id
LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
AND sat05.cx_item_no = sam60_lq.cx_item_no
LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no
AND sat100.company_id = sam65_lq.company_id
LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
AND sat05.sa_unit = ctlm17.unit
LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
AND sat04.c_code = salm02.client_id
LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
AND sat04.c_code = ctlm23.corr_id
LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
WHERE salm02.dept_id IS NOT NULL
AND sat100.company_id = ''
AND sat100.corr_id LIKE 'A010131%'
AND sat04.l_date_d >= '2015/06/01 00:00:00'
AND sat04.l_date_d <= '2015/06/30 23:59:59'
) t
GROUP BY line_no ,
company_name ,
c_code ,
corr_name ,
tran_no_row
ORDER BY line_no ,
tran_no_row
dense_rank()+hash提示改写优化SQL的更多相关文章
- 利用查询提示优化SQL
数据库环境:SQL SERVER 2005 我们来看一下SQL语句及对应的数据量 SELECT COUNT(*) FROM cinvout_02 a WHERE ( a.dept_id IN ( SE ...
- 优化sql,返回行数少情况下,NL比hash快好多
sql如下 select t.id, t.value, tt.sort as sortno from ENGINEERING_TYPE t left join ENGINEERING_TYPE tt ...
- sql语句优化SQL Server
MS SQL Server查询优化方法查询速度慢的原因很多,常见如下几种 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了 ...
- 转载 50种方法优化SQL Server数据库查询
原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...
- [20181220]使用提示OR_EXPAND优化.txt
[20181220]使用提示OR_EXPAND优化.txt --//链接http://www.itpub.net/thread-2107240-2-1.html,http://www.itpub.ne ...
- merge into优化sql(转)
使用Merge INTO优化SQL,性能提升巨大 分类: Oracle 2017-04-13 10:55:07 说说背景:开发有个需求,需要对新加的一个字段根据特定的业务逻辑更新数据.TPS_TR ...
- 如何用 SQL Tuning Advisor (STA) 优化SQL语句
在Oracle10g之前,优化SQL是个比较费力的技术活,不停的分析执行计划,加hint,分析统计信息等等.在10g中,Oracle推出了自己的SQL优化辅助工具: SQL优化器(SQL Tuning ...
- mysql优化SQL语句的一般步骤及常用方法
一.优化SQL语句的一般步骤 1. 通过show status命令了解各种SQL的执行频率 mysqladmin extended-status 或: show [session|global]sta ...
- 利用ordered hints优化SQL
SQL_ID 4g70n3k9bqc5v, child number 0 ------------------------------------- MERGE INTO YJBZH_GRXDFHZ ...
随机推荐
- asp.net EasyUI DataGrid 实现增删改查
转自:http://www.cnblogs.com/create/p/3410314.html 前台代码: <!DOCTYPE html> <html xmlns="htt ...
- java中Pattern.compile函数的相关解释
Pattern.compile函数:Pattern Pattern.compile(String regex, int flag) flag的取值范围如下: Pattern.CANON_EQ,当且仅当 ...
- HDU-1495 非常可乐(BFS)
广搜的灵活应用题: 非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- VS2010+Oracle11+Entity Framework4.1环境搭建及常见问题(转)
一,开场白: 在微软的实体数据模型中存在四种查询方式:SQL字符串:Linq:Linq to SQL:Linq to Entity(ESQL) 对于Linq SQL目前微软虽然仍在支持,但微软已经声明 ...
- JavaScript---网络编程(9-1)--DHTML技术演示(2-1)-表格创建的几种方式
一:用最底层的方式,该方式用来创建别的对象树也可以 代码演示: <html> <head> <title>DHTML技术演示---表格创建--用最底层的方式,其实该 ...
- MongoDB:The Definitive Guide CHAPTER 2 Getting Started
MongoDB is very powerful, but it is still easy to get started with. In this chapter we’ll introduce ...
- 数字集成电路设计-8-一个简单sobel图像边缘检测加速器的设计,实现,仿真与综合
引言 图像视频处理等多媒体领域是FPGA应用的最主要的方面之一,边缘检测是图像处理和计算机视觉中的基本问题,所以也是最常用的,随着数据量的不断增加以及对实时性的要求,一般软件已经不能满足实际需要,这时 ...
- PHP开发APP接口(二)
这里将会调用前面博客的数据库连接单例.文件缓存类和开发APP接口(一) <?php // http://app.com/list.php?page-=1&pagesize=12 requ ...
- Unity3D延迟回调的封装
最近,整理项目框架逻辑时,无意中翻到n年前封装的延迟回调管理器,就拎出来说道说道: 先说一下系统提供的几种常用的延迟回调方式: 1)Invoke(Invoke.CancelInvoke):首先需要继承 ...
- sql优化-hint的作用
目前,oracle采用的是CBR优化器,所以在有些时候,机器会按照自己的意愿去执行sql,当然oracle是根据本身的一些信息来做决定的,比如:统计信息.但有些时候,机器并不一定会按照我们预想的那样去 ...