I have the following query to find topics related by a set of ID's that are not the topic to be viewed:
SELECT
press.image,
press_i18n.title,
press_i18n.slug
FROM press
INNER JOIN press_i18n ON (
press.press_id = press_i18n.press_id
)
WHERE press.ocult_from_site = 'no'
AND press.status = 'active'
AND press_i18n.i18n_id = 'por'
AND (
FIND_IN_SET (1326, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (77, REPLACE(press.tag_id, ";", ","))
)
AND press_i18n.slug != 'bubu-tem-muito-sono'
ORDER by press.publish_date DESC
Showing rows 0 - 7 (8 total, Query took 0.0047 sec)
The query according to the example above is receiving two ID's, but if it gets twenty, it will become a bit repetitive.
In addition, the ,
but is in this example used to find values separated by ;
.
Question
How to optimize the query in order to ensure the correct performance of the query as the table fills up and / or the number of IDs to be located is increasing?
-- ...
AND (
FIND_IN_SET (1326, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (77, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (10545, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (43256, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (1234567, REPLACE(press.tag_id, ";", ","))
OR
FIND_IN_SET (7654321, REPLACE(press.tag_id, ";", ","))
)
-- ...
SQL Fiddle to assist with testing, with minimal structure for example in given.