从mysql慢速日志查询

时间:2017-08-17 06:51:47

标签: mysql relational-database prestashop rds infrastructure

我们使用Prestashop作为电子商务应用程序,对于移动应用程序,我们使用相同的数据库和Prestashop架构,几乎使用来自核心PrestaShop的相同查询。

对于我们在NODE Js API中使用的一些查询,当流量激增时,我们将RDS(MySQL)CPU峰值提高到100%。

RDS配置:db.m4.xlarg,4vCPU,16 GB RAM

慢查询(此查询仅在流量较高时出现在慢速查询日志中):

    EXPLAIN SELECT t1.id_product,
    t1.position,
    t1.price,
    t1.quantity,
    t1.reserve_stock,
    t1.name,
    t1.link_rewrite hyphen_name,
    t1.id_category_default,
    t1.id_sub_category,
    t1.is_back_in_stock,
    (SELECT link_rewrite FROM pml_category_lang WHERE id_category = 36 AND id_shop = 1 AND id_lang = 1 LIMIT 1) category_link_rewrite,
    CONCAT('[', t1.images, ']') images,
    t3.reduction,
    t3.reduction_type,
    IF(t3.reduction,
    IF(t3.reduction_type = 'percentage', ROUND((t1.price - (t3.reduction * t1.price))),
    ROUND(t1.price - t3.reduction)),
    t1.price) discounted_price
    FROM
    (SELECT ps.id_product, ps.id_category_default, ps.is_back_in_stock, cp.position, ROUND ((7/100) * ps.price * 1 + ps.price * 1) price,
    sa.quantity, sa.reserve_stock, pl.name, p.date_add, pl.link_rewrite,
    (SELECT pml_category.id_category FROM pml_category
    LEFT JOIN pml_category_product ON pml_category_product.id_category = pml_category.id_category
    AND pml_category_product.id_shop = 1
    WHERE id_parent = 36 AND pml_category_product.id_category IS NOT NULL AND pml_category_product.id_product = cp.id_product LIMIT 1) AS id_sub_category,
    GROUP_CONCAT(DISTINCT CONCAT('{"id_image":', i.id_image, ',', '"position":', i.position, ',', '"cover":', ims.cover, '}')) images
    FROM pml_category_product cp
    JOIN pml_product_shop ps
    ON cp.id_product = ps.id_product AND cp.id_shop = ps.id_shop 
    JOIN pml_product p
    ON p.id_product = cp.id_product
    AND p.id_category_default = 36
    JOIN pml_stock_available sa
    ON cp.id_product = sa.id_product AND cp.id_shop = sa.id_shop AND sa.id_product_attribute = 0 AND sa.quantity > 0 
    JOIN pml_product_lang pl
    ON cp.id_product = pl.id_product AND cp.id_shop = pl.id_shop AND pl.id_lang = 1
    JOIN pml_image i
    ON cp.id_product = i.id_product
    JOIN pml_image_shop ims
    ON ims.id_image = i.id_image AND ims.id_shop = 1
    WHERE ps.id_shop = 1
    AND ps.active = 1 AND i.smartly !=1
    AND ps.visibility = 'both'
    GROUP BY cp.id_product) t1 join pml_pomelo_rank pr on t1.id_product = pr.id_product and pr.id_shop = 1 and (pr.alltime_regular_cr > 2 or pr.alltime_qty_sold > 100) and pr.id_product != 14930 and pr.id_product not in (select id_product_2 from pml_accessory where id_product_1 = 14930) 
    LEFT JOIN
    (SELECT t2.* FROM
    (SELECT id_product, id_specific_price_rule, reduction, reduction_type, `from`, `to`
    FROM pml_specific_price
    WHERE id_shop IN (0, 1) and id_currency IN (0,1)
    AND ((`from` = '0000-00-00 00:00:00' OR '2017-08-15 13:15:33' >= `from`) AND (`to` = '0000-00-00 00:00:00' OR '2017-08-15 13:15:33' <= `to`))
    ORDER BY id_product ASC, id_specific_price_rule ASC) t2
    GROUP BY t2.id_product) t3
    ON t1.id_product = t3.id_product ORDER BY rand() LIMIT 0, 4;

目前我们正试图找出增加RDS基础架构大小或查询是否合适,这需要改进。

注意:这与Prestashop核心用于类别页面的查询完全相同

有关此类查询的查询优化或基础架构RDS(配置)优化的任何建议或帮助都会有所帮助。

RDS图: RDS graph spikes are correlated with traffic

EXPLAIN output for the query

0 个答案:

没有答案