简体   繁体   中英

Added new columns to table and slow down the whole database .. Why?

I've got a table USERDATA and I added few new columns to it. All are NOT NUL and default 0 or 1. However, my website became slow after doing that. I mean really slow. And it's because of this query script on my website:

$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');

It worked all good previously but after adding the new columns which are not included at all in this query above .. it still slow down the website to request TOP 50 .. basically the SUM query is doing it .. why these new columns could produce such a problem?

USERDATA has 32k rows only and it worked all good previously. I didn't specify constaint, could be this the issue?

I use MSSQL Server 2005.

Change your query like this.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM