查询结果非常慢

时间:2013-04-09 09:47:38

标签: mysql social-networking mysql-workbench

我正在使用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$$

UserID& 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

这表现得很慢。 evolute需要2-3秒。

1 个答案:

答案 0 :(得分:0)

关系数据库可能与社交网站无关。 因为连接表非常慢,请尝试使用其他no-sql数据库(NoSql类型数据库)。 如果你仍然坚持使用mysql,那么尽量不要加入你的查询。

抱歉我的英语不好,如果有的话。