0

私はmySQLクエリを持っていますが、それは機能していますが、160万回の実行になると、常に再実行されるため、必要なパフォーマンスを満たしていませんNOT EXIST(new query)

INSERT INTO `votes` (`representatives_rID`, `events_eID`, `voteResult`) 
SELECT `representatives`.`rID`,`events`.`eID`,? 
FROM `representatives`, `events` 
WHERE `events`.`eventDateTime` = ? 
AND `representatives`.`rID` = ? 
AND NOT EXISTS (SELECT * FROM `votes` 
                WHERE `votes`.`representatives_rID` = `representatives`.`rID` 
                AND `votes`.`events_eID` = `events`.`eID`);

回路図:

if (input one is in `representatives` AND input two is in `events` AND there is no row in `votes` that contains ( `votes`.`representatives_rID` = `representatives`.`rid` and `votes`.`events_eID` = `events`.`eID)) {
    insert a row into votes containing `eid` from `events` and `rid` from `representatives` and input three;
}

おそらく結合を使用して、これを高速化する方法はありますか?

4

1 に答える 1

2
INSERT IGNORE INTO votes 
(representatives_rID
,events_eID
,voteResult
) 
SELECT r.rID
     , e.eID
     , ?
  FROM representatives r 
 CROSS
  JOIN events e
    ON e.eventDateTime = ? 
   AND r.rID = ?; 

?

于 2013-04-24T16:25:29.483 に答える