我有一个很长的查询,有多个子查询,这个查询最多可能需要30秒才能完成。
有没有更好的方法来构建查询?
select FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStartTime,"%m-%d-%Y") AS 'Incident Date', PEG_Category AS Category, (select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXnetworkRe.Market ORDER BY Market) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types IN (1,2) GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID, peg_frm_inc_incidentXnetworkRe.CustomerCode) AS Market,peg_frm_inc_incidentXcustomerR.CustomerName AS 'Customer' , peg_frm_inc_incidentXcustomerR.CustomerTicket AS 'Customer TT#', peg_remedy_incident.PEG_NetworkProvider AS 'Network Provider', peg_remedy_incident.PEG_ProviderTicketNumber AS 'Network Provider TT#',
peg_remedy_incident.PEG_ThirdPartyVendor AS '3rd Party Support Vendor', peg_remedy_incident.PEG_ThirdPartyVendor_TT_ AS '3rd Party TT#','' AS 'Maintenance Window Outage? (Y/N)', TRIM(LEADING '0' FROM peg_frm_inc_incidentXcustomerR.IncidentID) AS 'TT#',FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStartTime,"%m-%d-%Y %r") AS `Event Start Time`, FROM_UNIXTIME(peg_remedy_incident.PEG_OutageStopTime,"%m-%d-%Y %r") AS `Event Stop Time`,
'=(M{ROWID}-L{ROWID})*1440' AS 'Total Minutes', '=(M{ROWID}-L{ROWID})' AS 'Total Hours/Minutes','=(M{ROWID}-L{ROWID})*R{ROWID}*1440' AS ' Total Outage Minutes','=(M{ROWID}-L{ROWID})*R{ROWID}' AS ' Total Outage Hours/Minutes',
(select count(DISTINCT(Site_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1) AS 'Outage Sites Impacted',
(select count(DISTINCT(Circuit_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND
peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1) AS 'Outage Circuits Impacted',
(select count(DISTINCT(Site_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=2) AS 'Simplex Sites Impacted',
(select count(DISTINCT(Circuit_ID)) from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND
peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=2) AS 'Simplex Circuits Impacted',
peg_remedy_incident.PEG_TechName AS 'Technician Dispatch', '' AS 'Summary & Ticket Notes', peg_remedy_incident.PEG_NetworkAvailabilityAffecti ,
CAST( CASE
WHEN peg_remedy_incident.PEG_Ring_Lateral = 'Lateral'
THEN 'Yes'
WHEN peg_remedy_incident.PEG_Ring_Lateral = 'Ring'
THEN 'No'
ELSE ''
END AS char(3)) as 'Lateral (Y/N)' ,
'' AS 'Backhaul/Non-Backhaul?', PEG_Resolution_Details AS 'Resolution Details',
PEG_CauseCode1 AS 'Cause Code 1', PEG_CauseCode2 AS 'Cause Code 2',
PEG_CauseCode3 AS 'Cause Code 3', PEG_CauseCode4 AS 'Cause Code 4', Root_Cause_Analysis AS 'Root Cause Analysis','' AS 'Notes',
(select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXwork_log.Notes ORDER BY Request_ID) AS 'Work Log1' from peg_frm_inc_incidentXwork_log WHERE peg_frm_inc_incidentXwork_log.Incident_ID=peg_frm_inc_incidentXcustomerR.IncidentID GROUP BY peg_frm_inc_incidentXwork_log.Incident_ID) as 'Work Log',
(select GROUP_CONCAT(DISTINCT peg_frm_inc_incidentXnetworkRe.Site_ID ORDER BY Site_ID) AS 'OutageSite List1' from peg_frm_inc_incidentXnetworkRe WHERE peg_frm_inc_incidentXnetworkRe.IncidentVisibleID=peg_frm_inc_incidentXcustomerR.IncidentID AND peg_frm_inc_incidentXcustomerR.CustomerCode=peg_frm_inc_incidentXnetworkRe.CustomerCode AND peg_frm_inc_incidentXnetworkRe.RadioButton_Types=1 GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID, peg_frm_inc_incidentXnetworkRe.CustomerCode) as 'Outage Site List'
FROM peg_frm_inc_incidentXcustomerR LEFT JOIN peg_frm_inc_incidentXnetworkRe ON peg_frm_inc_incidentXcustomerR.IncidentID=peg_frm_inc_incidentXnetworkRe.IncidentVisibleID
LEFT JOIN peg_remedy_incident ON peg_frm_inc_incidentXcustomerR.IncidentID=peg_remedy_incident.PEG_VisibleTicketID WHERE peg_frm_inc_incidentXnetworkRe.RadioButton_Types IN(1 ,2) AND peg_remedy_incident.PEG_VisibleTicketID>=000000000034000 GROUP BY peg_frm_inc_incidentXnetworkRe.IncidentVisibleID,peg_frm_inc_incidentXcustomerR.CustomerCode