Jump to content
MakeWebGames

Effienciecy


Recommended Posts

For the last few hours I've been going through my game trying to make the mysql queries efficient.

Now I've come down the v1 hall of fame, and i realise this kills my server.

"This page loaded in 15.25 seconds."

For total stats and best strength, defense etc.

The query used for total stats is:

$q=mysql_query("SELECT u.username, u.userid, g.gangPREF FROM `users` u LEFT JOIN userstats us ON
u.userid=us.userid LEFT JOIN gangs g ON g.gangID=u.gang ORDER BY 
(us.might+us.stealth+us.defence+us.force+us.IQ) DESC, u.userid ASC LIMIT 20", $c);

 

I then fetch it using mysql_fetch_array();.

Any idea how i could make this query more efficient? I'm selecting only the columns i'll need (i think).

Also, if you have any other tips on MySQL efficiencies i would be grateful :).

Thanks.

Link to comment
Share on other sites

Re: Effienciecy

$q=mysql_query("SELECT u.username, u.userid, g.gangPREF FROM `users` u

LEFT JOIN userstats us ON u.userid=us.userid

LEFT JOIN gangs g ON g.gangID=u.gang

ORDER BY (us.might+us.stealth+us.defence+us.force+us.IQ) DESC, u.userid ASC LIMIT 20", $c);

You could take that query and try this:

$q=mysql_query("SELECT u.username, u.userid, g.gangPREF FROM `users` u

LEFT JOIN gangs g ON g.gangID=u.gang

order by (select (us.might+us.stealth+us.defence+us.force+us.IQ) from userstats as us where u.userid = us.userid) DESC, u.userid ASC LIMIT 20", $c);

I'm not guaranteeing that that will be faster, but it's another way to do the same thing.

Another thing you can do is put an index on columns you use to match things with.

u.gang would be a good one.

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...