Welcome to the Browser-Base Game Zone forums!
//...while($row=mysql_fetch_array(...)){...}
Am I doing it right?
$bestsword=$bestshield=0;while($row=mysql_fetch_array(...)){if($row['type']==SWORD && $row['level']>$bestsword) $bestsword=$row['level'];if($row['type']==SHIELD && $row['level']>$bestshield) $bestshield=$row['level'];}
Quote from: CygnusX on July 06, 2011, 09:22:09 AMAm I doing it right?Not really Maybe I will post a simple solution for 1 best selection so it become more clear.Code: [Select]$bestsword=$bestshield=0;while($row=mysql_fetch_array(...)){if($row['type']==SWORD && $row['level']>$bestsword) $bestsword=$row['level'];if($row['type']==SHIELD && $row['level']>$bestshield) $bestshield=$row['level'];}So, selecting "one best" is trivial (the fastest algorithm is obvious) but it becomes more complex for "select sum of 5 best" and when items are stacked.
Question, why would you handle the comparisons between their levels and such with PHP, versus the SQL query?
Also, using mysql_fetch_array() is fine, but it creates 2 data sets, an associative array, and an indexed array, why not just use mysql_fetch_assoc() if you're going to use associative naming?
Because PHP is incomparably faster than SQL. It is almost impossible to code something in PHP as unoptimized as to make it slower than additional SQL query.
Quote from: V3RTEXGaming on July 07, 2011, 01:25:42 PMQuestion, why would you handle the comparisons between their levels and such with PHP, versus the SQL query?Because PHP is incomparably faster than SQL. It is almost impossible to code something in PHP as unoptimized as to make it slower than additional SQL query.
SELECT players.id, items.id, items.name, items.type FROM playersINNER JOIN itemsON players.id = items.owner_idWHERE players.id = 12576 AND items.type = 'SWORD' AND items.equipped = 0 AND items.level > (SELECT items.level FROM players INNER JOIN items ON players.id = items.owner_id WHERE items.equipped = 1 AND items.type = 'SWORD')
Quote from: V3RTEXGaming on July 07, 2011, 01:25:42 PMAlso, using mysql_fetch_array() is fine, but it creates 2 data sets, an associative array, and an indexed array, why not just use mysql_fetch_assoc() if you're going to use associative naming?ROFL, because I never thought about it Everyone was using mysql_fetch_array so I was using it too (it's really funny no one on this forum pointed this out over these years). You are absolutely correct, using mysql_fetch_array() is stupid in more than 99% of cases.REP +1
Quote from: Chris on July 07, 2011, 02:09:15 PMQuote from: V3RTEXGaming on July 07, 2011, 01:25:42 PMQuestion, why would you handle the comparisons between their levels and such with PHP, versus the SQL query?Because PHP is incomparably faster than SQL. It is almost impossible to code something in PHP as unoptimized as to make it slower than additional SQL query.I wouldn't be so quick to say PHP is incomparably faster than SQL. I can run some benchmarks, but not yet. From what I've read in this thread, I don't think you'll need an extra query to do what you're trying to do.
Code: [Select]SELECT players.id, items.id, items.name, items.type FROM playersINNER JOIN itemsON players.id = items.owner_idWHERE players.id = 12576 AND items.type = 'SWORD' AND items.equipped = 0 AND items.level > (SELECT items.level FROM players INNER JOIN items ON players.id = items.owner_id WHERE items.equipped = 1 AND items.type = 'SWORD')The above probably won't work, and is simply to be used as an example, that you don't need 2 queries to figure it out, using a single query can get the job done, without the overhead of php trying to figure everything out for you, and save you the trouble of pulling loads of unneeded data from the DB.
There was a topic about this on the forum. Various game owners were comparing their history of performance problems and the unquestionable conclusion was that all problems were always caused by MySQL (only codestryke managed to lag his server by PHP once, but he really went overboard and was young and unsmart back then so it can't really count ).
Quote from: Chris on July 08, 2011, 10:35:48 AMThere was a topic about this on the forum. Various game owners were comparing their history of performance problems and the unquestionable conclusion was that all problems were always caused by MySQL (only codestryke managed to lag his server by PHP once, but he really went overboard and was young and unsmart back then so it can't really count ).I did this by having to many loops, which is basically what you are considering. I also showed in a previous how a query is indeed faster then PHP allocating memory for the array and then processing the array.
Joins are NOT like adding another query, if the join is indexed then the added time is trivial.
SELECT MAX(level) FROM items WHERE playerid = ? GROUP BY typeYou have the best level for every type of group and that would be faster then PHP if indexed correctly
So you could either sort it in the query or sort it with php.
$bestsword=$bestshield=0;$dogs=array(); $falcons=array();while($row=mysql_fetch_array(...)){if($row['type']==SWORD && $row['level']>$bestsword) $bestsword=$row['level'];if($row['type']==SHIELD && $row['level']>$bestshield) $bestshield=$row['level'];if($row['type']==DOG) // add it to $dogs array here ($row['level'] and $row['quantity'])if($row['type']==FALCON) // add it to $falcons array here ($row['level'] and $row['quantity'])}$sumof5bestdogs=_SOME_SORT_FUNCTION($dogs)_$sumof5bestfalcons=_SOME_SORT_FUNCTION($falcons)_
<? $dblink = mysql_connect('127.0.0.1', 'root', ''); if( !$dblink ) die('Could not connect: ' . mysql_error()); $db = mysql_select_db('test', $dblink); if( !$db ) die ('Can\'t use foo : ' . mysql_error()); //make(); //exit; $num_tests = 1000; $i = 0; $time_start = microtime(true); while( $i < $num_tests ) { test_db(); ++$i; } echo '<p>DB Execution time: ', (microtime(true) - $time_start), '</p>'; $i = 0; $time_start = microtime(true); while( $i < $num_tests ) { test_array(); ++$i; } echo '<p>Array Execution time: ', (microtime(true) - $time_start), '</p>'; function test_array() { global $db; $best = array(); $rs = mysql_query('SELECT level, typeid FROM inventory WHERE playerid = ' . mt_rand(1, 200)); while( $row = mysql_fetch_assoc($rs) ) { if( $row['typeid'] == 1 && $row['level'] > $best['sword']) $best['sword'] = $row['level']; if( $row['typeid'] == 2 && $row['level'] > $best['armor']) $best['armor'] = $row['level']; if( $row['typeid'] == 3 && $row['level'] > $best['ring']) $best['ring'] = $row['level']; if( $row['typeid'] == 4 && $row['level'] > $best['dog']) $best['dog'] = $row['level']; if( $row['typeid'] == 5 && $row['level'] > $best['shoes']) $best['shoes'] = $row['level']; } } function test_db() { global $db; $best = array(); $rs = mysql_query('SELECT MAX(level) FROM inventory WHERE playerid = '.mt_rand(1, 200).' GROUP BY typeid'); while( $row = mysql_fetch_assoc($rs) ) { if( $row['typeid'] == 1 ) $best['sword'] = $row['level']; if( $row['typeid'] == 2 ) $best['armor'] = $row['level']; if( $row['typeid'] == 3 ) $best['ring'] = $row['level']; if( $row['typeid'] == 4 ) $best['dog'] = $row['level']; if( $row['typeid'] == 5 ) $best['shoes'] = $row['level']; } } function make() { global $db; mysql_query('DELETE FROM inventory'); $num_players = 5000; $num_levels = 20; $items = array(1 => 'sword', 'armor', 'ring', 'dog', 'shoes'); for($i = 1; $i < $num_players+1; $i++ ) { for($j = 1; $j < count($items)+1; $j++ ) { for($k = 1; $k < $num_levels+1; $k++ ) { mysql_query('INSERT INTO inventory (playerid, typeid, name, level) VALUES ('.$i.', '.$j.', "'.$items[$j].' '.$k.'", '.$k.')'); } } } }?>
CREATE TABLE IF NOT EXISTS `inventory` ( `playerid` int(10) unsigned NOT NULL, `typeid` int(10) unsigned NOT NULL, `name` char(10) NOT NULL, `level` tinyint(3) unsigned NOT NULL, KEY `playerid` (`playerid`)) ENGINE=MyISAM;