0
我有一個SQL查詢需要4個多小時才能完成。Mysql innerjoin非常慢
查詢是如下
UPDATE carepathaction a1
INNER JOIN carepathaction a2
ON a1.sourceoid = a2.oid
INNER JOIN carepathgroup g
ON g.sourceoid = a2.carepathgroupoid
INNER JOIN carepathphase f
ON f.oid = g.carepathphaseoid
SET a1.carepathgroupoid = g.oid
WHERE f.code = 'EDIT'
他有更新65000項。在這個查詢中我能做些什麼優化? CarePathAction
CREATE TABLE `carepathaction`
(
`oid` INT(11) NOT NULL,
`typeid` CHAR(1) NOT NULL DEFAULT 'X',
`sequencenumber` INT(11) NOT NULL,
`description` VARCHAR(1024) NOT NULL,
`duration` INT(11) NOT NULL comment 'In minutes',
`deadline` INT(11) NOT NULL comment
'# dagen in combinatie met deadlineType (sinds begin traject of sinds begin deze actie)'
,
`deadlinetype` INT(11) NOT NULL comment
'1) = deadline sinds zorgpad\n2) = deadline sinds start deze actie\n3) = deadline sinds zorgpadfase'
,
`repeatperweek` INT(11) DEFAULT NULL,
`numbertorepeat` INT(11) DEFAULT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`usertaskoid` INT(11) DEFAULT NULL,
`templateattachment` VARCHAR(45) DEFAULT NULL,
`partyrolecategoryoid` INT(11) DEFAULT NULL,
`automatic` BIT(1) NOT NULL DEFAULT b'0',
`carepathphaseoid` INT(11) DEFAULT NULL,
`parentoid` INT(11) DEFAULT NULL,
`documentoid` INT(11) DEFAULT NULL,
`epdtemplateoid` INT(11) DEFAULT NULL,
`producttypeoid` INT(11) DEFAULT NULL,
`evaluateby` INT(11) DEFAULT NULL,
`pathlock` BIT(1) NOT NULL DEFAULT b'0',
`carepathgroupoid` INT(11) NOT NULL,
`activitygroupoid` INT(11) DEFAULT NULL,
`title` VARCHAR(45) DEFAULT NULL,
`carepathactionswitchtype` INT(11) DEFAULT NULL,
`modulesdomaincode` VARCHAR(10) DEFAULT NULL,
`startday` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
`templatefilename` VARCHAR(100) DEFAULT NULL,
`istask` BIT(1) NOT NULL DEFAULT b'0',
`typecontact` INT(11) DEFAULT NULL,
`carepathactionepdoid` INT(11) DEFAULT NULL,
`carepathactiontreatmentplanoid` INT(11) DEFAULT NULL,
`clientportalenabled` BIT(1) DEFAULT NULL,
`startafternumberofdaysepddatefield` INT(11) DEFAULT NULL,
`startafterdateepdfieldoid` INT(11) DEFAULT NULL,
`epdtemplatefieldoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathactionfk1_idx` (`carepathoid`),
KEY `carepathactionfk2_idx` (`carepathphaseoid`),
KEY `carepathactionfk3_idx` (`documentoid`),
KEY `carepathactionfk4_idx` (`epdtemplateoid`),
KEY `carepathactionfk5_idx` (`producttypeoid`),
KEY `carepathactionfk6_idx` (`carepathgroupoid`),
KEY `carepathactionfk6_idx1` (`activitygroupoid`),
KEY `carepathactionfk7_idx` (`parentoid`),
KEY `carepathactionfk8_idx` (`usertaskoid`),
KEY `carepathactionfk10_idx` (`partyrolecategoryoid`),
KEY `carepathactionfk11_idx` (`carepathactionepdoid`),
KEY `carepathactionfk12_idx` (`carepathactiontreatmentplanoid`),
KEY `carepathactionfk13_idx` (`startafterdateepdfieldoid`),
KEY `carepathactionfk14_idx` (`epdtemplatefieldoid`),
CONSTRAINT `carepathactionfk14` FOREIGN KEY (`epdtemplatefieldoid`)
REFERENCES `epdtemplatefield` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk10` FOREIGN KEY (`partyrolecategoryoid`)
REFERENCES `partyrolecategory` (`oid`) ON DELETE no action ON UPDATE no
action,
CONSTRAINT `carepathactionfk11` FOREIGN KEY (`carepathactionepdoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk12` FOREIGN KEY (
`carepathactiontreatmentplanoid`) REFERENCES `carepathaction` (`oid`) ON
DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk13` FOREIGN KEY (`startafterdateepdfieldoid`)
REFERENCES `carepathaction` (`oid`) ON DELETE no action ON UPDATE no action
,
CONSTRAINT `carepathactionfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk3` FOREIGN KEY (`documentoid`) REFERENCES
`document` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk4` FOREIGN KEY (`epdtemplateoid`) REFERENCES
`epdtemplate` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk5` FOREIGN KEY (`producttypeoid`) REFERENCES
`producttype` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk6` FOREIGN KEY (`carepathgroupoid`) REFERENCES
`carepathgroup` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk7` FOREIGN KEY (`parentoid`) REFERENCES
`carepathaction` (`oid`) ON DELETE no action ON UPDATE no action,
CONSTRAINT `carepathactionfk8` FOREIGN KEY (`usertaskoid`) REFERENCES
`tasktoschedule` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathGroup
CREATE TABLE `carepathgroup`
(
`oid` INT(11) NOT NULL,
`sequencenumber` INT(11) NOT NULL,
`title` VARCHAR(50) NOT NULL,
`carepathoid` INT(11) DEFAULT NULL,
`carepathphaseoid` INT(11) NOT NULL,
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathgroupfk1_idx` (`carepathoid`),
KEY `carepathgroupfk2_idx` (`carepathphaseoid`),
CONSTRAINT `carepathgroupfk2` FOREIGN KEY (`carepathphaseoid`) REFERENCES
`carepathphase` (`oid`) ON DELETE no action ON UPDATE no action
)
CarePathPhase
CREATE TABLE `carepathphase`
(
`oid` INT(11) NOT NULL,
`code` VARCHAR(6) NOT NULL,
`description` VARCHAR(45) NOT NULL,
`sequencenumber` INT(11) NOT NULL DEFAULT '0',
`carepathoid` INT(11) DEFAULT NULL,
`carepathphasetype` INT(11) NOT NULL DEFAULT '0',
`sourceoid` INT(11) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `carepathphasefk1_idx` (`carepathoid`),
CONSTRAINT `carepathphasefk1` FOREIGN KEY (`carepathoid`) REFERENCES
`carepath` (`oid`) ON DELETE no action ON UPDATE no action
)
解釋
1 SIMPLE f ALL PRIMARY 61925 Using where
1 SIMPLE g ref CarePathGroupFK2_idx CarePathGroupFK2_idx 4 amis.f.Oid 1
1 SIMPLE a1 ALL 166582 Using join buffer
1 SIMPLE a2 eq_ref PRIMARY,CarePathActionFK6_idx PRIMARY 4 amis.a1.SourceOid 1 Using where
請發佈連接中所有表的創建表,以便我們可以看到索引。請將查詢改爲select,並在此處發佈解釋的結果。 – Shadow
我希望這是足夠的信息?選擇有65000行,所以我不能在這裏發佈 – joey
不,這是不夠的。我明確要求創建表語句來查看索引。我沒有要求選擇的結果被披露,我請求你對選擇進行解釋併發布解釋的結果。 – Shadow