我有用來獲取一些事件主查詢,這些子查詢:Mysql的子查詢優化 - 計數
WEventUser的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.
結構相當簡單
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;
查詢中的e.eID是什麼。請分享第二個表格結構 –