我有一个查询,在超过一分钟后将结果返回给我:
select distinct o.OMS_IDTOPO,e.TYPEQPTL,e.LIBUPR,
o.OMS_SITEA,o.OMS_SITEB,
siteA.Code_Postal as CPA, siteB.Code_Postal as CPB,
siteA.Nom_court as NomA, siteB.Nom_court as NomB,
siteA.Commune as ComA,siteB.Commune as ComB,
siteA.Lat_GPS as latA,siteA.Long_GPS as longA,
siteB.Lat_GPS as latB,siteB.Long_GPS as longB
from occ_wdm_oms as o
left join noeud_dico_traitement as siteA on o.OMS_IDNOEUDA=siteA.Code_DICO
left join noeud_dico_traitement as siteB on o.OMS_IDNOEUDB=siteB.Code_DICO
left join occ_wdm_eqpt as e on o.OMS_IDTOPO=e.OMS_IDTOPO
where o.OMS_IDTOPO NOT LIKE '%.VP%'
and siteA.Lat_GPS != 0 and siteA.Long_GPS != 0
and siteB.Lat_GPS != 0 and siteB.Long_GPS != 0;
但是,如果我做一个像
这样的投影select distinct o.OMS_IDTOPO,e.TYPEQPTL,e.LIBUPR,
o.OMS_SITEA,o.OMS_SITEB,
siteA.Code_Postal as CPA, siteB.Code_Postal as CPB,
siteA.Nom_court as NomA, siteB.Nom_court as NomB,
siteA.Commune as ComA,siteB.Commune as ComB,
siteA.Lat_GPS as latA,siteA.Long_GPS as longA,
siteB.Lat_GPS as latB,siteB.Long_GPS as longB
from occ_wdm_oms as o
left join noeud_dico_traitement as siteA on o.OMS_IDNOEUDA=siteA.Code_DICO
left join noeud_dico_traitement as siteB on o.OMS_IDNOEUDB=siteB.Code_DICO
left join (select distinct LIBUPR,TYPEQPTL,OMS_IDTOPO from occ_wdm_eqpt) as e on o.OMS_IDTOPO=e.OMS_IDTOPO
where o.OMS_IDTOPO NOT LIKE '%.VP%'
and siteA.Lat_GPS != 0 and siteA.Long_GPS != 0
and siteB.Lat_GPS != 0 and siteB.Long_GPS != 0;
只需10秒钟。
我发现了两个类似的问题:Which MySQL JOIN query is more efficient?和Optimizing join in vertica。
答案告诉我通常第一个版本效率更高。
我解释了两个查询,结果看起来像告诉我同样的事情。
我很困惑,因为实际上我通过投影查询会更快地得到结果,如果有什么我错过了吗?