使用5个连接提高查询性能

时间:2017-01-27 10:00:52

标签: mysql sql performance innodb database-performance

使用本地java程序我可以访问vps上的数据库,其中涉及的表平均有大约1,500条记录。

不幸的是,尽管记录并不多,但我提取的性能问题大约为10秒。

查询如下:

SELECT  DISTINCT P.advanced_stock_management,
        PA.id_product, PA.reference, PL.name,
        round(P.wholesale_price,2),
        round((P.price + P.price * 22 / 100), 2),
        round(SP.reduction,2),
        SA.quantity, PQ.is_true
    FROM  db.ps_product AS P
    INNER JOIN  db.ps_product_attribute AS PA
          ON P.id_product=PA.id_product
    INNER JOIN  db.ps_product_lang AS PL 
          ON PA.id_product=PL.id_product
    INNER JOIN  db.ps_stock_available AS SA
          ON SA.id_product_attribute=PA.id_product_attribute
    LEFT OUTER JOIN  db.ps_product_quantity_real AS PQ
          ON PA.id_product=PQ.id_product
         AND  PA.reference=PQ.reference
    LEFT OUTER JOIN  db.ps_specific_price AS SP
          ON PA.id_product=SP.id_product
    WHERE  P.active = 1;

如何改进查询结构并提高性能?

提前致谢。

3 个答案:

答案 0 :(得分:0)

您应该在以下列中拥有适当的索引:

table_name (column_name)
ps_product (id_product)
ps_product_attribute (id_product)
ps_product_attribute (reference) 
ps_product_attribute (id_product_attribute)
ps_product_lang (id_product)
ps_stock_available (id_product_attribute)
ps_product_quantity_real (id_product)
ps_product_quantity_real (reference)
ps_specific_price (id_product)

答案 1 :(得分:0)

通过适当的缩进重写查询:

SELECT DISTINCT 
    product.advanced_stock_management, 
    attribute.id_product, 
    attribute.reference, 
    lang.name, 
    round(product.wholesale_price,2), 
    round((product.price + product.price * 22 / 100),2), 
    round(price.reduction,2), 
    availablity.quantity, 
    quantity.is_true
FROM db.ps_product AS product
    INNER JOIN db.ps_product_attribute AS attribute 
        ON product.id_product = attribute.id_product
            INNER JOIN db.ps_product_lang AS lang 
                ON lang.id_product = attribute.id_product
            INNER JOIN db.ps_stock_available AS availablity 
                ON availablity.id_product_attribute = attribute.id_product_attribute
            LEFT OUTER JOIN db.ps_product_quantity_real AS quantity 
                ON attribute.id_product = quantity.id_product 
                AND attribute.reference = quantity.reference
            LEFT OUTER JOIN db.ps_specific_price AS price 
                ON price.id_product = attribute.id_product
WHERE product.active = 1;

我们可以看到中央表实际上是ps_product_attribute。让它成为查询的开始:

SELECT DISTINCT 
    product.advanced_stock_management, 
    attribute.id_product, 
    attribute.reference, 
    lang.name, 
    round(product.wholesale_price,2), 
    round((product.price + product.price * 22 / 100),2), 
    round(price.reduction,2), 
    availablity.quantity, 
    quantity.is_true
FROM db.ps_product_attribute AS attribute
    INNER JOIN db.ps_product AS product
        ON attribute.id_product = product.id_product
    INNER JOIN db.ps_product_lang AS lang 
        ON attribute.id_product = lang.id_product 
    INNER JOIN db.ps_stock_available AS availablity 
        ON attribute.id_product_attribute = availablity.id_product_attribute
    LEFT OUTER JOIN db.ps_product_quantity_real AS quantity 
        ON attribute.id_product = quantity.id_product 
        AND attribute.reference = quantity.reference
    LEFT OUTER JOIN db.ps_specific_price AS price 
        ON attribute.id_product = price.id_product
WHERE product.active = 1;

现在查询看起来非常好。
您是否在此查询中的每个字段上都有索引?如果没有,你应该!

ALTER TABLE `ps_product_attribute` ADD INDEX `id_product` (`id_product`)
ALTER TABLE `ps_product_attribute` ADD INDEX `reference` (`reference`)
ALTER TABLE `ps_product` ADD INDEX `id_product` (`id_product`)
ALTER TABLE `ps_product_lang` ADD INDEX `id_product` (`id_product`)
ALTER TABLE `ps_stock_available` ADD INDEX `id_product_attribute` (`id_product_attribute`)
ALTER TABLE `ps_product_quantity_real` ADD INDEX `id_product` (`id_product`)
ALTER TABLE `ps_product_quantity_real` ADD INDEX `reference` (`reference`)
ALTER TABLE `ps_specific_price` ADD INDEX `id_product` (`id_product`)

使用此数据库大小,查询应在1秒内运行。

答案 2 :(得分:-1)

你有两个选择

  • 在内存中预取1500条记录,然后使用记录匹配(无数据库)加入。预取提供了从数据库访问记录的速度和移除的网络延迟,但是记录匹配将是缓慢和令人厌烦的。

  • 将apache spark与本地群集( bigdata join )一起使用。我亲自在9ms内执行了100万* 2的加入。这必须仅使用内存中RDD来实现。