[英]Speeding Up a slow MySQL SubQuery Update
I have the following UPDATE script which keeps a count of the active products on my site so I can quickly reference if the category has products or not without doing this count on the front end. 我有以下UPDATE脚本,该脚本可对我的站点上的活动产品进行计数,因此我可以快速参考类别中是否有产品,而无需在前端进行此计数。
UPDATE category_to_store
SET products = (
SELECT COUNT(*)
FROM product p
LEFT JOIN product_to_category p2c
ON (p.product_id = p2c.product_id)
LEFT JOIN product_to_store p2s
ON (p.product_id = p2s.product_id)
WHERE p.status = '1'
AND p.date_available <= NOW()
AND p2c.category_id = category_to_store.category_id
AND p2s.store_id = category_to_store.store_id
);
My tables used are explained below: 我使用的表格说明如下:
DESCRIBE category_to_store;
Field Type Null Key Default Extra
---------------+---------------+-------+-------+-------+---------------
category_id int(11) NO PRI
store_id int(11) NO PRI
products int(11) NO 0
DESCRIBE product;
Field Type Null Key Default Extra
---------------+---------------+-------+-------+-------+---------------
product_id int(11) NO PRI auto_increment
~
date_available date NO
~
status tinyint(1) NO 0
~
DESCRIBE product_to_category;
Field Type Null Key Default Extra
---------------+---------------+-------+-------+-------+---------------
product_id int(11) NO PRI
category_id int(11) NO PRI
DESCRIBE product_to_store;
Field Type Null Key Default Extra
---------------+---------------+-------+-------+-------+---------------
product_id int(11) NO PRI
store_id int(11) NO PRI 0
( product
table has fields I've not included that aren't being used) ( product
表中有我尚未包括但尚未使用的字段)
This currently runs correctly but it takes 110 seconds currently. 目前,该程序可以正常运行,但目前需要110秒。
I have set the site up to use a WHERE category_to_store.category_id = '(int)'
to limit the query but this means working out which categories may have been affected with the update which works I guess but I was wondering if any of you lovely geniuses have any better solutions I have missed? 我已将网站设置为使用WHERE category_to_store.category_id = '(int)'
来限制查询,但这意味着找出可能会受到更新影响的类别,我猜这是可行的,但我想知道你们中的任何一个是否可爱天才有我错过的更好的解决方案?
Thank you in advance. 先感谢您。
If you are updating a lot of rows, it might be more efficient to do a join, rather than using a "nested loops" operation. 如果要更新很多行,则执行连接可能比使用“嵌套循环”操作更有效。
UPDATE category_to_store cts
LEFT
JOIN (
SELECT COUNT(*) AS cnt_
, p2c.category_id
, p2s.store_id
FROM product p
LEFT
JOIN product_to_category p2c
ON (p.product_id = p2c.product_id)
LEFT
JOIN product_to_store p2s
ON (p.product_id = p2s.product_id)
WHERE p.status = '1'
AND p.date_available <= NOW()
GROUP
BY p2c.category_id
, p2s.store_id
) c
ON c.category_id = cts.category_id
AND c.store_id = cts.store_id
SET cts.products = IFNULL(c.cnt_,0)
For optimum performance, you want suitable indexes available, you might want to consider adding an index eg 为了获得最佳性能,您需要合适的索引,可以考虑添加索引,例如
... ON product (product_id, status, date_available)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.