SQL query with OR gets slow

0

I have a query that does the search using:

WHERE pt.product_name LIKE '%termo da busca%'

Then I want the search to also look for the artist name and put:

WHERE pt.product_name LIKE '%termo da busca%' OR ct.mf_name LIKE '%termo da busca%'

But the search is very slow there, is there any other way to do that? I think one of my problems is because in the query it queries many tables: 8, pq picks the image of one, artist of another, name of another ....

This is the whole query

SELECT pt.virtuemart_product_id, pt.product_name as nome, pt.slug as musica, ct.mf_name, cantor.virtuemart_product_id,
pc.virtuemart_product_id, pc.virtuemart_category_id,
p.virtuemart_product_id, p.product_special,
cpt.virtuemart_category_id, cpt.slug as link,
pm.virtuemart_product_id, pm.virtuemart_media_id,
m.virtuemart_media_id, m.file_url as foto
FROM euk_virtuemart_products_pt_pt pt, euk_virtuemart_product_categories pc, euk_virtuemart_products p, euk_virtuemart_categories_pt_pt cpt, euk_virtuemart_product_medias pm, euk_virtuemart_medias m, euk_virtuemart_manufacturers_pt_pt ct, euk_virtuemart_product_manufacturers cantor
WHERE pt.virtuemart_product_id = pc.virtuemart_product_id
AND pt.virtuemart_product_id = p.virtuemart_product_id
AND pc.virtuemart_category_id = cpt.virtuemart_category_id
AND pm.virtuemart_product_id = pt.virtuemart_product_id
AND m.virtuemart_media_id = pm.virtuemart_media_id
AND ct.virtuemart_manufacturer_id = cantor.virtuemart_product_id
AND p.product_special = 0
AND ct.mf_name LIKE '%quero%' OR pt.product_name LIKE '%quero%'
GROUP BY pt.virtuemart_product_id
ORDER BY p.created_on DESC
LIMIT 30
    
asked by anonymous 24.04.2018 / 17:22

1 answer

2

If you make a UNION you might get better:

(SELECT pt.virtuemart_product_id, pt.product_name as nome, pt.slug as musica, ct.mf_name, cantor.virtuemart_product_id,
pc.virtuemart_product_id, pc.virtuemart_category_id,
p.virtuemart_product_id, p.product_special,
cpt.virtuemart_category_id, cpt.slug as link,
pm.virtuemart_product_id, pm.virtuemart_media_id,
m.virtuemart_media_id, m.file_url as foto
FROM euk_virtuemart_products_pt_pt pt, euk_virtuemart_product_categories pc, euk_virtuemart_products p, euk_virtuemart_categories_pt_pt cpt, euk_virtuemart_product_medias pm, euk_virtuemart_medias m, euk_virtuemart_manufacturers_pt_pt ct, euk_virtuemart_product_manufacturers cantor
WHERE pt.virtuemart_product_id = pc.virtuemart_product_id
AND pt.virtuemart_product_id = p.virtuemart_product_id
AND pc.virtuemart_category_id = cpt.virtuemart_category_id
AND pm.virtuemart_product_id = pt.virtuemart_product_id
AND m.virtuemart_media_id = pm.virtuemart_media_id
AND ct.virtuemart_manufacturer_id = cantor.virtuemart_product_id
AND p.product_special = 0
AND ct.mf_name LIKE '%quero%'
GROUP BY pt.virtuemart_product_id
ORDER BY p.created_on DESC
LIMIT 30)
UNION
(SELECT pt.virtuemart_product_id, pt.product_name as nome, pt.slug as musica, ct.mf_name, cantor.virtuemart_product_id,
pc.virtuemart_product_id, pc.virtuemart_category_id,
p.virtuemart_product_id, p.product_special,
cpt.virtuemart_category_id, cpt.slug as link,
pm.virtuemart_product_id, pm.virtuemart_media_id,
m.virtuemart_media_id, m.file_url as foto
FROM euk_virtuemart_products_pt_pt pt, euk_virtuemart_product_categories pc, euk_virtuemart_products p, euk_virtuemart_categories_pt_pt cpt, euk_virtuemart_product_medias pm, euk_virtuemart_medias m, euk_virtuemart_manufacturers_pt_pt ct, euk_virtuemart_product_manufacturers cantor
WHERE pt.virtuemart_product_id = pc.virtuemart_product_id
AND pt.virtuemart_product_id = p.virtuemart_product_id
AND pc.virtuemart_category_id = cpt.virtuemart_category_id
AND pm.virtuemart_product_id = pt.virtuemart_product_id
AND m.virtuemart_media_id = pm.virtuemart_media_id
AND ct.virtuemart_manufacturer_id = cantor.virtuemart_product_id
AND p.product_special = 0
AND pt.product_name LIKE '%quero%'
GROUP BY pt.virtuemart_product_id
ORDER BY p.created_on DESC
LIMIT 30)
    
24.04.2018 / 17:32