Question

Need help speeding up this query. When only one article is in p.article IN ('200101') the query runs in 0.0006 sec. If two articles are in p.article IN ('200101','200102') the query runs in 0.6414 sec.

Since I need to run this query on about 3000 items I would like to speed it up a little bit. I think the solution is in INNER JOIN, but haven't been able to figure it out myself.

SELECT p.article, (
    SELECT s.final_item
    FROM  `structure` s
    WHERE s.item = p.article
    ORDER BY s.level DESC 
    LIMIT 1
) AS final_item
FROM products p
WHERE p.article IN ('200101','200102')
Was it helpful?

Solution

First, you should have indexes for article in table products and for item in table structure.

If you always have at least one row in structure for a given product, you could give this a try:

SELECT s.final_item, p.article
    FROM  `structure` s INNER JOIN products p ON s.item = p.article
    WHERE s.article IN ('200101','200102')
    ORDER BY s.level DESC 
    LIMIT 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top