本文主要是介绍一个困扰了我三天的SQL优化问题。(多条数据取最近的数据),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于优化的方向不对,一个SQL困扰了我好几天,物化视图什么之类的,全部都试过了,还是没有解决。今天,在看这个问题的时候,灵光一现,咦,好像是这里有问题,然后改了一下,终于解决了。这个SQL,从最初的16秒,后面换了各种方法,有180秒,150秒,60多秒,试过了各种SQL,终于,优化到了0.3秒。好了,现在说下问题。
这个SQL总共涉及了6张表,其实最主要的是做数据的统计。直接看SQL吧。
1.这个是最初的:速度是16秒。
SELECT
*
FROM
(
SELECT
A . ID,
warehousecode,
sku,
OWNER,
enname,
customername,
createtime,
(startnum + changenum) sumnum,
(startnum + changenum - locknum) availablenum,
locknum
FROM
(
SELECT
t1. ID,
t2.warehousecode,
t1.sku,
C . NAME customername,
t1.createtime,
t1. OWNER,
P .cnname enname,
(
NUMBER + COALESCE (locknumber, 0)
) startnum,
(
SELECT
COALESCE (
SUM (
(
CASE
WHEN TYPE = 'inbound' THEN
qty
ELSE
(- 1 * qty)
END
)
),
0
) AS qty
FROM
t_inventory_change tc
WHERE
tc.warehousecode = t2.warehousecode
AND tc. OWNER = t1. OWNER
AND tc.sku = t1.sku
AND tc.createtime > t1.createtime
) changeNum,
(
SELECT
COALESCE (SUM(qty), 0)
FROM
t_inventory_lock tl
WHERE
VALID = 't'
AND tl.warehousecode = t2.warehousecode
AND tl. OWNER = t1. OWNER
AND tl.sku = t1.sku
AND tl.createtime > t1.createtime
) lockNum
FROM
t_inventory_detail t1
LEFT JOIN t_inventory t2 ON t1.inventoryid = t2. ID
LEFT JOIN t_product P ON P . OWNER = t1. OWNER
AND P .status = 1
AND P .sku = t1.sku
LEFT JOIN t_customer C ON C .code = t1. OWNER
WHERE
t1.createtime = (
SELECT
MAX (t3.createtime)
FROM
t_inventory_detail t3,
t_inventory t4
WHERE
t3.inventoryid = t4. ID
AND t2.warehousecode = t4.warehousecode
AND t1.sku = t3.sku
)
) AS A
) AS T
WHERE
1 = 1
order by createtime desc
2.这个是 160 秒的
select DISTINCT t.*
,(NUMBER + COALESCE (locknumber, 0) + changenum) sumnum
,(NUMBER + COALESCE (locknumber, 0) + changenum - locknum) availablenum
from(
select td.sku,td.warehousecode,td.owner,td.cnname,td.number,td.locknumber,td.createtime,
c.name customername,
p.cnname enname,
COALESCE(SUM(CASE WHEN tc.TYPE = 'inbound' THEN tc.qty ELSE (- 1 * tc.qty) END ) OVER(partition by tc.warehousecode,tc.OWNER,tc.sku),0) changenum,
COALESCE(SUM(tl.qty) OVER(partition by tl.warehousecode,tl.OWNER,tl.sku),0) lockNum
from(
select sku,warehousecode,owner,cnname,number,locknumber,createtime from(
select t1.sku,t2.warehousecode,t1.owner,t1.cnname,t1.number,t1.locknumber,t1.createtime,
row_number() over(partition by t1.sku,t1.owner,t2.warehousecode order by t1.createtime desc) rn
from t_inventory_detail t1 left join t_inventory t2 on t1.inventoryid=t2.id) t where rn=1) td
left join t_product p on p.owner = td.owner and p.status = 1 and p .sku = td.sku
left join t_customer c on c.code = td.owner
left join t_inventory_lock tl on tl.warehousecode = td.warehousecode AND tl. OWNER = td. OWNER AND tl.sku = td.sku AND tl.createtime > td.createtime and VALID = 't'
left join t_inventory_change tc on tc.warehousecode = td.warehousecode AND tc. OWNER = td. OWNER AND tc.sku = td.sku AND tc.createtime > td.createtime
) t;
3.这个是0.3秒的
SELECT
*
FROM
(
SELECT
A . ID,
warehousecode,
sku,
OWNER,
enname,
customername,
createtime,
(startnum + changenum) sumnum,
(startnum + changenum - locknum) availablenum,
locknum
FROM
(
SELECT
td. ID,
td.warehousecode,
td.sku,
C . NAME customername,
td.createtime,
td. OWNER,
P .cnname enname,
(
NUMBER + COALESCE (locknumber, 0)
) startnum,
(
SELECT
COALESCE (
SUM (
(
CASE
WHEN TYPE = 'inbound' THEN
qty
ELSE
(- 1 * qty)
END
)
),
0
) AS qty
FROM
t_inventory_change tc
WHERE
tc.warehousecode = td.warehousecode
AND tc. OWNER = td. OWNER
AND tc.sku = td.sku
AND tc.createtime > td.createtime
) changeNum,
(
SELECT
COALESCE (SUM(qty), 0)
FROM
t_inventory_lock tl
WHERE
VALID = 't'
AND tl.warehousecode = td.warehousecode
AND tl. OWNER = td. OWNER
AND tl.sku = td.sku
AND tl.createtime > td.createtime
) lockNum
FROM
(
SELECT
T . ID,
sku,
warehousecode,
OWNER,
cnname,
NUMBER,
locknumber,
createtime
FROM
(
SELECT
t1. ID,
t1.sku,
t2.warehousecode,
t1. OWNER,
t1.cnname,
t1. NUMBER,
t1.locknumber,
t1.createtime,
ROW_NUMBER () OVER (
PARTITION BY t1.sku,
t1. OWNER,
t2.warehousecode
ORDER BY
t1.createtime DESC
) rn
FROM
t_inventory_detail t1
LEFT JOIN t_inventory t2 ON t1.inventoryid = t2. ID
) T
WHERE
rn = 1
) td
LEFT JOIN t_product P ON P . OWNER = td. OWNER
AND P .status = 1
AND P .sku = td.sku
LEFT JOIN t_customer C ON C .code = td. OWNER
) AS A
) AS T
WHERE
1 = 1
order by createtime desc
其实最主要的问题,还是出在红色标记的区别。就是有多个数据的时候,取最近的数据。最开始以为这里没有问题,一直以为是子查询的问题,后面发现并不是子查询的问题。记在这里,给自己一个教训。
这篇关于一个困扰了我三天的SQL优化问题。(多条数据取最近的数据)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!