我有一個很難得到此查詢效果會更好:很難優化查詢
我有以下2個表:
DROP TABLE IF EXISTS `casino`.`mutualfriends`;
CREATE TABLE `casino`.`mutualfriends` (
`CustUID` varchar(64) CHARACTER SET ascii NOT NULL,
`CustUID2` varchar(64) CHARACTER SET ascii NOT NULL,
`FType` tinyint(4) NOT NULL,
PRIMARY KEY (`CustUID`,`CustUID2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `casino`.`customers`;
CREATE TABLE `casino`.`customers` (
`CustFullName` varchar(45) NOT NULL,
`CustEmail` varchar(128) NOT NULL,
`CustUID` varchar(64) CHARACTER SET ascii NOT NULL,
`CustMoney` bigint(20) NOT NULL DEFAULT '0',
`SmallPicURL` varchar(120) CHARACTER SET ascii DEFAULT '',
`LargePicURL` varchar(120) CHARACTER SET ascii DEFAULT '',
PRIMARY KEY (`CustUID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CUSTOMERS表具有1M行,共同的朋友有500K行。
我很難優化此查詢;似乎有一個表掃描使用它。我想盡量減少掃描:
SELECT c.CustUID AS Cuid, c.CustFullName, c.CustMoney, c.SmallPicURL
FROM `customers` c
WHERE c.`CustUID` = '1:1542073175'
UNION
SELECT m.`CustUID2` AS Cuid, c.CustFullName, c.CustMoney, c.SmallPicURL
FROM `mutualfriends` m, `customers` c
WHERE m.`CustUID` = '1:1542073175'
AND c.`CustUID` = m.`CustUID2`
UNION
SELECT m.`CustUID` AS Cuid, c.CustFullName, c.CustMoney, c.SmallPicURL
FROM `mutualfriends` m, customers c
WHERE m.`CustUID2` = '1:1542073175'
AND c.CustUID = m.`CustUID`
你究竟想要做什麼?您是否想要獲得所有客戶及其共同朋友的列表?從代碼中不太清楚。 – Ramy 2010-11-23 05:54:02
好吧,我正在做的是得到一個客戶列表,他與客戶是共同的朋友,他的ID是:'1:1542073175'。查詢結果是正確的,但其性能很差,我正在尋找一種方式來獲得與上述查詢相同的結果,但具有更好的性能 – mysqldude 2010-11-23 05:57:17