Mysql试图在新闻提要查询中获得最佳性能

时间:2014-08-28 20:00:23

标签: mysql performance

我在Facebook上做新闻提要,我使用两个表来维护帖子和连接:

Table Posts:
nId | txCategory_con | nItemid_con | txCategory | nItemid | 
--------------------------------------------------------------
1   | 'user'         | 1           | 'user'     | 1          (posted on user page by user)
2   | 'place'        | 1           | 'user'     | 1          (posted on place page by user)
....

Table Connections:
txCategory | nItemid | nUserid | nActive
------------------------------------------
'place'    | 1       | 1       | 1

要从我使用此查询的地方,页面等处获取所有状态更新:

SELECT
   DISTINCT(a.nId),
   a.txCategory_con,
   a.nItemid_con,
   a.txCategory,
   a.nItemid,
   a.dtCreated
FROM                    
   Posts a                                        
WHERE                    
 ((SELECT COUNT(*) 
   FROM Connections b 
   WHERE b.txCategory = a.txCategory_con 
       AND a.nItemid_con = b.nItemid AND b.nUserid = <USERID> AND b.nActive = 1) = 1)
OR (a.txCategory = 'user' AND a.nItemid = <USERID>)
ORDER BY a.dtCreated DESC
LIMIT 0, 5

此查询的问题在于性能,因为在2K行中,子查询非常慢。

有人有其他想法吗?

0 个答案:

没有答案