Mysql试图在新闻提要查询中获得最佳性能
我在Facebook上做一些类似于新闻提要的事情,我使用两个表来维护帖子和连接:Mysql试图在新闻提要查询中获得最佳性能,mysql,performance,Mysql,Performance,我在Facebook上做一些类似于新闻提要的事情,我使用两个表来维护帖子和连接: Table Posts: nId | txCategory_con | nItemid_con | txCategory | nItemid | -------------------------------------------------------------- 1 | 'user' | 1 | 'user' | 1 (posted on
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行中,子查询非常慢
有人有其他想法吗?解释、索引、厨房水槽。要优化查询,我们需要查看表和索引定义,以及每个表的行数。也许您的表定义不好。可能索引没有正确创建。也许你在你认为你有的专栏上没有索引。如果看不到表和索引定义,我们无法判断。我们还需要行计数,因为这会极大地影响查询优化。如果你知道如何进行解释或获得执行计划,也可以将结果放在问题中。如果没有索引,请尽快访问。