我不是 SQL 专家,请帮忙,我该如何优化这个查询?
我没有索引,只有名为 master_table 的表和更多表,我必须优化此查询以获得相同的结果,如果必须,我可以创建索引,但我无法更改创建表....
select month(date_hour), passenger, nationality, passport, airline,
count(*) N_Viagens
from masterTable
group by month(date_hour), passenger, airline, nationality, passport
having count(*) > 10
接下来是创建表的代码,但我无法编辑该代码,只能编辑查询或创建索引:
select
p.birthdate, p.gender, p.passport, p.name + ' ' + p.surname passenger, p.nationality,
r.class, r.flightNR, r.payment, r.ticketNR,
f.src_AP_ID, f.dest_AP_ID, f.AL_ID, f.date_hour, f.AirCrft_Code,ac.manufacturer, ac.model,
SA.City 'Origin City', SA.Country 'Origin Country', SA.Name 'Origin Airport',
DA.City 'Dest City', DA.Country 'Dest Country', DA.Name 'Dest Airport',
al.Name airline, al.IATA, al.icao
into masterTable
from passenger p
join reservation r on r.passport = p.passport
join flight f on f.flightNR = r.flightNR
join airport SA on f.src_AP_ID = SA.AP_Id
join airport DA on f.dest_AP_ID = DA.AP_Id
join aircraft ac on f.airCrft_Code = ac.code
join airline al on f.AL_ID = al.AL_ID
无索引:
SQL Server Execution Times: CPU time = 10125 ms, elapsed time = 17052 ms.
我想我做到了,感谢你们所有人,再次感谢你们
我创建了这样的索引:
create index idx_MasterTable_Passenger on masterTable(passport, airline)
和我改成这样的查询:
select month(date_hour), max(passenger) as passenger, nationality, passport, airline, count(*) N_Viagens
from masterTable
group by airline, nationality, passport, month(date_hour)
having count(*) > 10
你怎么看,可以接受吗?
SQL Server Execution Times: CPU time = 8362 ms, elapsed time = 5721 ms.
如果老师同意,我会和老师谈谈
老师没有接受这个改变,我们不能改变表或查询,只能创建一个好的索引....建议??