2013-04-09 92 views
0

我正在開發一個網站,就像使用mysql的任何其他社交網站。查詢結果很慢

我希望它給我的用戶提供建議,並且我在我的應用程序中實現了這個功能,但它工作起來非常緩慢。這個過程需要2-3秒從服務器獲取結果。它具有所有必要的指標,關係表。我使用EXPLAIN命令來了解它,但我沒有任何問題。 我不明白它的基本問題是什麼。請幫幫我。

這裏是我的表結構:表 :UserMaster ~~~~~~~~~~~~~~~~~~

CREATE TABLE `UserMaster` (
     `UserID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, 
    `UserName` varchar(20) DEFAULT NULL, 
     `EMailID` varchar(50) DEFAULT NULL, 
    `FirstName` varchar(20) NOT NULL, 
    `LastName` varchar(20) NOT NULL, 
    `CityID` mediumint(8) unsigned DEFAULT NULL, 
    PRIMARY KEY (`UserID`), 
    UNIQUE KEY `UX_UserMaster_UserName` (`UserName`), 
    UNIQUE KEY `UX_UserMaster_EMailID` (`EMailID`), 
     KEY `FK_UserMaster_CityMst_CityID_idx` (`CityID`), 
     KEY `FK_UserMaster_CountryMst_CountryID_idx` (`CountryID`), 
    CONSTRAINT `FK_UserMaster_CityMst_CityID`  
     FOREIGN KEY (`CityID`) REFERENCES `CityMst` (`CityID`) ON DELETE NO ACTION, 
    CONSTRAINT `FK_UserMaster_CountryMst_CountryID` FOREIGN KEY CountryID REFERENCES CountryMst (CountryID) ON DELETE NO ACTION ON UPDATE CASCADE 
) 
ENGINE=InnoDB AUTO_INCREMENT=19722 DEFAULT CHARSET=utf8$$ 

表:UserFriends ~~~~~~ ~~~~~~~~~~~~~

CREATE TABLE `UserFriends` (
    `FriendID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, 
    `UserID` mediumint(8) unsigned NOT NULL, 
    `UserID2` mediumint(8) unsigned NOT NULL, 
    `RequestDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP, 
    `Status` tinyint(3) unsigned NOT NULL DEFAULT '2', 
    `ApprovalDate` timestamp NULL DEFAULT NULL, 
    PRIMARY KEY (`FriendID`), 
    UNIQUE KEY `UX_UserFriends_UserID_UserID2` (`UserID`,`UserID2`), 
    KEY `IX_UserFriens_UserID_ApprovalStatus` (`UserID`,`ApprovalStatus`,`UserID2`,`FriendID`,`RequestDate`,`ApprovalDate`), 
    KEY `FK_UserFriends_UserMaster_UserID_idx` (`UserID`), 
    KEY `FK_UserFriends_UserMaster_UserID2_idx` (`UserID2`), 
    CONSTRAINT `FK_UserFriends_UserMaster_UserID` FOREIGN KEY (`UserID`) REFERENCES `UserMaster` (`UserID`) ON DELETE NO ACTION ON UPDATE CASCADE, 

    CONSTRAINT `FK_UserFriends_UserMaster_UserID2` FOREIGN KEY (`UserID2`) REFERENCES `UserMaster` (`UserID`) ON DELETE NO ACTION ON UPDATE CASCADE 
) 
    ENGINE=InnoDB AUTO_INCREMENT=50825 DEFAULT CHARSET=utf8$$ 

用戶ID & UserID2兩個字段與UserMaster.UserID鏈接

這裏是我的選擇查詢: ~~~~~~~~~~~~~~~~~~~~~~~~~

SELECT 
    upm.UserID, 
    upm.UserName, 
    upm.FirstName, 
    COALESCE(mf.TotMutualFriends,0) TotMutualFriends 
FROM UserMaster upm 
LEFT JOIN CityMst ct on ct.CityID = upm.CityID 
LEFT JOIN StateMst st on st.StateID = ct.StateID 
LEFT JOIN (
    SELECT uf.UserID, COUNT(1) TotMutualFriends 
    FROM (
     SELECT uf.UserID, uf.UserID2, uf.ApprovalStatus 
     FROM UserFriends uf 
     UNION ALL 
     SELECT uf.UserID2 UserID, uf.UserID UserID2, uf.ApprovalStatus 
     FROM UserFriends uf 
    ) uf 
    INNER JOIN ( 
     SELECT IF(uf.UserID = 1, uf.UserID2, uf.UserID) UserID2 
     FROM UserFriends uf 
     WHERE (uf.UserID = 1 OR uf.UserID2 = 1) 
      AND uf.ApprovalStatus = 1 
    ) uf1 on uf1.UserID2 = uf.UserID2 
    WHERE uf.ApprovalStatus = 1 
    GROUP BY uf.UserID 
) mf on mf.UserID = upm.UserID 
LEFT JOIN (
    SELECT DISTINCT usar.UserID2 
    FROM UserSuggAutoRejct usar 
    WHERE usar.UserID = 1 
    UNION 
    SELECT IF(uf.UserID = 1, uf.UserID2, uf.UserID) UserID2 
    FROM UserFriends uf 
    WHERE (uf.UserID = 1 OR uf.UserID2 = 1) 
) usar ON usar.UserID2 = upm.UserID 
WHERE upm.UserStatus IN(10,11) 
    AND upm.UserID <> 1 
    AND upm.UserID NOT IN (1221,2191) 
    AND usar.UserID2 IS NULL 
ORDER BY 
    (CASE WHEN COALESCE(mf.TotMutualFriends,0) > 0 THEN 0  ELSE 1 END), 
    (CASE WHEN COALESCE(mf.TotMutualFriends,0) > 0 THEN RAND() ELSE NULL END), 
    (CASE upm.CityID WHEN 1 THEN 0  ELSE 1 END), 
    (CASE upm.CityID WHEN 1 THEN RAND() ELSE NULL END), 
    (CASE ct.StateID WHEN 1 THEN 0  ELSE 1 END), 
    (CASE ct.StateID WHEN 1 THEN RAND() ELSE NULL END), 
    (CASE st.CountryID WHEN 91 THEN 0  ELSE 1 END), 
    (CASE st.CountryID WHEN 91 THEN RAND() ELSE NULL END) 
LIMIT 10 

這是執行速度很慢。它需要2-3秒才能變化。

+0

這將是很好,如果你發佈一些樣本數據和所需的輸出,而不是這個複雜的查詢。 – 2013-04-09 09:55:20

+1

爲什麼使用標籤[tag:mysql-error-1064]?你遇到語法錯誤嗎? – Jocelyn 2013-04-09 10:03:36

+0

不,沒有語法錯誤。但這是我選擇標籤的錯誤。 – 2013-04-09 12:46:32

回答

0

關係數據庫可能與社交網站無關。 因爲連接表非常慢,請嘗試使用其他非sql數據庫(NoSql類型的數據庫)。 如果你仍然堅持使用mysql,那麼儘量不要加入你的查詢。

對不起,我的英語不好,如果有的話。