SQL查询耗时太长(LEFT OUTER JOIN)

时间:2015-04-03 07:13:06

标签: mysql join left-join

我一直在测试搜索的SQL查询,建议从列'舰队','旅行','预订','代理'和'人'中获取数据。

问题是此查询花费的时间太长(2分钟)才能完成。 我尝试使用'WHERE'和/或'INNER JOIN',查询时间减少到几毫秒,但它并没有给我所有我想要的结果。例如,“旅行”中有一些条目没有“预订”关联,或者某些“预订”条目没有“人”关联。这就是我需要使用LEFT OUTER JOIN的原因,但这使查询变得非常慢。

我相信我在这种语法中有一些效率问题,但我无法弄清楚它是什么。

我希望结果如下:

bid     tid     tstatus    timestamp    start   end   gname pname   fname       blockedtrips  tcreator  bcreator    baname  taname
NULL    2736    1          1468022400   540     720   NULL  NULL    Little Boy  1             9         NULL        NULL    Conscience Dauphin
NULL    2737    1          1468022400   540     720   NULL  NULL    Moby Dick   1             9         NULL        NULL    Conscience Dauphin

我的查询如下:

SELECT 
 bookings.id AS bid, trips.id AS tid, trips.id_status AS tstatus,
 trips.timestamp, trips.start, trips.end, bookings.groupname AS gname,
 persons.name AS pname, fleet.name AS fname, trips.blockedtrips AS blockedtrips,
 trips.id_creator AS tcreator, bookings.id_creator AS bcreator,
 (SELECT name FROM agencies WHERE deleted=false AND id=bookings.id_agency) AS baname,
 (SELECT name FROM agencies WHERE deleted=false AND id=trips.id_agency) AS taname
FROM
 trips OUTER LEFT JOIN fleet ON fleet.id = trips.id_fleet AND fleet.deleted=false AND trips.deleted=false
 LEFT OUTER JOIN bookings ON trips.id = bookings.id_trip AND bookings.deleted=false
 LEFT OUTER JOIN persons ON bookings.id = persons.id_booking AND persons.deleted=false
  AND ( 
   (SELECT name FROM agencies WHERE deleted=false AND id=bookings.id_agency) LIKE '%conscience%'
   OR (SELECT name FROM agencies WHERE deleted=false AND id=trips.id_agency) LIKE '%conscience%'
   OR bookings.groupname LIKE '%conscience%'
   OR persons.name LIKE '%conscience%'
   OR fleet.name LIKE '%conscience%'
  )
  ORDER BY trips.timestamp
  DESC LIMIT 0,100

QUERY UPDATED:

SELECT 
     bookings.id AS bid, trips.id AS tid, trips.id_status AS tstatus,
     trips.timestamp, trips.start, trips.end, bookings.groupname AS gname, 
     persons.name AS pname, fleet.name AS fname, trips.blockedtrips AS blockedtrips, 
     trips.id_creator AS tcreator, bookings.id_creator AS bcreator, 
     bagency.name AS baname, tagency.name AS taname 
FROM fleet 
     LEFT JOIN trips ON fleet.id = trips.id_fleet AND fleet.deleted=false AND trips.deleted=false
     LEFT JOIN agencies AS tagency ON tagency.id = trips.id_agency AND tagency.deleted=false
     LEFT JOIN bookings ON trips.id = bookings.id_trip AND bookings.deleted=false
     LEFT JOIN agencies AS bagency ON bagency.id = bookings.id_agency AND bagency.deleted=false  
     LEFT JOIN persons ON bookings.id = persons.id_booking AND persons.deleted=false
WHERE
     bagency.name LIKE '%conscience%' 
     OR tagency.name LIKE '%conscience%' 
     OR bookings.groupname LIKE '%conscience%' 
     OR persons.name LIKE '%conscience%' 
     OR fleet.name LIKE '%conscience%'
ORDER BY trips.timestamp DESC 
LIMIT 0,100

1 个答案:

答案 0 :(得分:0)

您可以使用agencies加入表格而不是使用多个子查询,这应该会给您相同的结果,尝试希望它有用:

SELECT 
 bookings.id AS bid, trips.id AS tid, trips.id_status AS tstatus,
 trips.timestamp, trips.start, trips.end, bookings.groupname AS gname,
 persons.name AS pname, fleet.name AS fname, trips.blockedtrips AS blockedtrips,
 trips.id_creator AS tcreator, bookings.id_creator AS bcreator,
 ag1.name AS baname,
 ag2.name AS taname
FROM
 trips OUTER LEFT JOIN fleet ON fleet.id = trips.id_fleet AND fleet.deleted=false AND trips.deleted=false
 LEFT OUTER JOIN bookings ON trips.id = bookings.id_trip AND bookings.deleted=false
 LEFT OUTER JOIN persons ON bookings.id = persons.id_booking AND persons.deleted=false
 left join agencies ag1 on ag1.id=bookings.id_agency and ag1.deleted=false and ag1.name  LIKE '%conscience%'
 left join agencies ag2 on ag2.id=trips.id_agency ag2.deleted=false
  where 
   ag1.name LIKE '%conscience%'
   OR ag2.name  LIKE '%conscience%'
   OR bookings.groupname LIKE '%conscience%'
   OR persons.name LIKE '%conscience%'
   OR fleet.name LIKE '%conscience%'
  ORDER BY trips.timestamp
  DESC LIMIT 0,100