PHP quicker Query

C++, C#, Java, PHP, ect...
User avatar
Bane_Star
Posts: 47
Joined: Fri Apr 23, 2010 4:31 pm

PHP quicker Query

Post by Bane_Star »

I've noticed that people are still making their Queries directly in code, Its time consuming,

Can I suggest, In your Connect file, add the following function

The first Connects to the DB, and returns the single line

i.e. $player = callData("SELECT * FROM database WHERE playerID = 1");

Code: Select all

function callData($DataString) {

$query = $DataString;

$result = mysql_query($query);
	if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
    }
    
$fullData = mysql_fetch_object($result);
Allowing the user to grab objects $player->name or $player->password.

The second grabs a large selection and returns an Array of objects

Code: Select all

return $fullData;
}

function callFullData($DataString) {

$query = $DataString;

$result = mysql_query($query);
	if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
    }
    
$fullData = $result;

return $fullData;
}
Allowing the user to grab objects $player[0]->name or $player[1]->password.
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Re: PHP quicker Query

Post by MAruz »

Wont this be a larger strain on the server? I mean, often you will get allot more data than you actually need when using this techique...
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP quicker Query

Post by hallsofvallhalla »

I have often been slammed for my non use of functions and includes and such. I rewrite queries over and over and sometimes code.

I have done it the "right" way, write a function then transfer all the variables through the function call, create classes and instances of the classes blah blah blah...

I am not a fan of that and they both get parsed the same so they both run the same speed. It is all about preference and who will be looking at your code.

My code is praised by people trying to learn code and hated by people who know code. Since I write tutorials it is better for me to write out everything. Plus I can find problems 100X easier when everything is written out.

You can do it either way it is all preference. Thanks for posting this though, there are those here that do want their code shrunk up for ease.

Edit: i do stil use functions and classes...just not that often. :)
User avatar
Bane_Star
Posts: 47
Joined: Fri Apr 23, 2010 4:31 pm

Re: PHP quicker Query

Post by Bane_Star »

I don't mean to say this is right or this is wrong, but more that I'm lazy and I don't like to write things over and over again, especially things like

Code: Select all

or die("message XYZ you did this wrong blah");
It just gets annoying.

As to server strain.. If you word your SQL call correctly, use joins and clever selections, you can reduce the data called from the DB.

I make calls to the DB for 10,000 objects, each with 12 points of data, some varchars of 50, It takes less than a microsecond to make the call, usually PHP sorting variables & storing data is the main problem,

Biggest slow downs, Like a lightblub to electricity, the main cost in power is not keeping it on, but the inital, turning on, or turning off.. The SQL main cost in server time is not grabbing out the data but mostly opening the DB and closing it afterwards.

Also storing variables, especially multiples.. from what I've been able to determine,

Code: Select all

$bob1 = 2;
$bob2 = $bob1+4;
$bob3 = $bob2+6;
Costs alot more server time than

Code: Select all

$bob = 2;
$bob += 4;
$bob += 6;
I definitely don't want to step on any toes, I too am self taught, I'm hoping that little tricks I learn't to save time and server time will be useful for others who also seek to do the same as me.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP quicker Query

Post by hallsofvallhalla »

wow yeah thanks for the info. I am all for info on making queries faster!
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP quicker Query

Post by Jackolantern »

Bane_Star wrote: Also storing variables, especially multiples.. from what I've been able to determine,

Code: Select all

$bob1 = 2;
$bob2 = $bob1+4;
$bob3 = $bob2+6;
Costs alot more server time than

Code: Select all

$bob = 2;
$bob += 4;
$bob += 6;
This is just syntactic sugar. The only gain would be in script parsing time, which would be minimal. The underlying code works exactly the same way, since you are just adding numbers to a variable. If you are using the Zend PHP compiler/cache, it would compile to the exact same machine code.

EDIT: Oops, read over it a bit fast and didn't see it was making a new variable in each line. Your right. I thought you had typed:

Code: Select all

$bob = $bob +1;
$bob = $bob +2;
The indelible lord of tl;dr
User avatar
Bane_Star
Posts: 47
Joined: Fri Apr 23, 2010 4:31 pm

Re: PHP quicker Query

Post by Bane_Star »

I'd like to address the concept of minimal..

Lets extrapolate some concepts with conservative figures..

The average simple online game has (if it lasts a year or more) around 1 - 2,000 players. Some decent games more than that..

Most games have a tendency for full page loads, and from some stat sheets I've seen, an average of 30-60 loads per hour.

If 50% of your players are in the same timezone, and are in the same or similar geo-economical zone, (which is likely in the case of MMO's) it means that alot of people are playing around the same time of day,

30 sessions x 500 players, 18k x any 'minimal' cost in delays..

1k of text on ADSL is 0.0016 seconds, 1 single char of error in your code .. = 0.024 of time for these 600 online players.

To avoid breaking the immersion feel, nothing above 0.10 of a second for any given player.. thats less than 4 char errors.. !!

Now there are of course many ways to improve things beyond code, but talking code alone, cutting down on errors will improve your players 'feeling' while playing the game.. (ways to improve: http://www.die.net/musings/page_load_time/ )

of course, while we develop games and have less than 50 players, and our games are just simple concepts.. a 1 second delay is barely noticed.. to us..

Imagine, coding 100 pages of game, with 30+ errors in each, each taking an hour to go back and find, and potentially 10% creating further errors, or 1% game crashing errors.. 3300 errors and 33 chances of crashing your game while finding them.. plus the fact that you slow down your game a full second for each page load..

sorry about the large post, but these are some of the things I think about when I'm coding up a script or a new page design..
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP quicker Query

Post by hallsofvallhalla »

excellent things to think about.

One thing though is if you have 100 pages you should definitely think about combining some of those pages. The browser will cache much to help slow down those loading times. In my web game Forsaken Sanctum I have several thousand lines of code stuffed into a few pages and had about 350 players in alpha. Things actually ran very smooth. Much smoother than the older version that was about 50 pages and took.

There is a balance though to too many pages and too much to parse ;)
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP quicker Query

Post by Jackolantern »

Bane_Star (or anyone else familiar), what do you mean by "1 char of error"?
The indelible lord of tl;dr
User avatar
Bane_Star
Posts: 47
Joined: Fri Apr 23, 2010 4:31 pm

Re: PHP quicker Query

Post by Bane_Star »

char = character (varchar definition?)

i.e. without compression I believe variable names take up memory based on the name itself. something like $variableOfDetermination could be just $VoD and same your server some calculation time.

Sending a single character in text, like a space or carriage return. I don't use Image tags if I use an Image twice, because Chat for Char an Image call uses on average MORE characters of text than just telling a div what class it is, and assigning the css appropriately.

I'm not professional, just paraphrasing from things I've been reading a few months ago..
Post Reply

Return to “Coding”