3
I have the following query to find related topics 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 as the example above is receiving two ID’s, but if you receive twenty, it will become a little repetitive.
In addition, the function FIND_IN_SET() was designed to work with ,
but is being used in this example to locate values separated by ;
.
Question
How to optimize the query to ensure the correct performance of the same as the table fills and/ or the number of ID’s to locate increases?
-- ...
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 help test, with the minimum structure for the given example.
It is worth to touch the structure of the bank, or only the query?
– bfavaretto
Anything goes, optimized structure and/or optimized query, the important thing is to ensure the performance over time. @bfavaretto
– Zuul