Oracle SQL查询的性能调优
Posted
技术标签:
【中文标题】Oracle SQL查询的性能调优【英文标题】:Performance tuning of Oracle SQL query 【发布时间】:2020-04-06 04:59:12 【问题描述】:有人可以帮我调整这个查询吗? sqldeveloper返回数据需要1分钟时间。
SELECT
masterid, notification_id, notification_list, typeid,
subject, created_at, created_by, approver, sequence_no,
productid, statusid, updated_by, updated_at, product_list,
notification_status, template, notification_type, classification
FROM
(
SELECT
masterid, notification_id, notification_list, typeid, subject,
approver, created_at, created_by, sequence_no, productid,
statusid, updated_by, updated_at, product_list, notification_status,
template, notification_type, classification,
ROW_NUMBER() OVER(ORDER BY masterid DESC)AS r
FROM
(
SELECT DISTINCT
a.masterid AS masterid,
a.maxid AS notification_id,
notification_list,
typeid,
noti.subject AS subject,
noti.approver AS approver,
noti.created_at AS created_at,
noti.created_by AS created_by,
noti.sequence_no AS sequence_no,
a.productid AS productid,
a.statusid AS statusid,
noti.updated_by AS updated_by,
noti.updated_at AS updated_at,
(
SELECT LISTAGG(p.name,',') WITHIN GROUP(ORDER BY p.id) AS list_noti
FROM product p
INNER JOIN notification_product np ON np.product_id = p.id
WHERE notification_id = a.maxid
) AS product_list,
(
SELECT description
FROM notification_status
WHERE id = a.statusid
) AS notification_status,
(
SELECT name
FROM template
WHERE id = a.templateid
) AS template,
(
SELECT description
FROM notification_type
WHERE id = a.typeid
) AS notification_type,
(
SELECT tc.description
FROM template_classification tc
INNER JOIN notification nt ON tc.id = nt.classification_id
WHERE nt.id = a.maxid
) AS classification
FROM
(
SELECT
nm.id AS masterid,
nm.product_id AS productid,
nm.notification_status_id AS statusid,
nm.template_id AS templateid,
nm.notification_type_id AS typeid,
(
SELECT MAX(id)
FROM notification
WHERE notification_master_id = nm.id
) AS maxid,
(
SELECT LISTAGG(n.id,',') WITHIN GROUP(ORDER BY nf.id) AS list_noti
FROM notification n
WHERE notification_master_id = nm.id
) AS notification_list
FROM notification_master nm
INNER JOIN notification nf ON nm.id = nf.notification_master_id
WHERE nm.disable = 'N'
ORDER BY nm.id DESC
) a
INNER JOIN notification noti
ON a.maxid = noti.id
AND
(
(
(
TO_DATE('01-jan-1970','dd-MM-YYYY') +
numtodsinterval(created_at / 1000,'SECOND')
) <
(current_date + INTERVAL '-21' DAY)
)
OR (typeid exists(2,4) AND statusid = 4)
)
)
)
WHERE r BETWEEN 11 AND 20
【问题讨论】:
如果你能分享这个查询的解释计划会很有帮助OR (typeid EXISTS (2,4)
是无效的 SQL。你的意思是OR (typeid IN (2,4)
?
您使用的是哪个 Oracle 版本?
@ThorstenKettner 我正在使用 oracle 19,你是对的,我正在寻找所有通知大师,最后一次通知恰好是 21 天前或具有某种类型和状态。 .......查询正常执行,没有任何失败,但获取数据需要 1.2 分钟的时间。我想减少获取数据的时间。
【参考方案1】:
DISTINCT
通常表示查询写得不好。规范化的数据库不包含重复数据,那么您必须使用DISTINCT
删除的重复数据突然来自哪里?很多时候是你自己的查询产生这些。一开始就避免产生重复,这样以后就不需要DISTINCT
了。
在您的情况下,您在子查询a
中加入了表notification
,但您没有在该子查询中使用它的行;你只能从notification_master_id
中选择。
毕竟,您想获得通知大师,获得他们最新的相关通知(首先获取其 ID,然后选择行)。您不需要数百个子查询来实现这一点。
一些旁注:
要从template_classification
获取描述,您将再次加入通知表,这不是必需的。
子查询 (ORDER BY nm.id DESC
) 中的 ORDER BY
是多余的,因为子查询结果按照标准 SQL 未排序。 (Oracle 有时会违反此标准,以便在结果上应用 ROWNUM
,但您没有在查询中使用 ROWNUM
。)
很遗憾您将created_at
存储为一个数字而不是DATE
或TIMESTAMP
。这迫使你计算。不过,我认为这对您的查询影响不大,因为您在 OR
条件下使用它。
CURRENT_DATE
为您提供客户日期。这是很少需要的,因为您从数据库中选择数据,这当然不应该与某些客户的日期相关,而是与它自己的日期相关 SYSDATE
。
如果我没记错的话,你的查询可以缩短为:
SELECT
nm.id AS masterid,
nf.id AS notification_id,
nfagg.notification_list AS notification_list,
nm.notification_type_id AS typeid,
nf.subject AS subject,
nf.approver AS approver,
nf.created_at AS created_at,
nf.created_by AS created_by,
nf.sequence_no AS sequence_no,
nm.product_id AS productid,
nm.notification_status_id AS statusid,
nf.updated_by AS updated_by,
nf.updated_at AS updated_at,
(
SELECT LISTAGG(p.name, ',') WITHIN GROUP (ORDER BY p.id)
FROM product p
INNER JOIN notification_product np ON np.product_id = p.id
WHERE np.notification_id = nf.id
) AS product_list,
(
SELECT description
FROM notification_status
WHERE id = nm.notification_status_id
) AS notification_status,
(
SELECT name
FROM template
WHERE id = nm.template_id
) AS template,
(
SELECT description
FROM notification_type
WHERE id = nm.notification_type_id
) AS notification_type,
(
SELECT description
FROM template_classification
WHERE id = nf.classification_id
) AS classification
FROM notification_master nm
INNER JOIN
(
SELECT
notification_master_id,
MAX(id) AS maxid,
LISTAGG(id,',') WITHIN GROUP (ORDER BY id) AS notification_list
FROM notification
GROUP BY notification_master_id
) nfagg ON nfagg.notification_master_id = nm.id
INNER JOIN notification nf
ON nf.id = nfagg.maxid
AND
(
(
DATE '1970-01-01' + NUMTODSINTERVAL(nf.created_at / 1000, 'SECOND')
< CURRENT_DATE + INTERVAL '-21' DAY
)
OR (nm.notification_type_id IN (2,4) AND nm.notification_status_id = 4)
)
WHERE nm.disable = 'N'
ORDER BY nm.id DESC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;
如前所述,您可能希望将 CURRENT_DATE
替换为 SYSDATE
。
我为查询推荐以下索引:
CREATE INDEX idx1 ON notification_master (disable, id, notification_status_id, notification_type_id);
CREATE INDEX idx2 ON notification (notification_master_id, id, created_at);
关于分页的最后一句话:为了跳过 n 行以获取下一个 n,必须对所有数据执行整个查询,然后对所有结果行进行排序,最后选择其中的 n 个。通常最好记住最后一次获取的 ID,然后在下次执行时只选择 ID 较高的行。
【讨论】:
感谢您的快速支持。它工作没有任何失败 它更快吗?如果是这种情况,您可以接受此答案(通过单击旁边的复选标记),或者等待显示其他选项的另一个答案。 是的,现在更快了。它返回的数据与我正在寻找的数据相同。再次感谢您的即时支持。以上是关于Oracle SQL查询的性能调优的主要内容,如果未能解决你的问题,请参考以下文章