0
SELECT
*

FROM
pages


INNER JOIN cms_collection ON cms_collection.collection_id LIKE 

( 
   CONCAT('%', pages.page_id, '/heading%') OR
   CONCAT('%', pages.page_id, '/content%') 
)

WHERE
site_id = 51

LIMIT 10

The stacked CONCATs are causing the query to be super slow, is there anyway to optimize this? And actually it seems like this statement isn't working as expected.. I want to do a LIKE based on if the collection_id is '%pages.page_id/heading%' OR '%pages.page_id/content%'.

This statement works fine if just the first CONCAT exists.

4

2 回答 2

2

OR是一个逻辑运算符。它没有定义LIKE. 您需要将其拆分为两个LIKE子句。

SELECT * FROM pages
INNER JOIN cms_collection ON
(cms_collection.collection_id LIKE CONCAT('%', pages.page_id, '/heading%')) OR
(cms_collection.collection_id LIKE CONCAT('%', pages.page_id, '/content%'))
WHERE site_id = 51 LIMIT 10

如果这仍然很慢,请将您的表结构和 an 的输出添加EXPLAIN到您的问题中。

于 2013-06-11T20:37:43.907 回答
0
SELECT * FROM pages
INNER JOIN cms_collection ON
(cms_collection.collection_id LIKE CONCAT('%', pages.page_id, '/heading%'))
WHERE site_id = 51

UNION ALL

SELECT * FROM pages
INNER JOIN cms_collection ON
(cms_collection.collection_id LIKE CONCAT('%', pages.page_id, '/content%'))
WHERE site_id = 51 
LIMIT 10
;

使用 UNION ALL 代替 OR 可以提高性能

于 2013-08-23T13:04:20.137 回答