Jump to content

Recommended Posts

Would using this to generate a rank from a player from the database, if there were to be thousands of players in the DB, and this script would have to be run every couple seconds, would this be bad?

 

//Get the name from the URL
$username = $_REQUEST['name'];

//Get data from the database
$result = mysql_query("SELECT * FROM `table` ORDER by exp desc") 
or die(mysql_error());  
$rank = 1;
while($row = mysql_fetch_array( $result )) {
$check = $row['name'];
//When the user is found output the rank in imagestring() then break the loop.
if($check == $username){
imagestring($im, 5, 0, 0, $rank, $textcolor);
}
//If the loop was not broken, increment the rank by 1
$rank++;
}

 

If so would there be any other way? Currently the only other thing that was thought of was to only generate the ranks every 2 hours or so and store it in a row in the db.

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/
Share on other sites

Are the ranks shown on every page?

 

Why not have something inside an include and call that only on the pages that need it?

The ranks are actually generated on a php GD image. And those are used on the Game's Forums, and many people use the player cards, so they're generated very, very frequently.

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709112
Share on other sites

At the end of the day the best solution is to use as little amount of code as is possible.

 

You're looping the table "table" - if you've got a lot of rows in there that's soon going to put a strain on the server...

 

Thats the point of pagination....actually. Which would be more code, but only run x amount per page.

 

Little code as possible is not always a good thing, depending on the circumstances. In this scenario, he would want more code to break up the results and display links to pages to display the other data.

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709122
Share on other sites

At the end of the day the best solution is to use as little amount of code as is possible.

 

You're looping the table "table" - if you've got a lot of rows in there that's soon going to put a strain on the server...

Well, how would I be able to generate the ranks in real time without straining my server?

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709123
Share on other sites

Just had a quick look one thing is the use of checking the username in "table" - if you can check using the user's ID number that would be a LOT quicker.

 

When paginating data the entire table still has to be read and sorted before the results can be returned.

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709129
Share on other sites

Just had a quick look one thing is the use of checking the username in "table" - if you can check using the user's ID number that would be a LOT quicker.

 

When paginating data the entire table still has to be read and sorted before the results can be returned.

 

Yea, but MySQL is built to be amazingly fast and can do this very quick even with hundreds of thousands, even millions of records. The DB won't slow him down one bit, its the generating the image for the whole table/displaying the whole table that will make the script crawl.

 

With the return rows being limited to pagination the script will run smooth. Take this site for example, there are 25 topics per page with 2356 pages, and yet it displays dang quick.

 

That is a total of 58,900 rows, just for this forum, now tally all the forums up together....

 

 

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709133
Share on other sites

You can do this in MySQL

 

You just need to figure out the row number of that requested username which you can do via MySQL

 

You can do something like

 

Find the number of rows that are >= the users EXP

Find the total number of rows

 

2 queries with 2 data points makes a lot more than 1 query with X rows = to the number of users you have

 

 

There may be an alternative MySQL solution but that is the best off the top of my head

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709165
Share on other sites

cooldude832, that's a good solution, but what about indexes?

 

If you index the exp field, everytime the table changes the index will have to be rebuilt, but without an index, a table-wide COUNT could be disastrous.  I guess the table could be replicated or something.  Or, maybe rebuilding the index or doing it without an index wouldn't be too much of a problem.

 

 

Bleh.....  Now this is really bugging me lol.

 

 

Edit:  Just found http://www.oreillynet.com/pub/a/mysql/2007/03/01/optimize-mysql-rank-data.html googling, and finding it quite interesting so far.

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709166
Share on other sites

cooldude832, that's a good solution, but what about indexes?

 

If you index the exp field, everytime the table changes the index will have to be rebuilt, but without an index, a table-wide COUNT could be disastrous.  I guess the table could be replicated or something.  Or, maybe rebuilding the index or doing it without an index wouldn't be too much of a problem.

 

 

Bleh.....  Now this is really bugging me lol.

 

 

Edit:  Just found http://www.oreillynet.com/pub/a/mysql/2007/03/01/optimize-mysql-rank-data.html googling, and finding it quite interesting so far.

 

 

You can always take into account a secondary table to only manage ranks that stores EXP  and UserIDs only so you are working on a smaller table but I see your point

 

Its trial and error when you get into efficiency

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709167
Share on other sites

//Get the name from the URL
$username = mysql_real_escape_string($_REQUEST['name']);

//Get data from the database
$sql = "SELECT v1.name, COUNT(v2.exp) AS rank
          FROM table v1
          JOIN table v2 ON v1.exp < v2.exp
          OR (v1.exp = v2.exp AND v1.name = v2.name)
          GROUP BY v1.name
          HAVING v1.name = '$username'
          ORDER BY rank";

$result = mysql_query($sql) or die(mysql_error()); 
if ($row = mysql_fetch_assoc($result)) {
   imagestring($im, 5, 0, 0, $row['rank'], $textcolor);
} else {
  // username was not found, do something
} // end if..else $row

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-709181
Share on other sites

I gotta image that MySQL has addressed figuring the rows # in a specific query based on the total number of results because it just seems to practical not to have functionalized.

 

 

Just because something is a function doesn't mean it's efficient.

 

 

I wonder how other RDBs do it....

Link to comment
https://forums.phpfreaks.com/topic/136007-is-this-unefficient/#findComment-710110
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.