Seteven Wei
Seteven Wei

Reputation: 5

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

this is my query

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 ";

now develop php with phpmyadmin.

Every table has 4 coloumn only

How to make it faster?

Upvotes: 0

Views: 54

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

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.

Upvotes: 1

Related Questions