Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Which is faster - MySQL query, or PHP loop

 
   Database Help (Home) -> mySQL RSS
Next:  32- or 64-bit IDS?  
Author Message
Jason Carlton

External


Since: Dec 12, 2007
Posts: 7



(Msg. 1) Posted: Fri Jan 29, 2010 8:47 pm
Post subject: Which is faster - MySQL query, or PHP loop
Archived from groups: comp>databases>mysql (more info?)

In theory, which of these would be faster? Assume a few thousands
rows:

$query = "SELECT category FROM classifieds;
$sth_classifieds = mysql_query($query);

while (list($category) = mysql_fetch_array($sth_classifieds)) {
if (isset($num_cats[$category])) $num_cats[$category]++;
else $num_cats[$category] = 1;
}


Or:

$query = "SELECT COUNT( category ) AS nums, category FROM classifieds
GROUP BY category";
$sth_classifieds = mysql_query($query);

while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
$num_cats[$category] = $nums;


The query itself is about 10 times faster for the first as for the
second, but since the loop is a lot larger I'm not sure if the final
result would be faster.

 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
Jason Carlton

External


Since: Dec 12, 2007
Posts: 7



(Msg. 2) Posted: Sat Jan 30, 2010 1:10 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 30, 3:24 am, Robert Hairgrove wrote:
> Jason Carlton wrote:
> > In theory, which of these would be faster? Assume a few thousands
> > rows:
>
> > $query = "SELECT category FROM classifieds;
> >   $sth_classifieds = mysql_query($query);
>
> > while (list($category) = mysql_fetch_array($sth_classifieds)) {
> >   if (isset($num_cats[$category])) $num_cats[$category]++;
> >   else $num_cats[$category] = 1;
> > }
>
> > Or:
>
> > $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
> > GROUP BY category";
> >   $sth_classifieds = mysql_query($query);
>
> > while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
> >   $num_cats[$category] = $nums;
>
> > The query itself is about 10 times faster for the first as for the
> > second, but since the loop is a lot larger I'm not sure if the final
> > result would be faster.
>
> Your first query is fetching all rows from table classifieds and
> counting them row by row; i.e. might as well use "SELECT COUNT(*) FROM
> classifieds;"
>
> The second is doing something entirely different.

The result for both is actually the same. I'm counting the number of
rows in each category, then assigning that number to $num_cats
[$category] (where category is assigned previously in the script). The
first snippet results in 5,000 rows to loop through, while the second
results in 12.

With around 5,000 rows, and on a slow server, the first snippet takes
0.0014 seconds to complete, while the second takes 0.013 seconds to
complete. During peak hours, though, I might have 800 requests/sec, so
that difference can add up. I'm just not sure whether looping through
5,000 rows in PHP would take long enough to offset the time gain of
the query.

 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
Robert Hairgrove

External


Since: Nov 19, 2009
Posts: 6



(Msg. 3) Posted: Sat Jan 30, 2010 4:26 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jason Carlton wrote:
> In theory, which of these would be faster? Assume a few thousands
> rows:
>
> $query = "SELECT category FROM classifieds;
> $sth_classifieds = mysql_query($query);
>
> while (list($category) = mysql_fetch_array($sth_classifieds)) {
> if (isset($num_cats[$category])) $num_cats[$category]++;
> else $num_cats[$category] = 1;
> }
>
>
> Or:
>
> $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
> GROUP BY category";
> $sth_classifieds = mysql_query($query);
>
> while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
> $num_cats[$category] = $nums;
>
>
> The query itself is about 10 times faster for the first as for the
> second, but since the loop is a lot larger I'm not sure if the final
> result would be faster.

Your first query is fetching all rows from table classifieds and
counting them row by row; i.e. might as well use "SELECT COUNT(*) FROM
classifieds;"

The second is doing something entirely different.
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
Erick T. Barkhuis

External


Since: Jan 30, 2010
Posts: 1



(Msg. 4) Posted: Sat Jan 30, 2010 6:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jason Carlton:


>With around 5,000 rows, and on a slow server, the first snippet takes
>0.0014 seconds to complete, while the second takes 0.013 seconds to
>complete.

Have you eliminated possible caching influences?

>During peak hours, though, I might have 800 requests/sec, so
>that difference can add up.

800 * 0.0014 = 1.12
800 * 0.013 = 10.4

Either way, you will not be able to process 800 requests per second.
Which website is this, anyway, please?


> I'm just not sure whether looping through
>5,000 rows in PHP would take long enough to offset the time gain of
>the query.

Why don't you just time that, then? I mean: you already have the
numbers from the MySql-variant, so why not collect the same numbers
from the PHP-side?
Get microtime(), run through your loop and get microtime() again.




--
Erick
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
Luuk

External


Since: Nov 02, 2008
Posts: 17



(Msg. 5) Posted: Sat Jan 30, 2010 6:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Op 30-1-2010 5:47, Jason Carlton schreef:
> In theory, which of these would be faster? Assume a few thousands
> rows:
>
> $query = "SELECT category FROM classifieds;
> $sth_classifieds = mysql_query($query);
>
> while (list($category) = mysql_fetch_array($sth_classifieds)) {
> if (isset($num_cats[$category])) $num_cats[$category]++;
> else $num_cats[$category] = 1;
> }
>
>
> Or:
>
> $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
> GROUP BY category";
> $sth_classifieds = mysql_query($query);
>
> while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
> $num_cats[$category] = $nums;
>
>
> The query itself is about 10 times faster for the first as for the
> second, but since the loop is a lot larger I'm not sure if the final
> result would be faster.

And, i hope, there is an index on `category` ?

Your 1st query give more traffic between you MySQL-server and you
PHP/WEB-server, less load on MySQL, but more on PHP

The second gives less traffice, but more load on MySQL, and less on PHP

What is your setup? Do you have two physical servers, 1 database, and 1
web? Or is all on the same system?

--
Luuk
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 6) Posted: Sat Jan 30, 2010 7:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jason Carlton wrote:
> On Jan 30, 3:24 am, Robert Hairgrove wrote:
>> Jason Carlton wrote:
>>> In theory, which of these would be faster? Assume a few thousands
>>> rows:
>>> $query = "SELECT category FROM classifieds;
>>> $sth_classifieds = mysql_query($query);
>>> while (list($category) = mysql_fetch_array($sth_classifieds)) {
>>> if (isset($num_cats[$category])) $num_cats[$category]++;
>>> else $num_cats[$category] = 1;
>>> }
>>> Or:
>>> $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
>>> GROUP BY category";
>>> $sth_classifieds = mysql_query($query);
>>> while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
>>> $num_cats[$category] = $nums;
>>> The query itself is about 10 times faster for the first as for the
>>> second, but since the loop is a lot larger I'm not sure if the final
>>> result would be faster.
>> Your first query is fetching all rows from table classifieds and
>> counting them row by row; i.e. might as well use "SELECT COUNT(*) FROM
>> classifieds;"
>>
>> The second is doing something entirely different.
>
> The result for both is actually the same. I'm counting the number of
> rows in each category, then assigning that number to $num_cats
> [$category] (where category is assigned previously in the script). The
> first snippet results in 5,000 rows to loop through, while the second
> results in 12.
>
> With around 5,000 rows, and on a slow server, the first snippet takes
> 0.0014 seconds to complete, while the second takes 0.013 seconds to
> complete. During peak hours, though, I might have 800 requests/sec, so
> that difference can add up. I'm just not sure whether looping through
> 5,000 rows in PHP would take long enough to offset the time gain of
> the query.

Probably not.

Also beware of memory usage.

I have PHP code that downsizes images to thumbnails to speed up
transfers to slow bandwidth clients. It worked brilliantly, until I gave
it a massive image to process. It wasn't juts N times slower, in was
hugely slower. Many seconds. I suspect it ran into PHP memory limits and
swapped..

Its probably worth using php to do work the database does badly, but I'd
never use it to do what the database does well..
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 7) Posted: Sat Jan 30, 2010 8:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> Jason Carlton wrote:
>> On Jan 30, 3:24 am, Robert Hairgrove wrote:
>>> Jason Carlton wrote:
>>>> In theory, which of these would be faster? Assume a few thousands
>>>> rows:
>>>> $query = "SELECT category FROM classifieds;
>>>> $sth_classifieds = mysql_query($query);
>>>> while (list($category) = mysql_fetch_array($sth_classifieds)) {
>>>> if (isset($num_cats[$category])) $num_cats[$category]++;
>>>> else $num_cats[$category] = 1;
>>>> }
>>>> Or:
>>>> $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
>>>> GROUP BY category";
>>>> $sth_classifieds = mysql_query($query);
>>>> while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
>>>> $num_cats[$category] = $nums;
>>>> The query itself is about 10 times faster for the first as for the
>>>> second, but since the loop is a lot larger I'm not sure if the final
>>>> result would be faster.
>>> Your first query is fetching all rows from table classifieds and
>>> counting them row by row; i.e. might as well use "SELECT COUNT(*) FROM
>>> classifieds;"
>>>
>>> The second is doing something entirely different.
>>
>> The result for both is actually the same. I'm counting the number of
>> rows in each category, then assigning that number to $num_cats
>> [$category] (where category is assigned previously in the script). The
>> first snippet results in 5,000 rows to loop through, while the second
>> results in 12.
>>
>> With around 5,000 rows, and on a slow server, the first snippet takes
>> 0.0014 seconds to complete, while the second takes 0.013 seconds to
>> complete. During peak hours, though, I might have 800 requests/sec, so
>> that difference can add up. I'm just not sure whether looping through
>> 5,000 rows in PHP would take long enough to offset the time gain of
>> the query.
>
> Probably not.
>
> Also beware of memory usage.
>
> I have PHP code that downsizes images to thumbnails to speed up
> transfers to slow bandwidth clients. It worked brilliantly, until I gave
> it a massive image to process. It wasn't juts N times slower, in was
> hugely slower. Many seconds. I suspect it ran into PHP memory limits and
> swapped..

If it hits the php memory limit, it will stop executing the script.

Swapping will happen depending on your swappiness setting, the amount ram you
have and how you have configured your web service (it's quite easy to
configure it to use more memory than you have on your machine and will slow
down things).

I have been working with similar things myself, where images where downsized
to proper size for cellphone screens, the trick is really to have a script
that generates the most common of those thumbnails, but let the displaying
script to have the possibility to regenerate the thumbnail if it would get
lost. Otherwise you have extreme load on the server after you add a lot of new
images to the system.


> Its probably worth using php to do work the database does badly, but I'd
> never use it to do what the database does well..

When doing the same query many times, it's worth to think about using
memcached to store the result, just see to remove the cached data when you
make an update.


--

//Aho
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 8) Posted: Sat Jan 30, 2010 10:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

J.O. Aho wrote:
> The Natural Philosopher wrote:
>> Jason Carlton wrote:
>>> On Jan 30, 3:24 am, Robert Hairgrove wrote:
>>>> Jason Carlton wrote:
>>>>> In theory, which of these would be faster? Assume a few thousands
>>>>> rows:
>>>>> $query = "SELECT category FROM classifieds;
>>>>> $sth_classifieds = mysql_query($query);
>>>>> while (list($category) = mysql_fetch_array($sth_classifieds)) {
>>>>> if (isset($num_cats[$category])) $num_cats[$category]++;
>>>>> else $num_cats[$category] = 1;
>>>>> }
>>>>> Or:
>>>>> $query = "SELECT COUNT( category ) AS nums, category FROM classifieds
>>>>> GROUP BY category";
>>>>> $sth_classifieds = mysql_query($query);
>>>>> while (list($nums, $category) = mysql_fetch_array($sth_classifieds))
>>>>> $num_cats[$category] = $nums;
>>>>> The query itself is about 10 times faster for the first as for the
>>>>> second, but since the loop is a lot larger I'm not sure if the final
>>>>> result would be faster.
>>>> Your first query is fetching all rows from table classifieds and
>>>> counting them row by row; i.e. might as well use "SELECT COUNT(*) FROM
>>>> classifieds;"
>>>>
>>>> The second is doing something entirely different.
>>> The result for both is actually the same. I'm counting the number of
>>> rows in each category, then assigning that number to $num_cats
>>> [$category] (where category is assigned previously in the script). The
>>> first snippet results in 5,000 rows to loop through, while the second
>>> results in 12.
>>>
>>> With around 5,000 rows, and on a slow server, the first snippet takes
>>> 0.0014 seconds to complete, while the second takes 0.013 seconds to
>>> complete. During peak hours, though, I might have 800 requests/sec, so
>>> that difference can add up. I'm just not sure whether looping through
>>> 5,000 rows in PHP would take long enough to offset the time gain of
>>> the query.
>> Probably not.
>>
>> Also beware of memory usage.
>>
>> I have PHP code that downsizes images to thumbnails to speed up
>> transfers to slow bandwidth clients. It worked brilliantly, until I gave
>> it a massive image to process. It wasn't juts N times slower, in was
>> hugely slower. Many seconds. I suspect it ran into PHP memory limits and
>> swapped..
>
> If it hits the php memory limit, it will stop executing the script.
>

You are probably right: what is most likely happening is that Linux
needs too make extra RAM available to PHP, and has to flush some buffers.


> Swapping will happen depending on your swappiness setting, the amount ram you
> have and how you have configured your web service (it's quite easy to
> configure it to use more memory than you have on your machine and will slow
> down things).
>
> I have been working with similar things myself, where images where downsized
> to proper size for cellphone screens, the trick is really to have a script
> that generates the most common of those thumbnails, but let the displaying
> script to have the possibility to regenerate the thumbnail if it would get
> lost. Otherwise you have extreme load on the server after you add a lot of new
> images to the system.
>
Oh no, these are done on the fly every time a query is done on that image.

I have traded CPU power for bandwidth. At the scale of the site, its
more optimal.

If the site settles down, i might then rescale and store the thumbnails.
but right now they are used at three different sizes at least, and its
getting a pain to store different thumbnails at all sizes.


>
>> Its probably worth using php to do work the database does badly, but I'd
>> never use it to do what the database does well..
>
> When doing the same query many times, it's worth to think about using
> memcached to store the result, just see to remove the cached data when you
> make an update.
>
>

SQL does that anyway, all by itself.Makes the second identical 10 second
query happen in a flash!

Until you change a record...Sad
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 9) Posted: Sat Jan 30, 2010 12:25 pm
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> J.O. Aho wrote:
>> The Natural Philosopher wrote:

>> Swapping will happen depending on your swappiness setting, the amount
>> ram you
>> have and how you have configured your web service (it's quite easy to
>> configure it to use more memory than you have on your machine and will
>> slow
>> down things).
>>
>> I have been working with similar things myself, where images where
>> downsized
>> to proper size for cellphone screens, the trick is really to have a
>> script
>> that generates the most common of those thumbnails, but let the
>> displaying
>> script to have the possibility to regenerate the thumbnail if it would
>> get
>> lost. Otherwise you have extreme load on the server after you add a
>> lot of new
>> images to the system.
>>
> Oh no, these are done on the fly every time a query is done on that image.
> I have traded CPU power for bandwidth. At the scale of the site, its
> more optimal.

Thats just crazy, why generate a new thumbnail of an image if you already have
med it once, save the thumbnail and you save loads of CPU cycles.



> If the site settles down, i might then rescale and store the thumbnails.
> but right now they are used at three different sizes at least, and its
> getting a pain to store different thumbnails at all sizes.

On the site I worked on, for each image there was 5 different w/h ratio images
which had been cut from the original image and each of those images where
rescaled to the right size depending on the cellphones screen size.
As there are a lot of different cellphones with different screen resolutions,
there was quite many different thumbnails for the same image.

If image hadn't been auto generated ahead, the "web page" generated it, but
then went everything quite slowly and high CPU load, as for you. Of course we
didn't generate thumbnails for the uncommon screen resolutions, as we could
save storage space that way. In your case I think you only have one thumbnail
for each image, which will not be that much storage space needing.


>>> Its probably worth using php to do work the database does badly, but I'd
>>> never use it to do what the database does well..
>>
>> When doing the same query many times, it's worth to think about using
>> memcached to store the result, just see to remove the cached data when
>> you
>> make an update.
>>
> SQL does that anyway, all by itself.Makes the second identical 10 second
> query happen in a flash!
>
> Until you change a record...Sad
>

MySql has some caching features, put those are far short time, if you have a
busy site, mysql may not have a heavy query cashed anymore a hour later, while
using memcached it wouldn't be a problem.

--

//Aho
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 10) Posted: Sat Jan 30, 2010 8:25 pm
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

J.O. Aho wrote:
> The Natural Philosopher wrote:
>> J.O. Aho wrote:
>>> The Natural Philosopher wrote:
>
>>> Swapping will happen depending on your swappiness setting, the amount
>>> ram you
>>> have and how you have configured your web service (it's quite easy to
>>> configure it to use more memory than you have on your machine and will
>>> slow
>>> down things).
>>>
>>> I have been working with similar things myself, where images where
>>> downsized
>>> to proper size for cellphone screens, the trick is really to have a
>>> script
>>> that generates the most common of those thumbnails, but let the
>>> displaying
>>> script to have the possibility to regenerate the thumbnail if it would
>>> get
>>> lost. Otherwise you have extreme load on the server after you add a
>>> lot of new
>>> images to the system.
>>>
>> Oh no, these are done on the fly every time a query is done on that image.
>> I have traded CPU power for bandwidth. At the scale of the site, its
>> more optimal.
>
> Thats just crazy, why generate a new thumbnail of an image if you already have
> med it once, save the thumbnail and you save loads of CPU cycles.
>

To save storage.

CPU is more than adequate. Backing up the storage is not.

>
>
>> If the site settles down, i might then rescale and store the thumbnails.
>> but right now they are used at three different sizes at least, and its
>> getting a pain to store different thumbnails at all sizes.
>
> On the site I worked on, for each image there was 5 different w/h ratio images
> which had been cut from the original image and each of those images where
> rescaled to the right size depending on the cellphones screen size.
> As there are a lot of different cellphones with different screen resolutions,
> there was quite many different thumbnails for the same image.
>
> If image hadn't been auto generated ahead, the "web page" generated it, but
> then went everything quite slowly and high CPU load, as for you. Of course we
> didn't generate thumbnails for the uncommon screen resolutions, as we could
> save storage space that way. In your case I think you only have one thumbnail
> for each image, which will not be that much storage space needing.
>

No, I have currently at lest three.


>
>>>> Its probably worth using php to do work the database does badly, but I'd
>>>> never use it to do what the database does well..
>>> When doing the same query many times, it's worth to think about using
>>> memcached to store the result, just see to remove the cached data when
>>> you
>>> make an update.
>>>
>> SQL does that anyway, all by itself.Makes the second identical 10 second
>> query happen in a flash!
>>
>> Until you change a record...Sad
>>
>
> MySql has some caching features, put those are far short time, if you have a
> busy site, mysql may not have a heavy query cashed anymore a hour later, while
> using memcached it wouldn't be a problem.
>

Then allocate more memory to mysql.
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 11) Posted: Sun Jan 31, 2010 2:28 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> J.O. Aho wrote:
>> The Natural Philosopher wrote:
>>> J.O. Aho wrote:
>>>> The Natural Philosopher wrote:
>>
>>>>> Its probably worth using php to do work the database does badly,
>>>>> but I'd
>>>>> never use it to do what the database does well..
>>>> When doing the same query many times, it's worth to think about using
>>>> memcached to store the result, just see to remove the cached data when
>>>> you
>>>> make an update.
>>>>
>>> SQL does that anyway, all by itself.Makes the second identical 10 second
>>> query happen in a flash!
>>>
>>> Until you change a record...Sad
>>>
>>
>> MySql has some caching features, put those are far short time, if you
>> have a
>> busy site, mysql may not have a heavy query cashed anymore a hour
>> later, while
>> using memcached it wouldn't be a problem.
>>
>
> Then allocate more memory to mysql.

You still have the problem that you won't know if your query is stored in the
query cache or not.

--

//Aho
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 12) Posted: Sun Jan 31, 2010 7:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

J.O. Aho wrote:
> The Natural Philosopher wrote:
>> J.O. Aho wrote:
>>> The Natural Philosopher wrote:
>>>> J.O. Aho wrote:
>>>>> The Natural Philosopher wrote:
>>>>>> Its probably worth using php to do work the database does badly,
>>>>>> but I'd
>>>>>> never use it to do what the database does well..
>>>>> When doing the same query many times, it's worth to think about using
>>>>> memcached to store the result, just see to remove the cached data when
>>>>> you
>>>>> make an update.
>>>>>
>>>> SQL does that anyway, all by itself.Makes the second identical 10 second
>>>> query happen in a flash!
>>>>
>>>> Until you change a record...Sad
>>>>
>>> MySql has some caching features, put those are far short time, if you
>>> have a
>>> busy site, mysql may not have a heavy query cashed anymore a hour
>>> later, while
>>> using memcached it wouldn't be a problem.
>>>
>> Then allocate more memory to mysql.
>
> You still have the problem that you won't know if your query is stored in the
> query cache or not.
>
I dont care, as long as it is fast.
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 13) Posted: Sun Jan 31, 2010 8:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> J.O. Aho wrote:
>> The Natural Philosopher wrote:
>>> J.O. Aho wrote:
>>>> The Natural Philosopher wrote:
>>>>> J.O. Aho wrote:
>>>>>> The Natural Philosopher wrote:
>>>>>>> Its probably worth using php to do work the database does badly,
>>>>>>> but I'd
>>>>>>> never use it to do what the database does well..
>>>>>> When doing the same query many times, it's worth to think about using
>>>>>> memcached to store the result, just see to remove the cached data
>>>>>> when
>>>>>> you
>>>>>> make an update.
>>>>>>
>>>>> SQL does that anyway, all by itself.Makes the second identical 10
>>>>> second
>>>>> query happen in a flash!
>>>>>
>>>>> Until you change a record...Sad
>>>>>
>>>> MySql has some caching features, put those are far short time, if you
>>>> have a
>>>> busy site, mysql may not have a heavy query cashed anymore a hour
>>>> later, while
>>>> using memcached it wouldn't be a problem.
>>>>
>>> Then allocate more memory to mysql.
>>
>> You still have the problem that you won't know if your query is stored
>> in the
>> query cache or not.
>>
> I dont care, as long as it is fast.

It won't be fast if the wrong data is cached, you will need to actually do the
query over and over again.

--

//Aho
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
The Natural Philosopher

External


Since: Oct 01, 2009
Posts: 27



(Msg. 14) Posted: Sun Jan 31, 2010 8:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

J.O. Aho wrote:
> The Natural Philosopher wrote:
>> J.O. Aho wrote:
>>> The Natural Philosopher wrote:
>>>> J.O. Aho wrote:
>>>>> The Natural Philosopher wrote:
>>>>>> J.O. Aho wrote:
>>>>>>> The Natural Philosopher wrote:
>>>>>>>> Its probably worth using php to do work the database does badly,
>>>>>>>> but I'd
>>>>>>>> never use it to do what the database does well..
>>>>>>> When doing the same query many times, it's worth to think about using
>>>>>>> memcached to store the result, just see to remove the cached data
>>>>>>> when
>>>>>>> you
>>>>>>> make an update.
>>>>>>>
>>>>>> SQL does that anyway, all by itself.Makes the second identical 10
>>>>>> second
>>>>>> query happen in a flash!
>>>>>>
>>>>>> Until you change a record...Sad
>>>>>>
>>>>> MySql has some caching features, put those are far short time, if you
>>>>> have a
>>>>> busy site, mysql may not have a heavy query cashed anymore a hour
>>>>> later, while
>>>>> using memcached it wouldn't be a problem.
>>>>>
>>>> Then allocate more memory to mysql.
>>> You still have the problem that you won't know if your query is stored
>>> in the
>>> query cache or not.
>>>
>> I dont care, as long as it is fast.
>
> It won't be fast if the wrong data is cached, you will need to actually do the
> query over and over again.
>
I suspect mysql's ability to optimise is slightly better than anything I
could come up with in php in under several weeks.
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 15) Posted: Sun Jan 31, 2010 9:25 am
Post subject: Re: Which is faster - MySQL query, or PHP loop [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The Natural Philosopher wrote:
> J.O. Aho wrote:
>> The Natural Philosopher wrote:
>>> J.O. Aho wrote:
>>>> The Natural Philosopher wrote:
>>>>> J.O. Aho wrote:
>>>>>> The Natural Philosopher wrote:
>>>>>>> J.O. Aho wrote:
>>>>>>>> The Natural Philosopher wrote:
>>>>>>>>> Its probably worth using php to do work the database does badly,
>>>>>>>>> but I'd
>>>>>>>>> never use it to do what the database does well..
>>>>>>>> When doing the same query many times, it's worth to think about
>>>>>>>> using
>>>>>>>> memcached to store the result, just see to remove the cached data
>>>>>>>> when
>>>>>>>> you
>>>>>>>> make an update.
>>>>>>>>
>>>>>>> SQL does that anyway, all by itself.Makes the second identical 10
>>>>>>> second
>>>>>>> query happen in a flash!
>>>>>>>
>>>>>>> Until you change a record...Sad
>>>>>>>
>>>>>> MySql has some caching features, put those are far short time, if you
>>>>>> have a
>>>>>> busy site, mysql may not have a heavy query cashed anymore a hour
>>>>>> later, while
>>>>>> using memcached it wouldn't be a problem.
>>>>>>
>>>>> Then allocate more memory to mysql.
>>>> You still have the problem that you won't know if your query is stored
>>>> in the
>>>> query cache or not.
>>>>
>>> I dont care, as long as it is fast.
>>
>> It won't be fast if the wrong data is cached, you will need to
>> actually do the
>> query over and over again.
>>
> I suspect mysql's ability to optimise is slightly better than anything I
> could come up with in php in under several weeks.

It's not good enough, thats why they together with others came with memcached
which you have support for in PHP.


--

//Aho
 >> Stay informed about: Which is faster - MySQL query, or PHP loop 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select query on latin1 or utf8 column: which is faster? - Assume you have two varchar (or Text) columns named L and U which are identical except that the charset for L is latin1 and the charset for U is utf8. All the records in L and U are identical in terms of content, consisting of only 7 bit ASCII characters...

Query MySQL from Excel - Is it possible to pick up information from a MySQL database using a macro in Excel? Don

Tuning Mysql 4.1.10 - I have a freebsd box with 1 gig of memory for mysql/php, I was wondering what my my.cnf should look like, I am doing alot of searches and writes on ISAM tables, I currently have the following: [client] port=3306 socket=/tmp/mysql.sock [mysqld]..

Mysql InnoDB PHP - i have a strange problem : MySQL 4.0.21 innodb and php 4.3.9-1 all tables are in innodb type. For a simple select statement, sometimes i get result and sometimes nothing (i.e mysql_num_rows()>0 or mysql_num_rows()==0). It's a random behaviour. If my....

Installation and config of MYSQL - Hi, Need help. Installed Mysql in Redhat 9.0 workstation. I have installed MYSQL,Appache,PHP. PHP,Appache installation is fine and intergration is also working fine. I have installed and configured MYSQL Problem : I cannot log into MYSQL database either...
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]