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$$

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

这执行得很慢。进化需要2-3秒。

4

1 回答 1

0

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

对不起我的英语不好,如果有的话。

于 2013-04-09T10:41:03.070 回答