Seteven Wei Seteven Wei - 1 month ago 5
MySQL Question

How too speed up a UNION TABLE query on milion record mysql?

this is my query now

SELECT *
FROM (
SELECT simple_sku, picture_url, product_name, discounted_price, tracking_link, 'Lazada.co.id' AS table_identity
FROM `list_lazada`
UNION
SELECT id_product, picture_url, product_name, sale_price, tracking_link, 'Simulation.com' AS table_identity
FROM `list_simulation`
) AS big_table
WHERE product_name LIKE '%$search%' ORDER BY big_table.discounted_price ASC
LIMIT 12 ";


develop php with phpmyadmin.

every table has 4 coloumn only

how to make it faster? anyone help

Answer

You don't need the outer subquery:

SELECT simple_sku, picture_url, product_name, discounted_price, tracking_link,
       'Lazada.co.id' AS table_identity
FROM `list_lazada`
UNION
SELECT id_product, picture_url, product_name, sale_price, tracking_link,
       'Simulation.com' AS table_identity
FROM `list_simulation`
WHERE product_name LIKE '%$search%'
ORDER BY discounted_price ASC
LIMIT 12

I think your LIKE condition will render an index on the product_name column not usable, resulting in a full table scan. But maybe you can improve your search approach to take advantage of an index.

Comments