[英]Improve query performance with 5 join
使用本地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;
如何改善查询的结构并提高性能?
提前致谢。
您应该在以下列上使用适当的索引:
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)
通过使用适当的缩进重写查询:
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秒内运行。
你有两个选择
在内存中预取1500条记录,然后使用记录匹配( 无数据库 )进行联接。 在从数据库访问记录时,预取提供了速度并消除了网络延迟,但是Ya记录匹配将非常缓慢且令人厌烦。
在本地集群中使用apache spark( bigdata joins )。 我已经在9ms内亲自执行了100万* 2次加入。 必须仅使用内存内RDD
来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.