SQL query with OR gets slow

Asked

Viewed 20 times

0

I have an appointment that does the search using:

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

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

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

But then the search gets really slow, is there any other way to do that? I think one of my problems is because in the query it consults many tables: 8, because it takes the image of one, artist of another, name of another....

This is the whole consultation

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

1 answer

2


If you make a UNION may improve:

(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)
  • perfect worked, just so I understand, what you did was duplicate the consultations and in a search for music and the other for the artist and then unite?

  • Exactly that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.