gpt4 book ai didi

mysql - 如何优化长SQL语句?

转载 作者:行者123 更新时间:2023-11-29 22:03:55 25 4
gpt4 key购买 nike

在我的项目中,有几个很长的SQL语句。它们超过 100 行,并且有多个 select、join 和嵌套 union。阅读起来比较困难,需要优化。应该注意哪些方面呢?表结构、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}

最佳答案

如果您使用 MySQL,首先,我会阅读有关 an execution plan is 的引用文档。 .

您应该了解服务器在运行您的查询时执行的计划。例如,您可以在其中查看服务器是否正在使用您拥有的索引,或者是否需要添加新索引。

您可以采取多种措施来优化查询。例如,它可能是对您的结构进行非规范化或规范化......(这取决于查询)。如果更新不重要,则添加更多索引、对表进行分区等。

所以首先是在查询之前使用EXPLAINEXPLAIN EXTENDED命令:

EXPLAIN SELECT ...
EXPLAIN EXTENDED SELECT ...

在这里,您可以看到查询背后发生的情况,并进行您认为会提高性能的更改。

关于mysql - 如何优化长SQL语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com