我需要建议是否可以进行这种调整。
桌子:
CREATE TABLE UTIL_DAY (
CompID varchar(15) collate utf8_czech_ci NOT NULL,
TestplanID int(10) unsigned NOT NULL default '0',
FromDate datetime NOT NULL default '0000-00-00 00:00:00',
ToDate datetime NOT NULL default '0000-00-00 00:00:00',
Interval int(11) NOT NULL default '0',
Variant varchar(25) collate utf8_czech_ci default NULL,
WO_ID int(11) default NULL,
SAPTime int(5) default NULL,
UseTime int(5) NOT NULL,
MeasureCount int(10) unsigned NOT NULL,
KEY CompID (CompID),
KEY TestplanID (TestplanID),
KEY WO_ID (WO_ID),
KEY FromDate (FromDate),
KEY Variant (Variant)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
当前查询:
UPDATE UTIL_DAY UDAY SET TestplanID = (SELECT TestplanID
FROM (SELECT UD_U.Variant,UD_U.WO_ID,VS.TestplanID
FROM (SELECT UD.Variant,UD.WO_ID
FROM UTIL_DAY UD WHERE SUBSTR(CompID,1,3) <> 'AOI'
GROUP BY UD.Variant,UD.WO_ID) UD_U
JOIN VARIANTS V ON UD_U.Variant = V.Variant
JOIN VARIANT_STATUS VS ON V.VariantID = VS.VariantID
JOIN TESTPLANS_WO_ID TW ON UD_U.WO_ID = TW.WO_ID
WHERE VS.TestplanID = TW.TestplanID
GROUP BY UD_U.Variant,UD_U.WO_ID ) TP
WHERE TP.Variant = UDAY.Variant AND TP.WO_ID = UDAY.WO_ID)
我的更新是临时表:
CREATE TEMPORARY TABLE UTIL_DAY_TEMP (
Variant varchar(25) COLLATE 'utf8_czech_ci' NOT NULL,
WO_ID int unsigned NOT NULL,
TestplanID int unsigned NOT NULL,
PRIMARY KEY Variant_WO_ID (Variant, WO_ID)) COMMENT='' ENGINE='InnoDB' COLLATE 'utf8_czech_ci';
INSERT IGNORE INTO UTIL_DAY_TMP (SELECT UD_U.Variant,UD_U.WO_ID,VS.TestplanID
FROM (SELECT UD.Variant,UD.WO_ID
FROM UTIL_DAY UD WHERE SUBSTR(CompID,1,3) <> 'AOI'
GROUP BY UD.Variant,UD.WO_ID) UD_U
JOIN VARIANTS V ON UD_U.Variant = V.Variant
JOIN VARIANT_STATUS VS ON V.VariantID = VS.VariantID
JOIN TESTPLANS_WO_ID TW ON UD_U.WO_ID = TW.WO_ID
WHERE VS.TestplanID = TW.TestplanID
GROUP BY UD_U.Variant,UD_U.WO_ID);
UPDATE UTIL_DAY UDAY SET TestplanID = (SELECT TestplanID
FROM UTIL_DAY_TEMP TP
WHERE TP.Variant = UDAY.Variant AND TP.WO_ID = UDAY.WO_ID);
这一改进将超过 200 万行的查询速度提高了 400%。但是这种改进有同样的结果吗?谢谢您的回答。