How to Optimize Query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
fazi
Forum Newbie
Posts: 2
Joined: Thu Mar 13, 2008 5:23 am

How to Optimize Query?

Post 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
Last edited by fazi on Fri Mar 14, 2008 9:26 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to Optimize Query?

Post 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
(#10850)
fazi
Forum Newbie
Posts: 2
Joined: Thu Mar 13, 2008 5:23 am

Re: How to Optimize Query?

Post 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
Post Reply