,我有以下2個表:查詢性能問題
CREATE TABLE `customers` (
`CustFullName` varchar(45) NOT NULL,
`CustPassword` varchar(45) NOT NULL,
`CustEmail` varchar(128) NOT NULL,
`SocialNetworkId` tinyint(4) NOT NULL,
`CustUID` varchar(64) CHARACTER SET ascii NOT NULL,
`CustMoney` bigint(20) NOT NULL DEFAULT '0',
`LastIpAddress` varchar(45) CHARACTER SET ascii NOT NULL,
`LastLoginTime` datetime NOT NULL DEFAULT '1900-10-10 10:10:10',
`SmallPicURL` varchar(120) CHARACTER SET ascii DEFAULT '',
`LargePicURL` varchar(120) CHARACTER SET ascii DEFAULT '',
`LuckyChips` int(10) unsigned NOT NULL DEFAULT '0',
`AccountCreationTime` datetime NOT NULL DEFAULT '2009-11-11 11:11:11',
`AccountStatus` tinyint(4) NOT NULL DEFAULT '1',
`CustLevel` int(11) NOT NULL DEFAULT '0',
`City` varchar(32) NOT NULL DEFAULT '',
`State` varchar(32) NOT NULL DEFAULT '0',
`Country` varchar(32) NOT NULL DEFAULT '',
`Zip` varchar(16) CHARACTER SET ascii NOT NULL,
`CustExp` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`CustUID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
和:
CREATE TABLE `mutualfriends` (
`CustUID` varchar(32) CHARACTER SET ascii NOT NULL,
`CustUID2` varchar(32) CHARACTER SET ascii NOT NULL,
`FType` tinyint(4) NOT NULL,
PRIMARY KEY (`CustUID`,`CustUID2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
客戶表包含100萬行和大約50,000列mutalfriends。
我需要以下查詢的結果:
SELECT c.CustUID, c.CustFullName, c.CustMoney, c.SmallPicURL
FROM `customers` c
WHERE c.`CustUID` = '9:2'
OR c.`CustUID` IN
(SELECT m.CustUID2 FROM mutualfriends m WHERE m.CustUID = '9:2');
OR c.`CustUID` IN
(SELECT m.CustUID FROM mutualfriends m WHERE m.CustUID2 = '9:2');
由於某種原因,我不明白,這個查詢大約需要10 secconds完成。 子查詢中包含每次不超過3行,如果我把常數代替:
(SELECT m.CustUID2 FROM mutualfriends m WHERE m.CustUID = '9:2');
和:
(SELECT m.CustUID FROM mutualfriends m WHERE m.CustUID2 = '9:2');
例如:
SELECT c.CustUID, c.CustFullName, c.CustMoney, c.SmallPicURL
FROM `customers` c
WHERE c.`CustUID` = '9:2'
OR c.`CustUID` IN
('9:3','9:4','9:5');
OR c.`CustUID` IN
('9:6','9:7');
那麼查詢需要一個幾個毫秒完成。 我在做什麼錯此查詢應該不會超過幾毫秒...
我還可以補充的是,查詢的這個部分:
(SELECT m.CustUID2 FROM mutualfriends m WHERE m.CustUID = '9:2');
(SELECT m.CustUID FROM mutualfriends m WHERE m.CustUID2 = '9:2');
還需要幾毫秒.. ..
我不這麼認爲,因爲這行只需要幾個ms:(SELECT m.CustUID2 FROM mutualfriends m WHERE m.CustUID ='9:2'); – 2010-10-01 15:56:51