在我的项目中,有几个很长的SQL语句。它们超过100行,多个选择,连接和嵌套联合。它很难阅读,有必要进行优化。 我应该注意哪个方面?表结构,SQL语句本身还是其他什么?如何优化这些长SQL语句?欢迎任何建议。
以下是一段代码。
select p.id,p.from_zone fromzone,p.to_zone tozone,p.action,p.status,saw.srcaddr,daw.dstaddr,aw2.protocol,case when aw2.port=';' then ' ' else aw2.port end port
from policy p join (select sa.id,sa.device,concat(replace(group_concat(sa.nameshow),',',';<br />'),';') srcaddr from
(select sai.id,sai.device,sai.nameshow,max(sai.flag) flagw from
(select psa1.id,psa1.device,a1.network nameshow,case when a1.from_network<=#{srcAddr} and a1.to_network>=#{srcAddr} then 1 else 0 end flag from policy_source_destination_addr psa1 join address a1 on psa1.address=a1.name and psa1.device=a1.device and psa1.zone=a1.zone where psa1.device=#{deviceName} and psa1.direction='source'
union
select psa2.id,psa2.device,psa2.address nameshow,case when a2.from_network<=#{srcAddr} and a2.to_network>=#{srcAddr} then 1 else 0 end flag from policy_source_destination_addr psa2 join address_set as1 on psa2.address=as1.name and psa2.device=as1.device and psa2.zone=as1.zone
join address a2 on as1.device=a2.device and as1.zone=a2.zone and as1.address=a2.name
where psa2.device=#{deviceName} and psa2.direction='source'
union
select psa3.id,psa3.device,psa3.address nameshow,case when a5.from_network<=#{srcAddr} and a5.to_network>=#{srcAddr} then 1 else 0 end flag from policy_source_destination_addr psa3 join address_set as3 on psa3.address=as3.name and psa3.device=as3.device and psa3.zone=as3.zone
join address_set as4 on as3.device=as4.device and as3.zone=as4.zone and as3.address=as4.name
join address a5 on as4.device=a5.device and as4.zone=a5.zone and as4.address=a5.name
where psa3.device=#{deviceName} and psa3.direction='source'
union
select psa3.id,psa3.device,psa3.address nameshow,1 flag from policy_source_destination_addr psa3 where psa3.device=#{deviceName} and psa3.direction='source' and upper(psa3.address)='ANY'
) sai
group by sai.id,sai.device,sai.nameshow
) sa
group by sa.id,sa.device having max(sa.flagw)=1) saw on p.id=saw.id and p.device=saw.device
join (select da.id,da.device,concat(replace(group_concat(da.nameshow),',',';<br />'),';') dstaddr from
(select dai.id,dai.device,dai.nameshow,max(dai.flag) flagw from
(select pda1.id,pda1.device,a3.network nameshow,case when a3.from_network<=#{dstAddr} and a3.to_network>=#{dstAddr} then 1 else 0 end flag from policy_source_destination_addr pda1 join address a3 on pda1.address=a3.name and pda1.device=a3.device and pda1.zone=a3.zone where pda1.device=#{deviceName} and pda1.direction='destination'
union
select pda2.id,pda2.device,pda2.address nameshow,case when a4.from_network<=#{dstAddr} and a4.to_network>=#{dstAddr} then 1 else 0 end flag from policy_source_destination_addr pda2 join address_set as2 on pda2.address=as2.name and pda2.device=as2.device and pda2.zone=as2.zone
join address a4 on as2.device=a4.device and as2.zone=a4.zone and as2.address=a4.name
where pda2.device=#{deviceName} and pda2.direction='destination'
union
select pda3.id,pda3.device,pda3.address nameshow,case when a6.from_network<=#{dstAddr} and a6.to_network>=#{dstAddr} then 1 else 0 end flag from policy_source_destination_addr pda3 join address_set as5 on pda3.address=as5.name and pda3.device=as5.device and pda3.zone=as5.zone
join address_set as6 on as5.device=as6.device and as5.zone=as6.zone and as5.address=as6.name
join address a6 on as6.device=a6.device and as6.zone=a6.zone and as6.address=a6.name
where pda3.device=#{deviceName} and pda3.direction='destination'
union
select pda3.id,pda3.device,pda3.address nameshow,1 flag from policy_source_destination_addr pda3 where pda3.device=#{deviceName} and pda3.direction='destination' and upper(pda3.address)='ANY'
) dai
group by dai.id,dai.device,dai.nameshow
) da
group by da.id,da.device having max(da.flagw)=1) daw on p.id=daw.id and p.device=daw.device
join (select distinct ai.id,ai.device from
(select pa1.id,pa1.device
from policy_application pa1 join application ap1 on pa1.device=ap1.device and pa1.application=ap1.name
where pa1.device=#{deviceName} and upper(ap1.protocol)=#{protocol} and if(instr(ap1.destination_port,'-')=0,ap1.destination_port=#{port},substr(ap1.destination_port,1,instr(ap1.destination_port,'-')-1)+0<=#{port} and substr(ap1.destination_port,instr(ap1.destination_port,'-')+1)+0>=#{port})
union
select pa2.id,pa2.device
from policy_application pa2 join application_set aps1 on pa2.application=aps1.name and pa2.device=aps1.device
join application ap2 on aps1.application=ap2.name and aps1.device=ap2.device
where pa2.device=#{deviceName} and upper(ap2.protocol)=#{protocol} and if(instr(ap2.destination_port,'-')=0,ap2.destination_port=#{port},substr(ap2.destination_port,1,instr(ap2.destination_port,'-')-1)+0<=#{port} and substr(ap2.destination_port,instr(ap2.destination_port,'-')+1)+0>=#{port})
union
select pa3.id,pa3.device
from policy_application pa3 join application_set aps2 on pa3.application=aps2.name and pa3.device=aps2.device
join application_term apt1 on aps2.application=apt1.name and aps2.device=apt1.device
where pa3.device=#{deviceName} and upper(apt1.protocol)=#{protocol} and if(instr(apt1.destination_port,'-')=0,apt1.destination_port=#{port},substr(apt1.destination_port,1,instr(apt1.destination_port,'-')-1)+0<=#{port} and substr(apt1.destination_port,instr(apt1.destination_port,'-')+1)+0>=#{port})
union
select pa4.id,pa4.device
from policy_application pa4 join application_map am4 on pa4.application=am4.name
where pa4.device=#{deviceName} and upper(am4.protocol)=#{protocol} and (if(am4.portvalue is null,1=1,if(length(am4.portvalue)-length(replace(am4.portvalue,'#',''))=2,SUBSTRING_INDEX(am4.portvalue, '#', 1)+0<=#{port} and SUBSTRING_INDEX(SUBSTRING_INDEX(am4.portvalue, '#', -2), '#', 1)+0>=#{port},SUBSTRING_INDEX(am4.portvalue, '#', 1)+0=#{port} or SUBSTRING_INDEX(SUBSTRING_INDEX(am4.portvalue, '#', -2), '#', 1)+0=#{port})))
) ai
) aw on p.id=aw.id and p.device=aw.device
join (select a.id,a.device,a.protocol,concat(replace(group_concat(a.portshow),',',';<br />'),';') port from
(select pa5.id,pa5.device,ap3.protocol,case when substr(ap3.destination_port,1,instr(ap3.destination_port,'-')-1)=substr(ap3.destination_port,instr(ap3.destination_port,'-')+1) then substr(ap3.destination_port,1,instr(ap3.destination_port,'-')-1) else ap3.destination_port end portshow
from policy_application pa5 join application ap3 on pa5.device=ap3.device and pa5.application=ap3.name
where pa5.device=#{deviceName}
union
select pa6.id,pa6.device,ap4.protocol,case when substr(ap4.destination_port,1,instr(ap4.destination_port,'-')-1)=substr(ap4.destination_port,instr(ap4.destination_port,'-')+1) then substr(ap4.destination_port,1,instr(ap4.destination_port,'-')-1) else ap4.destination_port end portshow
from policy_application pa6 join application_set aps3 on pa6.application=aps3.name and pa6.device=aps3.device
join application ap4 on aps3.application=ap4.name and aps3.device=ap4.device
where pa6.device=#{deviceName}
union
select pa7.id,pa7.device,apt2.protocol,case when substr(apt2.destination_port,1,instr(apt2.destination_port,'-')-1)=substr(apt2.destination_port,instr(apt2.destination_port,'-')+1) then substr(apt2.destination_port,1,instr(apt2.destination_port,'-')-1) else apt2.destination_port end portshow
from policy_application pa7 join application_set aps4 on pa7.application=aps4.name and pa7.device=aps4.device
join application_term apt2 on aps4.application=apt2.name and aps4.device=apt2.device
where pa7.device=#{deviceName}
union
select pa8.id,pa8.device,am8.protocol,case when am8.portvalue is null then '' else concat('#',am8.portvalue) end portshow
from policy_application pa8 join application_map am8 on pa8.application=am8.name
where pa8.device=#{deviceName}
) a
group by a.id,a.device,a.protocol) aw2 on p.id=aw2.id and p.device=aw2.device
where p.device=#{deviceName} and p.status=#{status} and p.action=#{action}
order by p.id+0
limit ${start},${rows}
答案 0 :(得分:0)
如果您使用MySQL,首先,我会阅读有关an execution plan is的参考文档。
您应该了解服务器在运行查询时执行的计划。在那里,您可以看到服务器是否正在使用您拥有的索引,或者您是否需要添加新索引等。例如。
您可以采取一些措施来优化查询。它可能是,例如,对结构进行非规范化或将其规范化......(这取决于查询)。如果更新不重要,则分区表等添加更多索引
首先,在查询之前使用 EXPLAIN 和 EXPLAIN EXTENDED 命令:
EXPLAIN SELECT ...
EXPLAIN EXTENDED SELECT ...
从那里,您可以看到查询引擎盖下发生的事情,并进行您认为可以改善效果的更改。
答案 1 :(得分:0)
第一步:尝试使用视图或存储过程以使其更具可读性。
下一步:使用查询计划或自行运行每个视图/查询/存储过程并检查运行时间来检查哪个查询是语句的瓶颈
答案 2 :(得分:0)
哇,这是一段SQL,是一个~40表连接。
鉴于其复杂性,我很难看到任何明显的方法来大幅提升性能。
使用EXPLAIN可能会有所帮助,但是使用它的子查询数量可能只是抱怨大多数(因为它不会使用加入子查询的索引 - 同样适用于视图,这可能使它更具可读性但会可能没有帮助表现,可能会阻碍它。)
有一些小的调整。对于子查询 aw ,您在主要子查询周围有一个额外的查询,它正在执行DISTINCT。由于主要子查询是4个联合查询,因此UNION语句已经消除了重复项。因此,执行DISTINCT的外部查询是多余的。
进一步说。您的主要查询是检查策略表。我认为这大大减少了要处理的记录数,但是每个子查询都返回了大量与之不匹配的行。在每个子查询中对策略执行额外连接可能会更快,以减少每个子查询返回的记录(并在可以使用索引的级别执行此操作)。
这样的事情: -
SELECT p.id,p.from_zone fromzone,p.to_zone tozone,p.action,p.status,saw.srcaddr,daw.dstaddr,aw2.protocol,CASE WHEN aw2.port=';' then ' ' else aw2.port end port
FROM policy p
JOIN
(
SELECT sa.id,sa.device,CONCAT(REPLACE(GROUP_CONCAT(sa.nameshow),',',';<br />'),';') srcaddr
FROM
(
SELECT sai.id,sai.device,sai.nameshow,max(sai.flag) flagw
FROM
(
SELECT psa1.id,psa1.device,a1.network nameshow,CASE WHEN a1.from_network<=#{srcAddr} AND a1.to_network>=#{srcAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr psa1 ON p.id=psa1.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address a1 ON psa1.address=a1.name AND psa1.device=a1.device AND psa1.zone=a1.zone
WHERE psa1.device=#{deviceName} AND psa1.direction='source'
UNION
SELECT psa2.id,psa2.device,psa2.address nameshow,CASE WHEN a2.from_network<=#{srcAddr} AND a2.to_network>=#{srcAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr psa2 ON p.id=psa2.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address_set as1 ON psa2.address=as1.name AND psa2.device=as1.device AND psa2.zone=as1.zone
JOIN address a2 ON as1.device=a2.device AND as1.zone=a2.zone AND as1.address=a2.name
WHERE psa2.device=#{deviceName} AND psa2.direction='source'
UNION
SELECT psa3.id,psa3.device,psa3.address nameshow,CASE WHEN a5.from_network<=#{srcAddr} AND a5.to_network>=#{srcAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr psa3 ON p.id=psa3.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address_set as3 ON psa3.address=as3.name AND psa3.device=as3.device AND psa3.zone=as3.zone
JOIN address_set as4 ON as3.device=as4.device AND as3.zone=as4.zone AND as3.address=as4.name
JOIN address a5 ON as4.device=a5.device AND as4.zone=a5.zone AND as4.address=a5.name
WHERE psa3.device=#{deviceName} AND psa3.direction='source'
UNION
SELECT psa3.id,psa3.device,psa3.address nameshow,1 flag
FROM policy p
JOIN policy_source_destination_addr psa3 ON p.id=psa3.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
WHERE psa3.device=#{deviceName} AND psa3.direction='source' AND upper(psa3.address)='ANY'
) sai
GROUP BY sai.id,sai.device,sai.nameshow
) sa
GROUP BY sa.id,sa.device
HAVING MAX(sa.flagw)=1
) saw
ON p.id=saw.id AND p.device=saw.device
JOIN
(
SELECT da.id,da.device,CONCAT(REPLACE(GROUP_CONCAT(da.nameshow),',',';<br />'),';') dstaddr
FROM
(
SELECT dai.id,dai.device,dai.nameshow,max(dai.flag) flagw
FROM
(
SELECT pda1.id,pda1.device,a3.network nameshow,CASE WHEN a3.from_network<=#{dstAddr} AND a3.to_network>=#{dstAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr pda1 ON p.id=pda1.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address a3 ON pda1.address=a3.name AND pda1.device=a3.device AND pda1.zone=a3.zone where pda1.device=#{deviceName} AND pda1.direction='destination'
union
SELECT pda2.id,pda2.device,pda2.address nameshow,CASE WHEN a4.from_network<=#{dstAddr} AND a4.to_network>=#{dstAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr pda2 ON p.id=pda2.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address_set as2 ON pda2.address=as2.name AND pda2.device=as2.device AND pda2.zone=as2.zone
JOIN address a4 ON as2.device=a4.device AND as2.zone=a4.zone AND as2.address=a4.name
WHERE pda2.device=#{deviceName} AND pda2.direction='destination'
UNION
SELECT pda3.id,pda3.device,pda3.address nameshow,CASE WHEN a6.from_network<=#{dstAddr} AND a6.to_network>=#{dstAddr} then 1 else 0 end flag
FROM policy p
JOIN policy_source_destination_addr pda3 ON p.id=pda3.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN address_set as5 ON pda3.address=as5.name AND pda3.device=as5.device AND pda3.zone=as5.zone
JOIN address_set as6 ON as5.device=as6.device AND as5.zone=as6.zone AND as5.address=as6.name
JOIN address a6 ON as6.device=a6.device AND as6.zone=a6.zone AND as6.address=a6.name
WHERE pda3.device=#{deviceName} AND pda3.direction='destination'
UNION
SELECT pda3.id,pda3.device,pda3.address nameshow,1 flag
FROM policy p
JOIN policy_source_destination_addr pda3 ON p.id=pda3.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
WHERE pda3.device=#{deviceName} AND pda3.direction='destination' AND upper(pda3.address)='ANY'
) dai
GROUP BY dai.id,dai.device,dai.nameshow
) da
GROUP BY da.id,da.device
HAVING MAX(da.flagw)=1
) daw
ON p.id=daw.id AND p.device=daw.device
join
(
SELECT pa1.id,pa1.device
FROM policy p
JOIN policy_application pa1 ON p.id=pa1.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application ap1 ON pa1.device=ap1.device AND pa1.application=ap1.name
WHERE pa1.device=#{deviceName} AND upper(ap1.protocol)=#{protocol} AND if(INSTR(ap1.destination_port,'-')=0,ap1.destination_port=#{port},SUBSTR(ap1.destination_port,1,INSTR(ap1.destination_port,'-')-1)+0<=#{port} AND SUBSTR(ap1.destination_port,INSTR(ap1.destination_port,'-')+1)+0>=#{port})
UNION
SELECT pa2.id,pa2.device
FROM policy p
JOIN policy_application pa2 ON p.id=pa2.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_set aps1 ON pa2.application=aps1.name AND pa2.device=aps1.device
JOIN application ap2 ON aps1.application=ap2.name AND aps1.device=ap2.device
WHERE pa2.device=#{deviceName} AND upper(ap2.protocol)=#{protocol} AND if(INSTR(ap2.destination_port,'-')=0,ap2.destination_port=#{port},SUBSTR(ap2.destination_port,1,INSTR(ap2.destination_port,'-')-1)+0<=#{port} AND SUBSTR(ap2.destination_port,INSTR(ap2.destination_port,'-')+1)+0>=#{port})
UNION
SELECT pa3.id,pa3.device
FROM policy p
JOIN policy_application pa3 ON p.id=pa3.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_set aps2 ON pa3.application=aps2.name AND pa3.device=aps2.device
JOIN application_term apt1 ON aps2.application=apt1.name AND aps2.device=apt1.device
WHERE pa3.device=#{deviceName} AND upper(apt1.protocol)=#{protocol} AND if(INSTR(apt1.destination_port,'-')=0,apt1.destination_port=#{port},SUBSTR(apt1.destination_port,1,INSTR(apt1.destination_port,'-')-1)+0<=#{port} AND SUBSTR(apt1.destination_port,INSTR(apt1.destination_port,'-')+1)+0>=#{port})
UNION
SELECT pa4.id,pa4.device
FROM policy p
JOIN policy_application pa4 ON p.id=pa4.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_map am4 ON pa4.application=am4.name
WHERE pa4.device=#{deviceName} AND upper(am4.protocol)=#{protocol} AND (if(am4.portvalue is null,1=1,if(length(am4.portvalue)-length(replace(am4.portvalue,'#',''))=2,SUBSTRING_INDEX(am4.portvalue, '#', 1)+0<=#{port} AND SUBSTRING_INDEX(SUBSTRING_INDEX(am4.portvalue, '#', -2), '#', 1)+0>=#{port},SUBSTRING_INDEX(am4.portvalue, '#', 1)+0=#{port} or SUBSTRING_INDEX(SUBSTRING_INDEX(am4.portvalue, '#', -2), '#', 1)+0=#{port})))
) aw
ON p.id=aw.id AND p.device=aw.device
JOIN
(
SELECT a.id,a.device,a.protocol,CONCAT(REPLACE(GROUP_CONCAT(a.portshow),',',';<br />'),';') port
FROM
(
SELECT pa5.id,pa5.device,ap3.protocol,CASE WHEN SUBSTR(ap3.destination_port,1,INSTR(ap3.destination_port,'-')-1)=SUBSTR(ap3.destination_port,INSTR(ap3.destination_port,'-')+1) then SUBSTR(ap3.destination_port,1,INSTR(ap3.destination_port,'-')-1) else ap3.destination_port end portshow
FROM policy p
JOIN policy_application pa5 ON p.id=pa5.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application ap3 ON pa5.device=ap3.device AND pa5.application=ap3.name
WHERE pa5.device=#{deviceName}
UNION
SELECT pa6.id,pa6.device,ap4.protocol,CASE WHEN SUBSTR(ap4.destination_port,1,INSTR(ap4.destination_port,'-')-1)=SUBSTR(ap4.destination_port,INSTR(ap4.destination_port,'-')+1) then SUBSTR(ap4.destination_port,1,INSTR(ap4.destination_port,'-')-1) else ap4.destination_port end portshow
FROM policy p
JOIN policy_application pa6 ON p.id=pa6.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_set aps3 ON pa6.application=aps3.name AND pa6.device=aps3.device
JOIN application ap4 ON aps3.application=ap4.name AND aps3.device=ap4.device
WHERE pa6.device=#{deviceName}
UNION
SELECT pa7.id,pa7.device,apt2.protocol,CASE WHEN SUBSTR(apt2.destination_port,1,INSTR(apt2.destination_port,'-')-1)=SUBSTR(apt2.destination_port,INSTR(apt2.destination_port,'-')+1) then SUBSTR(apt2.destination_port,1,INSTR(apt2.destination_port,'-')-1) else apt2.destination_port end portshow
FROM policy p
JOIN policy_application pa7 ON p.id=pa7.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_set aps4 ON pa7.application=aps4.name AND pa7.device=aps4.device
JOIN application_term apt2 ON aps4.application=apt2.name AND aps4.device=apt2.device
WHERE pa7.device=#{deviceName}
UNION
SELECT pa8.id,pa8.device,am8.protocol,CASE WHEN am8.portvalue is null then '' else concat('#',am8.portvalue) END portshow
FROM policy p
JOIN policy_application pa8 ON p.id=pa8.id AND p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
JOIN application_map am8 ON pa8.application=am8.name
WHERE pa8.device=#{deviceName}
) a
GROUP BY a.id,a.device,a.protocol
) aw2
ON p.id=aw2.id AND p.device=aw2.device
WHERE p.device=#{deviceName} AND p.status=#{status} AND p.action=#{action}
ORDER BY p.id+0
LIMIT ${start},${rows}
答案 3 :(得分:0)
order by p.id+0
- id
是VARCHAR
持有号码。那伤害了。
JOIN ( SELECT ... ) JOIN ( SELECT ... )
优化非常,因为这些子查询没有索引。重新构造整个查询,或使用添加索引的tmp表。
where pa7.device=#{deviceName}
- policy_application
需要INDEX(device)
。好吧,如何提供SHOW CREATE TABLE
,以便我们知道您已经拥有哪些索引。
where psa1.device=#{deviceName} and psa1.direction='source'
- policy_source_destination_addr
需要INDEX(device, direction)
(按任意顺序排列)。
答案 4 :(得分:0)
难以阅读不是因为它很长,而是因为缺乏格式化使其视觉上复杂且难以理解。
如果有的话,应该分成更多行...
select
p.id ,
p.from_zone fromzone,
p.to_zone tozone ,
p.action ,
p.status ,
saw.srcaddr
旨在通过降低妨碍大脑理解其含义的视觉复杂性来使其可读......
select psa3.id ,
psa3.device ,
psa3.address nameshow,
case when a5.from_network <=#{srcAddr} and
a5.to_network <=#{srcAddr}
then 1
else 0
end flag
from policy p
join policy_source_destination_addr psa3 on p.id = psa3.id and
p.device = #{deviceName} and
p.status = #{status} and
p.action = #{action}
join address_set as3 on psa3.address = as3.name and
psa3.device = as3.device and
psa3.zone = as3.zone
join address_s ...
视觉复杂程度越低,您的大脑就越能识别代码中有意义的复杂性,例如四列连接,函数和案例语句的存在,列和表别名的存在和值等..
添加内联评论,以帮助他们...我自己的代码库中的一个例子......
with
cte_book_list as (
-- retrieves a list of book_id's and the batch id
-- we do this to provide a list of book_id's that can be used in a number of
-- other CTE's below. The batch ID is included so this table doesn't have to be
-- referenced again.
select
rb.id batch_id,
rb.client_id client_id,
行数无关紧要。我的陈述超过500行,易于阅读,理解和维护。
答案 5 :(得分:0)
我假设您已经知道如何优化正常大小的查询,并且您只想要了解如何控制大量查询的复杂性。
最近我不得不分析和优化一些大型视图(&gt; 30个连接)。 我已经为自己创造了一些工具(仍然是草案,我不愿意在这个阶段分享),这些工具帮助我取得了良好的成绩。
可视化
我用FoundationDB Java parser分叉进行了一段时间的实验,以便我可以解析视图并创建视图结构的MindMap(FreeMind有一个简单的基于xml的格式)。这非常有用,因为在这里和那里重复了一些模式,并且在图表上识别它们很简单。
即使您没有工具,绘制图表也是一个好主意 查询结构的表和子查询
(在Freemind中绘制的示例下面。带有&#34; list&#34;图标的节点是实际表格。)
分而治疗
尝试分解查询:查看自己的每个子查询,联合的每个部分。尝试获取它们的内容并进行一些实验以简化它们或优化它们。
A&#34;本地优化&#34;可能不能保证你有很大的改进,但是这个 阶段对于熟悉所有的大型查询非常重要 部分。
测试,测试,测试
本地方法(&#34; divide et impera&#34;)还有一个好处,就是你可以自己测试每个子查询(如果总查询真的很慢,那么测试任何回归都需要时间总查询)。如果查询的所有部分都在返回相同的行,您可以集中精力使它们更有效地协同工作。
以小型可管理的块测试您的重新设计
在更高级别管理查询
同样适用于&#34; divide et impera&#34;我发现创建我的手工工具很有用:在这种情况下,一个&#34;查询编译器&#34;。
基本上我将每个子查询复制到它自己的文件中,给它一个我能想出的最有意义的名字,并使用一个库来导入主查询中的子查询源(我使用Freemarker)
我们的目标是拥有更多的“manegeable”&#34; SQL之后,有了 优化子查询,你可以分析和优化如何 子查询正在一起工作。
或者,您可以通过为子查询创建视图来实现相同的结果,以便简化高级查询(我没有这种可能性)。