更新查询很慢

时间:2019-04-18 19:09:32

标签: mysql sql

如何使这些更新查询更快,我不是sql专家

完成了+6个小时,我的服务器是Amazon RDS 2 GB RAM,2个cpu内核和高速AWS SSD。

产品表包含123134行

orders表包含15135187行

表越大,查询完成所需的时间就越慢

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrders` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrders` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrders` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdres` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('US','UK','AU','CA') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersTop4` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('US','UK','AU','CA') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersTop4` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('US','UK','AU','CA') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersTop4` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('US','UK','AU','CA') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresTop4` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('US','UK','AU','CA') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeTop4Orders` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('US','MX','CA') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersUSMCA` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('US','MX','CA') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersUSMCA` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('US','MX','CA') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersUSMCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('US','MX','CA') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresUSMCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('US','MX','CA') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresUSMCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('US','CA') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersNA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('US','CA') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersNA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('US','CA') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersNA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('US','CA') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresNA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('US','CA') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimesOrdresNA` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode = 'US' GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersUS` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode = 'US' GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersUS` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode = 'US' GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersUS` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode = 'US' GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresUS` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode = 'US' GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresUS` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode = 'UK' GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersUK` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode = 'UK' GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersUK` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode = 'UK' GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersUK` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode = 'UK' GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresUK` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode = 'UK' GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresUK` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode = 'CA' GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode = 'CA' GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode = 'CA' GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode = 'CA' GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode = 'CA' GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresCA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode = 'AU' GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersAU` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode = 'AU' GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersAU` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode = 'AU' GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersAU` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode = 'AU' GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresAU` = filterd_orders;




UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode = 'AU' GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresAU` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PL','PT','RO','SK','SI','ES','SE') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersEEA` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PL','PT','RO','SK','SI','ES','SE') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersEEA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PL','PT','RO','SK','SI','ES','SE') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersEEA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PL','PT','RO','SK','SI','ES','SE') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresEEA` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PL','PT','RO','SK','SI','ES','SE') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresEEA` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('AT','BE','CY','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PT','SK','SI','ES') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersEA` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('AT','BE','CY','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PT','SK','SI','ES') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersEA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('AT','BE','CY','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PT','SK','SI','ES') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersEA` = filterd_orders;

UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('AT','BE','CY','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PT','SK','SI','ES') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresEA` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('AT','BE','CY','DK','EE','FI','FR','DE','GR','HU','HU','IS','IE','IT','LV','LI','LT','LU','MT','N','NO','PT','SK','SI','ES') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresEA` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND t4.countryCode IN ('BE','BG','CZ','DK','DE','EE','IE','EL','ES','FR','HR','IT','CY','LV','LT','LU','HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','UK') GROUP by productId) o
ON p.productID = o.productId
SET `dailyOrdersEU` = filterd_orders;


UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND t4.countryCode IN ('BE','BG','CZ','DK','DE','EE','IE','EL','ES','FR','HR','IT','CY','LV','LT','LU','HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','UK') GROUP by productId) o
ON p.productID = o.productId
SET `weeklyOrdersEU` = filterd_orders;




UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND t4.countryCode IN ('BE','BG','CZ','DK','DE','EE','IE','EL','ES','FR','HR','IT','CY','LV','LT','LU','HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','UK') GROUP by productId) o
ON p.productID = o.productId
SET `monthlyOrdersEU` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.date > DATE_SUB(CURDATE(), INTERVAL 90 DAY) AND t4.countryCode IN ('BE','BG','CZ','DK','DE','EE','IE','EL','ES','FR','HR','IT','CY','LV','LT','LU','HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','UK') GROUP by productId) o
ON p.productID = o.productId
SET `nintyDaysOrdresEU` = filterd_orders;



UPDATE `products` p
INNER JOIN
(select count(id) AS filterd_orders,productId from orders t4 WHERE t4.countryCode IN ('BE','BG','CZ','DK','DE','EE','IE','EL','ES','FR','HR','IT','CY','LV','LT','LU','HU','MT','NL','AT','PL','PT','RO','SI','SK','FI','SE','UK') GROUP by productId) o
ON p.productID = o.productId
SET `AllTimeOrdresEU` = filterd_orders;

产品表

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `productId` bigint(20) NOT NULL,
 `detailUrl` text CHARACTER SET utf32 NOT NULL,
 `belongToDSStore` int(11) NOT NULL,
 `promPCPriceStr` float NOT NULL DEFAULT '-1',
 `oriPriceStr` float NOT NULL DEFAULT '-1',
 `orderCnt` int(11) NOT NULL,
 `productTitle` text CHARACTER SET utf32 NOT NULL,
 `productImgUrl` text CHARACTER SET utf32 NOT NULL,
 `created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `cat` bigint(20) NOT NULL DEFAULT '-1',
 `dailyOrders` int(11) NOT NULL DEFAULT '0',
 `weeklyOrders` int(11) NOT NULL DEFAULT '0',
 `monthlyOrders` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdres` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersTop4` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersTop4` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersTop4` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresTop4` int(11) NOT NULL DEFAULT '0',
 `AllTimeTop4Orders` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersEEA` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersEEA` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersEEA` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresEEA` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresEEA` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersUSMCA` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersUSMCA` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersUSMCA` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresUSMCA` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresUSMCA` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersEA` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersEA` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersEA` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresEA` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresEA` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersEU` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersEU` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersEU` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresEU` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresEU` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersNA` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersNA` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersNA` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresNA` int(11) NOT NULL DEFAULT '0',
 `AllTimesOrdresNA` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersUS` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersUS` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersUS` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresUS` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresUS` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersUK` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersUK` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersUK` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresUK` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresUK` int(11) DEFAULT '0',
 `dailyOrdersCA` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersCA` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersCA` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresCA` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresCA` int(11) NOT NULL DEFAULT '0',
 `dailyOrdersAU` int(11) NOT NULL DEFAULT '0',
 `weeklyOrdersAU` int(11) NOT NULL DEFAULT '0',
 `monthlyOrdersAU` int(11) NOT NULL DEFAULT '0',
 `nintyDaysOrdresAU` int(11) NOT NULL DEFAULT '0',
 `AllTimeOrdresAU` int(11) NOT NULL DEFAULT '0',
 `isEditorPick` int(11) NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `productId` (`productId`),
 KEY `orderCnt` (`orderCnt`),
 KEY `cat` (`cat`),
 KEY `promPCPriceStr` (`promPCPriceStr`),
 KEY `dailyOrders` (`dailyOrders`),
 KEY `weeklyOrders` (`weeklyOrders`),
 KEY `monthlyOrders` (`monthlyOrders`),
 KEY `nintyDaysOrdres` (`nintyDaysOrdres`),
 KEY `dailyOrdersTop4` (`dailyOrdersTop4`),
 KEY `weeklyOrdersTop4` (`weeklyOrdersTop4`),
 KEY `monthlyOrdersTop4` (`monthlyOrdersTop4`),
 KEY `nintyDaysOrdresTop4` (`nintyDaysOrdresTop4`),
 KEY `dailyOrdersEEA` (`dailyOrdersEEA`),
 KEY `weeklyOrdersEEA` (`weeklyOrdersEEA`),
 KEY `monthlyOrdersEEA` (`monthlyOrdersEEA`),
 KEY `nintyDaysOrdresEEA` (`nintyDaysOrdresEEA`),
 KEY `dailyOrdersUSMCA` (`dailyOrdersUSMCA`),
 KEY `weeklyOrdersUSMCA` (`weeklyOrdersUSMCA`),
 KEY `monthlyOrdersUSMCA` (`monthlyOrdersUSMCA`),
 KEY `nintyDaysOrdresUSMCA` (`nintyDaysOrdresUSMCA`),
 KEY `dailyOrdersEA` (`dailyOrdersEA`),
 KEY `weeklyOrdersEA` (`weeklyOrdersEA`),
 KEY `monthlyOrdersEA` (`monthlyOrdersEA`),
 KEY `nintyDaysOrdresEA` (`nintyDaysOrdresEA`),
 KEY `dailyOrdersEU` (`dailyOrdersEU`),
 KEY `weeklyOrdersEU` (`weeklyOrdersEU`),
 KEY `monthlyOrdersEU` (`monthlyOrdersEU`),
 KEY `nintyDaysOrdresEU` (`nintyDaysOrdresEU`),
 KEY `dailyOrdersNA` (`dailyOrdersNA`),
 KEY `weeklyOrdersNA` (`weeklyOrdersNA`),
 KEY `monthlyOrdersNA` (`monthlyOrdersNA`),
 KEY `nintyDaysOrdresNA` (`nintyDaysOrdresNA`),
 KEY `dailyOrdersUS` (`dailyOrdersUS`),
 KEY `weeklyOrdersUS` (`weeklyOrdersUS`),
 KEY `monthlyOrdersUS` (`monthlyOrdersUS`),
 KEY `nintyDaysOrdresUS` (`nintyDaysOrdresUS`),
 KEY `dailyOrdersUK` (`dailyOrdersUK`),
 KEY `weeklyOrdersUK` (`weeklyOrdersUK`),
 KEY `monthlyOrdersUK` (`monthlyOrdersUK`),
 KEY `nintyDaysOrdresUK` (`nintyDaysOrdresUK`),
 KEY `dailyOrdersCA` (`dailyOrdersCA`),
 KEY `weeklyOrdersCA` (`weeklyOrdersCA`),
 KEY `monthlyOrdersCA` (`monthlyOrdersCA`),
 KEY `nintyDaysOrdresCA` (`nintyDaysOrdresCA`),
 KEY `dailyOrdersAU` (`dailyOrdersAU`),
 KEY `weeklyOrdersAU` (`weeklyOrdersAU`),
 KEY `monthlyOrdersAU` (`monthlyOrdersAU`),
 KEY `nintyDaysOrdresAU` (`nintyDaysOrdresAU`),
 KEY `AllTimeTop4Orders` (`AllTimeTop4Orders`),
 KEY `AllTimeOrdresUSMCA` (`AllTimeOrdresUSMCA`),
 KEY `AllTimeOrdersNA` (`AllTimesOrdresNA`),
 KEY `AllTimeOrdresUS` (`AllTimeOrdresUS`),
 KEY `AllTimeOrdresUK` (`AllTimeOrdresUK`),
 KEY `AllTimeOrdresCA` (`AllTimeOrdresCA`),
 KEY `AllTimeOrdresAU` (`AllTimeOrdresAU`),
 KEY `AllTimeOrdresEEA` (`AllTimeOrdresEEA`),
 KEY `AllTimeOrdresEA` (`AllTimeOrdresEA`),
 KEY `AllTimeOrdresEU` (`AllTimeOrdresEU`),
 KEY `isEditorPick` (`isEditorPick`),
 KEY `created_date` (`created_date`)
) ENGINE=InnoDB AUTO_INCREMENT=247877 DEFAULT CHARSET=latin1

订单表

CREATE TABLE `orders` (
 `oid` int(11) NOT NULL AUTO_INCREMENT,
 `countryCode` varchar(10) NOT NULL,
 `date` datetime NOT NULL,
 `id` bigint(20) NOT NULL,
 `productId` bigint(20) NOT NULL,
 PRIMARY KEY (`oid`),
 UNIQUE KEY `id` (`id`),
 KEY `date` (`date`),
 KEY `productId` (`productId`)
) ENGINE=InnoDB AUTO_INCREMENT=101646228 DEFAULT CHARSET=latin1

1 个答案:

答案 0 :(得分:1)

我建议创建一个临时表,该表将具有productid和所有这些订单总计列(dailyOrdersAU,weeklyOrdersAU等),然后您只需为“ product”表运行一个更新即可。您也可以在临时表上创建索引。查询的主要问题是,使用子查询基本上会否定您对productID的索引。这是临时表的教程:http://www.mysqltutorial.org/mysql-temporary-table/