いくつかのイベントをフェッチするために使用されるメインクエリにこれらのサブクエリがあります。
SELECT [...],
(SELECT COUNT(*) FROM WEventUser WHERE WEventUser.eID=e.eID AND favorited=1) as numfavorited,
(SELECT COUNT(*) FROM WEventUser WHERE WEventUser.eID=e.eID AND subscribed=1) as numsubscribed,
(SELECT COUNT(*) FROM WEventUser WHERE eID=e.eID AND WEventUser.uID=2 AND favorited=1) as favorited,
(SELECT COUNT(*) FROM WEventUser WHERE eID=e.eID AND WEventUser.uID=2 AND subscribed=1) as subscribed,
[...] WHERE...etc.
WEventUserの構造は非常に単純です
CREATE TABLE IF NOT EXISTS `WEventUser` (
`eID` int(10) unsigned NOT NULL auto_increment,
`uID` int(10) unsigned NOT NULL,
`favorited` int(1) unsigned default '0',
`subscribed` int(1) unsigned default '0',
PRIMARY KEY (`eID`,`uID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
これらのサブクエリは本当に高価です。代替案(単一の結合など)を見つけるのを手伝ってもらえますか?
前もって感謝します!
編集:私はメインのWEventsテーブルから選択しています:
CREATE TABLE IF NOT EXISTS `wevents` (
`eID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uID` int(10) unsigned DEFAULT NULL,
`ecID` int(10) unsigned NOT NULL,
`eName` varchar(64) NOT NULL,
`eDescription` longtext,
`eIsActive` varchar(1) NOT NULL DEFAULT '0',
`eIsValidated` tinyint(4) NOT NULL DEFAULT '-1',
`eDateAdded` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`eDateModified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`eID`,`ecID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;