Page 1 of 1

How to Optimize Query?

Posted: Thu Mar 13, 2008 5:33 am
by fazi
i got a query for creating a dynamicly ranks in my game but I think they are very 'slow'
$nazwy = array('points_fleet_old','points_tech_old','points_builds','points_builds2');
foreach($nazwy as $tab){
$tablica = $db->doquery("SELECT `id` FROM `users` ORDER BY `{$tab}` DESC");
$start = 1;
while($row = $tablica->fetch_array()){
if($row['id'] == $user['id']) {echo $tab.": palce".$start;}
++$start;
}
unset($tablica,$row);
}
little info
$start - is for counting ranks
$nazwy - names of tables in base to search in
tables have indexes

Re: How to Optimize Query?

Posted: Thu Mar 13, 2008 2:56 pm
by Christopher
Why not just?

SELECT `id` FROM {{table}} ORDER BY points_fleet_old DESC, points_tech_old DESC,points_builds DESC, points_builds2 DESC

Re: How to Optimize Query?

Posted: Fri Mar 14, 2008 9:23 am
by fazi
its no difrence cose i have to count every place by start and table.

its shoudl by like this
points_fleet_old: place 490
points_builds place 249
points_tech_old place 37
points_builds2 place 463