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.

2 Answers 2

2

OR is a logical operator. It does not define alternatives for LIKE. You need to split that out into two LIKE clauses.

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

If that is still slow, add your table structure and the output of an EXPLAIN to your question.

Sign up to request clarification or add additional context in comments.

Comments

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
;

Using UNION ALL instead of OR improves performance

1 Comment

I can't imagine that's faster than doing an OR in the WHERE clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.