Fixing performance issues when looping through MySQL results in PHP -


I am working on a virtual game board style game in which the players get points on a fixed area of ​​the board. (Make it easy for me, because I'm doing it only as a hobby, so I can do it in the worst possible way)

I have 3 tables, information about all the players (examples Id, screen name). All points of each other area (such as ID, X, Y) and one-third store each player (eg ID, player, regional, points) are the number of points of each player. To make a "leaderboard", I'm piping through all the players, then in that loop I am looping all the areas, then in the second loop in the end, I get the leaders of that area and see Whether the current player in the first loop matches, if so, then I raise a counter, then store it in an array. (See the code below with some commenting)

I looked in MySQL caching, but I do not have access to many server options, as well as would like to keep as much live results as possible, so caching is on the right track Can not go

My question is whether I am doing it properly or not. Currently there are only about 10 players, and around 500 fields are getting me writing down, it takes approximately 5-8 seconds to run in advance. There can potentially be millions of areas, so that such a long delay in processing can be catastrophic (anyway for leaderboards) am I going to go about this right and / or there is a better way of doing this?

  & lt ;? Php $ leaders = array (); // Loop through all players $ sql = "select * from players"; $ Result = mysqli_query ($ con, $ sql) or die (mysqli_error ($ con)); While ($ row = mysqli_fetch_array ($ result)) {// player information in variable $ playerId = $ row ['id']; $ PlayerScreenName = $ line ['screen name']; // reset field counter $ area area = 0; $ Leader = array (); // Loop $ sql2 = "SELECT * FROM regions" through all the areas; $ Result2 = mysqli_query ($ con, $ sql2) or die (mysqli_error ($ con)); While ($ row2 = mysqli_fetch_array ($ result2)) {$ areaId = $ row2 ['id']; // Get the player $ sql3 = "SELECT * FROM with the most points of the points where the regional = '$ region ID' ordering the total point DESC limit1"; $ Result3 = mysqli_query ($ con, $ sql3) or die (mysqli_error ($ con)); While ($ row3 = mysqli_fetch_array ($ result3)) {$ leaderOfArea = $ row3 ['player']; // If the leader of the field is the same player, then if we ($ playerId == $ leaderOfArea) {// If so, then counter $ AreaCount ++; }}} // Store the leader's information in an array to output the $ leader ['screenname]] = $ playerScreenName; $ Leader ['field'] = $ area area; $ Leaders [] = $ leader; } // can be scored by Sort Nair ($ leaders, 'compare_recount'); ? & Gt; The database connection is above the opening, and when you do it in the loop, you increase the problem (and then when you add a loop inside it, you spoil it too much). Instead, rejoin this as a query by using a join or sub-service 


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -