Re: Speeding up MySQL query
- From: Erwin Moller <since_humans_read_this_I_am_spammed_too_much@xxxxxxxxxxxxxxxx>
- Date: Thu, 08 Sep 2005 13:11:28 +0200
EmmettPower@xxxxxxxxx wrote:
> Hi,
>
> I am reusing the results of a MySQL query on a PHP page. The code works
> fine but it is taking about 4 to 5 seconds to load the page so I am
> wondering if there is a way to make the code more efficient and the
> page load more quickly.
>
> At the top of the page I am running the query with the following code.
> $id loads through a session variable:
>
> <?
>
> ini_set('max_execution_time', 86400);
> $link = mysql_connect($host, $uid, $pwd);
> mysql_select_db('Database');
> $query = "SELECT DISTINCT `Table`.`Field` FROM `Table` WHERE
> `Table`.`ID` = $id ORDER BY Table`.`Field`";
> $result = mysql_query($query) or die (mysql_error());
>
> ?>
>
> When I want to access the query data I run the following code.
>
> <?php
>
> $i=0;
> mysql_data_seek($result,0);
> while ($array= mysql_fetch_array($result)) {
> ?>
>
> HTML
> <?php echo $array[Field] ?>">
> HTML
>
> <?php
> $i++;
> }
> ?>
>
> This code is repeated about four times on the page.
>
> I'd appreciate any suggestions as to how to speed this up.
>
> Regards
>
> Emmett
Hi Emmett,
It looks pretty optimized to me...
Allthough DISTINCT can be a bottleneck in some situations.
To find ways to optimize, first find the bottlenecks.
To do this, make a rude benchmark, using systemtime in milliseconds.
Just measure the time needed to:
1) execute the query.
2) Loop over the results.
Maybe the query is extremely fast, and the bottleneck is somewhere else in
your code.
Two other ideas:
1) If you are producing a HUGE page: consider using compression (lookup zlib
on www.php.net)
2) If you are running over the same query 4 times, it might help to remember
the results maybe in a variable. Not sure, it depends.
Again: measure it in milliseconds, then you know something. Now is is
guessing. :-)
Good luck,
Regards,
Erwin Moller
.
- Follow-Ups:
- Re: Speeding up MySQL query
- From: EmmettPower
- Re: Speeding up MySQL query
- References:
- Speeding up MySQL query
- From: EmmettPower
- Speeding up MySQL query
- Prev by Date: Speeding up MySQL query
- Next by Date: Re: Speeding up MySQL query
- Previous by thread: Speeding up MySQL query
- Next by thread: Re: Speeding up MySQL query
- Index(es):
Relevant Pages
|