我有一张表USERDATA
,我添加了几个新列。
所有都不是NUL,默认为0或1.但是,我的网站在这之后变得很慢。
我的意思是真的很慢。这是因为我的网站上有这个查询脚本:
$num_rows = $db->doQuery('SELECT TOP 50 IDNum, IDName, Nation, (SELECT SUM(Loyalty) FROM USERDATA WHERE USERDATA.Knights = KNIGHTS.IDNum AND USERDATA.Authority = 1) as ClanLoyalty, (SELECT SUM(MannerPoint) FROM USERDATA WHERE USERDATA.Knights = KNIGHTS.IDNum AND USERDATA.Authority = 1) as ClanManner FROM KNIGHTS WHERE KNIGHTS.IDNum NOT IN (1,15001) ORDER BY ClanLoyalty DESC, IDName ASC');
以前工作得很好但是在添加了上面这个查询中根本没有包含的新列之后..它仍然减慢网站请求TOP 50 ..基本上SUM
查询正在执行它。为什么这些新专栏会产生这样的问题?
USERDATA
只有32k行,之前它运行良好。我没有指定constaint,这可能是问题吗?
我使用的是MSSQL Server 2005。
答案 0 :(得分:0)
像这样更改您的查询。
SELECT TOP 50 IDNum,
IDName,
Nation,
Sum(Loyalty) ClanLoyalty,
Sum(MannerPoint) ClanManner
FROM KNIGHTS
LEFT JOIN USERDATA
ON USERDATA.Knights = KNIGHTS.IDNum
AND USERDATA.Authority = 1
WHERE KNIGHTS.IDNum NOT IN ( 1, 15001 )
Group by IDNum,IDName,Nation
ORDER BY ClanLoyalty DESC,
IDName ASC