简体   繁体   中英

Combine this MySQL query to be more efficient

I recently developed this website where players can get ranked on my website for playing certain arcade games. One of the issues I've come across is using too many SQL queries. My pages loading times have been dramatically affected from members registering. I realized that some of the code I've been using might need to be revised. Here was my first issue.

<?php

$time = time();

     $query5 = $db->query("SELECT playeruid , playeruid AS player_id, (
(
    SELECT COALESCE(sum(player1points),0) 
    FROM  `mybb_matches` 
    WHERE player1uid = player_id AND gid = '1' AND timestamp < $time AND winneruid is NOT NULL )
+
(
    SELECT COALESCE(sum(player2points),0) 
    FROM  `mybb_matches` 
    WHERE player2uid = player_id AND gid = '1' AND timestamp < $time AND winneruid is NOT NULL )
+
(
    SELECT SUM( rank ) 
    FROM  `mybb_matchesgame` 
    WHERE playeruid = player_id AND gid = '1' LIMIT 1 )
)

 AS points
 FROM mybb_matchesgame WHERE gid = '1'
 ORDER BY points DESC LIMIT 1");         
while($row = mysqli_fetch_array($query5))

{
echo "
<div class=\"fluidbox\"><a href='member.php?action=profile&uid=$player[uid]'>$player[username]</a>";

} 
 ?>

<?php

$time = time();

     $query5 = $db->query("SELECT playeruid , playeruid AS player_id, (
(
    SELECT COALESCE(sum(player1points),0) 
    FROM  `mybb_matches` 
    WHERE player1uid = player_id AND gid = '2' AND timestamp < $time AND winneruid is NOT NULL )
+
(
    SELECT COALESCE(sum(player2points),0) 
    FROM  `mybb_matches` 
    WHERE player2uid = player_id AND gid = '2' AND timestamp < $time AND winneruid is NOT NULL )
+
(
    SELECT SUM( rank ) 
    FROM  `mybb_matchesgame` 
    WHERE playeruid = player_id AND gid = '2' LIMIT 1 )
)

 AS points
 FROM mybb_matchesgame WHERE gid = '2'
 ORDER BY points DESC LIMIT 1");         
while($row = mysqli_fetch_array($query5))

{
echo "
<div class=\"fluidbox\"><a href='member.php?action=profile&uid=$player[uid]'>$player[username]</a>";

} 
 ?>

As you can see, the code is repeated but then used to find the other GID. I did this 4 times to find all unique GID's. However, I realize, that this has painfully added on seconds to the loading time on the site. I realize that I need to combine this code in a way where I can use it to find ALL unique GIDs.

One of the methods I came across was distinct. However, I was unable to get it to work. What would be the proper way of doing so?

The problem is that you query your database several times which might not be necessary as you could get your results with less "sub-queries" Eg

(SELECT Coalesce(Sum(player1points), 0) 
    FROM   `mybb_matches` 
    WHERE  player1uid = player_id 
           AND gid = '1' 
           AND timestamp < $time 
           AND winneruid IS NOT NULL) 
+ (SELECT Coalesce(Sum(player2points), 0) 
    FROM   `mybb_matches` 
    WHERE  player2uid = player_id 
           AND gid = '1' 
           AND timestamp < $time 
           AND winneruid IS NOT NULL) [...]

Could be "simplified" to something like this. I do not know your schema and this was just a quick suggestion. This might even be slower or not working as there might be some errors in it :D

(SELECT (Sum(player1points)+Sum(player2points)+Sum(rank)) as total 
  FROM   `mybb_matches` 
  LEFT OUTER JOIN `mybb_matchesgame`
     ON playeruid = player_id
    AND gid = '1'
    AND timestamp < $time
    AND winnderuid IS NOT NULL
    AND ( player1uid = player_id OR player2uid = player_id )) 

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