[英]SQL query taking too long (LEFT OUTER 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
查詢已更新:
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
您可以與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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.