![]() Join Up! 96812 members and counting! |
|
|||
MySQL and PHP: How to make it work without killing your server
Introduction
There are currently over 25 Million websites powered by PHP. If you
had the time (and patience, nevermind the means) to check all of
them, you would most certainly find that most use PHP in
conjunction with a MySQL Database. The reasons for this is simple:
they work extremely well together, they are relatively simple to
integrate and they are flexible.
But in PHP's flexibility lies its greatest weakness. Just because it
allows you to do almost anything you like in just about any way you
like, does not mean that you are code is performing at it's best.
If it has not happened to you already, the day will come that you start
to ask yourself why your script is so slow, even on localhost!
Until you properly understand what happens in the background--on the
server--while your code is being processed, you will not have the
tools to eliminate slow server responses. I am not, however, talking
about the actual machine process that happens on the server. I am
simply talking about what happens when you type echo, or print, or
foreach. What are you telling PHP to do?
Just before we actually get into it, a little note: This article
assumes a basic level of understanding of PHP and MYSQL. We will not
be rooting through the basics of coding before getting to our points.
However, I will try to keep things as simple as possible as
this topic is relative to developers of all levels.
PHP: Print, Echo, Strings, Variables and Loops
You will have undoubtably heard of the cardinal rule regarding
echo vs. print. Certainly, I dont think any
discussion on server resources would be complete without it. Its
simple:
The crux of the matter lies in the fact that print always
returns true, so that you can do things like:
$ret will be 1. You can also do things like
On the topic of strings, it is a well known fact that the following
examples are all valid:
Something to remember regarding for loops is that the maximum
boundary should be calculated and stored inside a variable like this:
Something else I would like to touch on lightly before moving on is
that it is heavier on server resources to keep switching php on and off
inside a script than to just print or echo the html. So, instead of this:
MySQL
MySQL queries probably slow down your pageloads more than anything
else. A big server-killer comes from queries inside loops--sometimes
unavoidable but in most cases there are things that could be done
to lighten the server load.
A simple class
When you have built enough websites you will see that you use many
things over and over again. At some point it just becomes simpler to
chuck that into a class and store it in some sort of archive that way you can
dip into whenever you lay out the structure of a new site. Being an
OOP junkie, I have a simple class that first defines the array
which creates my database connection.
Data is data
When you get to the point that you are using the same data over and
over on the same page, the time has come to save it into an array which
can be referenced over repeatedly without having the resource of query
after query wasted.
Begin by building the query from what you know you will need from
the database. Let us assume we are going to print out user profile
information for a specific user from the user table of our database.
I did not use select *
This is important. If you dont need every single field from the database, dont waste server time by asking for it. Select only the fields you need. I used limit 1
This speeds your query up nicely. If you are only looking for a single row as a result, using limit 1 simply tells mysql
to stop looking once it has found a result. Not doing so means that
MySQL will continue looping through the table anyway, wasting
time and resources for no reason.
Now I have all the table data I will require safely stored in an array
that I can reference any time I need it. The format of the array will
look like this:
John Arbuckle (32) Kevin Smith (28) Mike Carrigan (43) And of course you could do tons more with that array, and you should not need much more than that in order to get the results you need.
In Conclusion
In this article we explored a few techniques that will not only improve
our coding style, but help to make our scripts execute faster. We also
had a short peak at a database management class that we will hopefully
be able to have a closer look at in the near future. Part of our
database optimization showed how to store database results in an array,
thereby making scripts execute faster because there is no need for repeat
queries. We also had a brief look at how to manipulate the resulting arrays.
Until next time, Good Luck! Marc Steven Plotz |