我的下面查询很慢请告诉我是什么原因

时间:2011-05-26 10:17:26

标签: sql

SELECT cm.course_id, xu.full_path, xu.file_name, xu.file_id, xu.virtual_server
FROM cms_doc.xyf_urls xu
LEFT JOIN bb_bb60.course_main cm ON xu.full_path LIKE '/courses/' ||
cm.course_id OR xu.full_path LIKE '/courses/' || cm.course_id || '%'
WHERE xu.full_path NOT LIKE '/orgs%'
AND xu.full_path NOT LIKE '/institution%'
AND xu.full_path NOT LIKE '/library%'
AND xu.full_path NOT LIKE '/internal%'
AND xu.full_path NOT LIKE '/user%'
ORDER BY cm.course_id 

2 个答案:

答案 0 :(得分:2)

它很慢,因为所有LIKE都是JOIN,特别是{{1}}的一部分。

答案 1 :(得分:0)

首先,确保xu.full_path上有索引。

其次,两个OR条件中的一个似乎是多余的。试试这个:

SELECT cm.course_id, xu.full_path, xu.file_name, xu.file_id, xu.virtual_server
FROM cms_doc.xyf_urls xu
  LEFT JOIN bb_bb60.course_main cm
    ON xu.full_path LIKE '/courses/' || cm.course_id || '%'
WHERE xu.full_path NOT LIKE '/orgs%'
  AND xu.full_path NOT LIKE '/institution%'
  AND xu.full_path NOT LIKE '/library%'
  AND xu.full_path NOT LIKE '/internal%'
  AND xu.full_path NOT LIKE '/user%'
ORDER BY cm.course_id