Need Help for Optimizing MySQL query with few joins

六眼飞鱼酱① 提交于 2019-12-12 06:49:40

问题


Hello I have the follow sql query it works but it is kind of slow

SELECT 
    ep . *,
    t_u.sUsername AS sLockedBy,
    epi.sName,
    em.sName AS sManufacturerName,
    SUM(IF(eop.dInProduction IS NULL
            AND eop.dFromProduction IS NULL
            AND eop.dShipped IS NULL
            AND eop.nIsCancelled = 0
            AND eos.sStatusCode NOT IN ('Canceled' , 'Finished', 'Complete'),
        1,
        0)) AS nProductOrdered,
    SUM(IF(eop.dInProduction IS NOT NULL
            AND eop.dFromProduction IS NULL
            AND eop.dShipped IS NULL
            AND eop.nIsCancelled = 0
            AND eos.sStatusCode NOT IN ('Canceled' , 'Finished', 'Complete'),
        1,
        0)) AS nProductInProduction,
    SUM(IF(eop.dInProduction IS NOT NULL
            AND eop.dFromProduction IS NOT NULL
            AND eop.dShipped IS NULL
            AND eop.nIsCancelled = 0
            AND eos.sStatusCode NOT IN ('Canceled' , 'Finished', 'Complete'),
        1,
        0)) AS nProductFromProduction,
    SUM(IF(eop.dInProduction IS NULL
            AND eop.dFromProduction IS NULL
            AND eop.dShipped IS NULL
            AND eop.nIsCancelled = 0
            AND eos.sStatusCode NOT IN ('Canceled' , 'Finished', 'Complete'),
        1,
        0)) + ep.nInStock AS nStockCheck
FROM
    eshop3_products AS ep
        LEFT JOIN
    users AS t_u ON ep.nUserLockID = t_u.nUID
        AND t_u.nDeleted = 0
        INNER JOIN
    eshop3_products_i18n AS epi ON ep.nUID = epi.nProductID
        AND epi.nLangID = 1
        AND epi.nDeleted = 0
        LEFT JOIN
    eshop3_manufacturers AS em ON ep.nManufacturerID = em.nUID
        AND em.nDeleted = 0
        LEFT JOIN
    eshop3_order_products AS eop ON ep.nUID = eop.nProductID
        LEFT JOIN
    eshop3_orders AS eo ON eop.nOrderID = eo.nUID
        LEFT JOIN
    eshop3_order_statuses AS eos ON eo.nUID = eos.nOrderID
        AND eos.nUID = (SELECT 
            nUID
        FROM
            eshop3_order_statuses as eos
        WHERE
            eo.nUID = eos.nOrderID
        ORDER BY eos.nUID DESC
        LIMIT 1)
WHERE
    (ep.nDeleted = 0)
GROUP BY ep.nUID
ORDER BY ep.sStoreCode ASC

The Product Table has total 462 rows,

The Order Statuses Tables has total 17 154 rows

The execution time of the query is 1.5 secs which I think it is slow.

I use the INNER SELECT in the ON clause to get the last status for the given order, and here I think there is something wrong.

WHen I remove the order statuses table from the whole query the execution time is several times faster 0.152 secs

Could you give me some advice how can I optimize the query I have

Here you go the sql with explain

id  select_type        table type   possible_keys    key      key_len ref                            rows  Extra
1   PRIMARY            epi   ALL    nProductID                                                        408  Using where; Using temporary; Using filesort
1   PRIMARY            ep    eq_ref PRIMARY,nUID     PRIMARY        4 dragoni_zfms.epi.nProductID       1  Using where
1   PRIMARY            em    eq_ref PRIMARY          PRIMARY        4 dragoni_zfms.ep.nManufacturerID   1 
1   PRIMARY            t_u   eq_ref PRIMARY          PRIMARY        4 dragoni_zfms.ep.nUserLockID       1 
1   PRIMARY            eop   ref    id_productid     id_productid   5 dragoni_zfms.ep.nUID             13 
1   PRIMARY            eo    eq_ref PRIMARY          PRIMARY        4 dragoni_zfms.eop.nOrderID         1  Using index
1   PRIMARY            eos   eq_ref PRIMARY,nOrderID PRIMARY        4 func                              1  
2   DEPENDENT SUBQUERY eos   ref    nOrderID         nOrderID       4 dragoni_zfms.eo.nUID              1  Using where; Using index; Using filesort

回答1:


In your case it is easier to perform some operations not using mysql but using php. I have seen in my practice cases when you win time. Please, try to divide a complex query into several simple queries. Then perform the necessary filtering using php scripts. Hope it helps. Thanks!



来源:https://stackoverflow.com/questions/28697569/need-help-for-optimizing-mysql-query-with-few-joins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!