所以我要让这个大选择的女巫与工会一起使用。我在想,有什么方法可以对其进行优化,因为它现在很重。
如您所见,主要区别在于联接表(srv_obj_intermediate
和srv_obj_attributes
)之间的区别,它们具有2个不同的oet.code
SELECT * FROM (SELECT soi.value, srv.osp_id, soi.stya_id, eax.estpt_id, eax.discount, seo.id AS sero_id FROM estimate_attr_xref eax
JOIN attribute_types attl ON attl.id = eax.attr_id
JOIN object_attr_type_links oatl ON oatl.attr_id = attl.id
JOIN service_type_attributes sta ON sta.objt_attr_id = oatl.id
JOIN srv_obj_intermediate soi ON soi.stya_id = sta.id
AND ((soi.value = 0) OR (soi.value = 1 AND festpae_id IS NOT NULL))
JOIN service_objects seo ON seo.id = soi.sero_id
JOIN services srv ON srv.id = seo.srv_id
JOIN order_event oet ON oet.code = 'INTERMEDIATE'
WHERE eax.rate = 1 AND eax.ordet_id = oet.id
AND eax.objt_attr_id = sta.objt_attr_id) WHERE value = 1
UNION
SELECT soa.value, srv.osp_id, soa.stya_id, eax.estpt_id, eax.discount, seo.id AS sero_id FROM estimate_attr_xref eax
JOIN attribute_types attl ON attl.id = eax.attr_id
JOIN object_attr_type_links oatl ON oatl.attr_id = attl.id
JOIN service_type_attributes sta ON sta.objt_attr_id = oatl.id
JOIN srv_obj_attributes soa ON soa.stya_id = sta.id
AND soa.value = 1
LEFT JOIN srv_obj_intermediate soi ON soi.stya_id = sta.id
AND soi.value = 1
JOIN service_objects seo ON seo.id = soa.sero_id
JOIN services srv ON srv.id = seo.srv_id
JOIN order_event oet ON oet.code = 'INITIAL'
WHERE eax.rate = 1 AND eax.ordet_id = oet.id
AND eax.objt_attr_id = sta.objt_attr_id AND soi.value IS NULL
答案 0 :(得分:1)
快速浏览,并且不了解您的数据,关系,卷,索引,分区,CPU等。
(1)在第一个外部选择中(即UNION之前),您似乎有一个过滤器WHERE VALUE = 1
,其中值实际上是soe.value
。在内部选择中,您有一个条件((soi.value = 0) OR (soi.value = 1 AND festpae_id IS NOT NULL))
。仅在内部选择中使用soi.value = 1 AND festpae_id IS NOT NULL
并避免外部选择就足够了吗?您在寻找什么soi.value?
(2)同样,在第二个选择中,您有LEFT JOIN srv_obj_intermediate soi ON soi.stya_id = sta.id
,而在第二个选择中,您有一个过滤器AND soi.value IS NULL
。同样,您在寻找什么soi.value
?
(3)考虑将oet.code过滤器谓词移动到where子句下,并出于here所述的原因使用JOIN order_event oet ON eax.ordet_id = oet.id
,尽管这不能保证性能的提高。您需要检查每种情况下执行计划是否以及如何更改。
(4)所有这些表的统计信息都是最新的吗?
(5)您是否审查过plan?您是否在计划中缺少任何联接和/或具有笛卡尔联接?当您期望使用索引或期望对分区进行修剪时,是否看到全表扫描?如果您不熟悉说明计划,This白皮书是一个很好的起点。