3 つしかない機器が 4 つの同時イベントで同時に使用されているかどうかを判断するために使用される MySQL クエリがあります。有効なクエリがありますが、効率が悪いようで、選択を行うためのより良い方法があるかどうか知りたいです。
テーブル構造 (クエリに関係のない列は削除されています) 現在、テーブルには約 7 ~ 8,000 行が含まれています。
CREATE TABLE IF NOT EXISTS `tc_event` (
`WorkOrderNumber` int(11) NOT NULL DEFAULT '0',
`EventName` varchar(120) DEFAULT NULL,
`CrewStartTime` datetime DEFAULT NULL,
`CrewEndTime` datetime DEFAULT NULL,
`SoundConsole` varchar(30) DEFAULT NULL,
`Canceled` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`WorkOrderNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
現在の (作業中の) クエリ:
SELECT UNIX_TIMESTAMP(T1.CrewStartTime) AS ConflictDate
FROM tc_event T1, tc_event T2, tc_event T3, tc_event T4
WHERE
(T1.CrewStartTime BETWEEN T2.CrewStartTime AND T2.CrewEndTime)
AND (T1.CrewStartTime BETWEEN T3.CrewStartTime AND T3.CrewEndTime)
AND (T1.CrewStartTime BETWEEN T4.CrewStartTime AND T4.CrewEndTime)
AND (T2.CrewSTartTime BETWEEN T3.CrewStartTime AND T3.CrewEndTime)
AND (T2.CrewStartTime BETWEEN T4.CrewStartTime AND T4.CrewEndTime)
AND (T3.CrewStartTime BETWEEN T4.CrewStartTime AND T4.CrewEndTime)
AND T1.WorkOrderNumber != T2.WorkOrderNumber
AND T1.WorkOrderNumber != T3.WorkOrderNumber
AND T1.WorkOrderNumber != T4.WorkOrderNumber
AND T2.WorkOrderNumber != T3.WorkOrderNumber
AND T2.WorkOrderNumber != T4.WorkOrderNumber
AND T3.WorkOrderNumber != T4.WorkOrderNumber
AND T1.CrewStartTime > NOW()
AND T1.SoundConsole = '12-Chan/PA-12'
AND T2.SoundConsole = '12-Chan/PA-12'
AND T3.SoundConsole = '12-Chan/PA-12'
AND T4.SoundConsole = '12-Chan/PA-12'
AND (T1.Canceled = 0 AND T1.EventName NOT LIKE '%*NOTC*%')
AND (T2.Canceled = 0 AND T2.EventName NOT LIKE '%*NOTC*%')
AND (T3.Canceled = 0 AND T3.EventName NOT LIKE '%*NOTC*%')
AND (T4.Canceled = 0 AND T4.EventName NOT LIKE '%*NOTC*%')
GROUP BY DATE(T1.CrewStartTime)
ORDER BY T1.CrewStartTime
EXPLAIN ステートメントのクエリ: