Slow results

Hi there



First off sorry if this is a long posting but I need to give all the facts
in the hope to get an answer. 2 Years ago I did a site for a client to track
online orders and run reports. One of the main reports is now going so slow
it's of no use to anybody.



I am the first to admit that I am not the best PHP and MySQL coder so I may
be missing something here or I may have just met the limit of MySQL, all
scripts and DBs are on a modem new dedicated server.



The 2 main tables are "orders" and "productslist" The orders table has
7000000 (yes that is 6 million) records and the productslist has 6000, all
search fields have been indexed, the whole DB have 25 tables



The users select the search criteria, summits the form and the below PHP
code it run , it can take upwards of 20 mins to return the results. So my
question is, is there anything wrong with my code or the way I and running
this, or have I just hit the limit of MySQL



Many thanks



Brian





$query = "SELECT p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
SUM(o.`del` ) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
SUM(o.`total_invoice_price`) AS totalinvoiceprice FROM productslist AS p
LEFT OUTER JOIN orders AS o ON ( p.prod_code = o.prod_code AND o.timestamp
>= $sdate AND o.timestamp <= $edate AND o.status <> "p" AND (status NOT IN
("u")) AND (status NOT IN ("n")) ) GROUP BY p.prod_code ORDER BY
p.prod_code";



$result = mysql_query($query,$db) or die ("Can't run getstore SQL ERROR: "
.. mysql_error () . "");

if ($myrow = mysql_fetch_array($result)) {

print "<h3 align=\"center\">Total number of products ordered and delivered
between $realstartdate and $realenddate for $brand</h3>";

print '<table width="" border="1" align="center" cellpadding="2"
cellspacing="2" bordercolor="#00CCFF" bgcolor="#F4F4F4"><tr><td>';

print "<table width=\"100%\" border=\"0\" ><tr><td>Product
Code</td><td>Description</td><td>Ordered</td><td>Delivered</td><td>Cost
Price</td><td>Invoice Price</td></tr>";



if ($myrow[totalor] == null) {$totalor = 0;} else {$totalor =
$myrow[totalor];}

$sumord = $totalor;

if ($myrow[totaldel] == null) {$totaldel = 0;} else {$totaldel =
$myrow[totaldel];}

$sumdel = $totaldel;

if ($myrow[totalcostprice] == null) {$cost = 0;} else {$cost =
$myrow[totalcostprice];}

if ($myrow[totalinvoiceprice] == null) {$invoice = 0;} else {$invoice =
$myrow[totalinvoiceprice];}

$sumcost = $cost;

$suminvoice = $invoice;

$cost = $cost / 100;

$invoice = $invoice / 100;

print
"<tr><td>$myrow[prod_code]</td><td>$myrow[description]</td><td>$totalor</td><td>$totaldel</td><td>£$cost</td><td>£$invoice</td></tr>";

while ($myrow = mysql_fetch_array($result)) {

if ($myrow[totalor] == null) {$totalor = 0;} else {$totalor =
$myrow[totalor];}

$sumord = $sumord + $totalor;

if ($myrow[totaldel] == null) {$totaldel = 0;} else {$totaldel =
$myrow[totaldel];}

$sumdel = $sumdel + $totaldel;

if ($myrow[totalcostprice] == null) {$cost = 0;} else {$cost =
$myrow[totalcostprice];}

if ($myrow[totalinvoiceprice] == null) {$invoice = 0;} else {$invoice =
$myrow[totalinvoiceprice];}

$sumcost = $sumcost + $cost;

$suminvoice = $suminvoice + $invoice;

$cost = $cost / 100;

$invoice = $invoice / 100;

print
"<tr><td>$myrow[prod_code]</td><td>$myrow[description]</td><td>$totalor</td><td>$totaldel</td><td>£$cost</td><td>£$invoice</td></tr>";

}

print "<tr><td colspan=5><hr></td></tr>";

$sumcost = $sumcost / 100;

$suminvoice = $suminvoice / 100;

print
"<tr><td> </td><td> </td><td>$sumord</td><td>$sumdel</td><td>£$sumcost</td><td>£$suminvoice</td><tr>";

print "</table></table>";

} else {

print "<h3 align=\"center\">Sorry there are no matches found on your
search, please try again</h3>";

}
Brian [ Fr, 05 Oktober 2007 00:21 ] [ ID #1836365 ]

Re: Slow results

Brian wrote:

> The 2 main tables are "orders" and "productslist" The orders table has
> 7000000 (yes that is 6 million) records and the productslist has 6000, all
> search fields have been indexed, the whole DB have 25 tables
>
> The users select the search criteria, summits the form and the below PHP
> code it run , it can take upwards of 20 mins to return the results. So my
> question is, is there anything wrong with my code or the way I and running
> this, or have I just hit the limit of MySQL

You could skip the join, this means you won't get the product description, you
could query this separately (store each fetched in an array, that way you can
limit refetching a description you already fetched.)

--

//Aho
Shion [ Fr, 05 Oktober 2007 06:32 ] [ ID #1837528 ]

Re: Slow results

Brian wrote:

> The 2 main tables are "orders" and "productslist" The orders table has
> 7000000 (yes that is 6 million) records and the productslist has 6000, all
> search fields have been indexed, the whole DB have 25 tables

Does "search fields" include the "prod_code" field? try indexing this
in both tables. Anyways, what do you mean with "search fields"? According
to the query, you should try indexes on p.prod_code, o.prod_code,
o.timestamp and o.status.


> The users select the search criteria, summits the form and the below PHP
> code it run , it can take upwards of 20 mins to return the results. So my
> question is, is there anything wrong with my code or the way I and running
> this, or have I just hit the limit of MySQL

I do not know MySQL, but I can hardly believe you hit a limit of MySQL with
this. After all, it claims itself to be a fast DBMS.


> $query = "SELECT p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
> SUM(o.`del` ) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
> SUM(o.`total_invoice_price`) AS totalinvoiceprice FROM productslist AS p
> LEFT OUTER JOIN orders AS o ON ( p.prod_code = o.prod_code AND o.timestamp
> >= $sdate AND o.timestamp <= $edate AND o.status <> "p" AND (status NOT
> >IN
> ("u")) AND (status NOT IN ("n")) ) GROUP BY p.prod_code ORDER BY
> p.prod_code";

$query = "
SELECT
p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
SUM(o.`del`) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
SUM(o.`total_invoice_price`) AS totalinvoiceprice
FROM
productslist AS p LEFT OUTER JOIN
orders AS o
ON (p.prod_code = o.prod_code AND
o.timestamp >= $sdate AND
o.timestamp <= $edate AND
o.status not in ("p", "u", "n"))
GROUP BY
p.prod_code
ORDER BY
p.prod_code
";

I transformated the query to a bit more readable form. Consider doing
this next time (also with your php code) if you want answers to your
questions.
Boris Stumm [ Fr, 05 Oktober 2007 10:18 ] [ ID #1837529 ]

Re: Slow results

On 5 Oct, 09:18, Boris Stumm <st... [at] informatik.uni-kl.de> wrote:
> Brian wrote:
> > The 2 main tables are "orders" and "productslist" The orders table has
> > 7000000 (yes that is 6 million) records and the productslist has 6000, all
> > search fields have been indexed, the whole DB have 25 tables
>
> Does "search fields" include the "prod_code" field? try indexing this
> in both tables. Anyways, what do you mean with "search fields"? According
> to the query, you should try indexes on p.prod_code, o.prod_code,
> o.timestamp and o.status.
>
> > The users select the search criteria, summits the form and the below PHP
> > code it run , it can take upwards of 20 mins to return the results. So my
> > question is, is there anything wrong with my code or the way I and running
> > this, or have I just hit the limit of MySQL
>
> I do not know MySQL, but I can hardly believe you hit a limit of MySQL with
> this. After all, it claims itself to be a fast DBMS.
>
> > $query = "SELECT p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
> > SUM(o.`del` ) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
> > SUM(o.`total_invoice_price`) AS totalinvoiceprice FROM productslist AS p
> > LEFT OUTER JOIN orders AS o ON ( p.prod_code = o.prod_code AND o.timestamp
> > >= $sdate AND o.timestamp <= $edate AND o.status <> "p" AND (status NOT
> > >IN
> > ("u")) AND (status NOT IN ("n")) ) GROUP BY p.prod_code ORDER BY
> > p.prod_code";
>
> $query = "
> SELECT
> p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
> SUM(o.`del`) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
> SUM(o.`total_invoice_price`) AS totalinvoiceprice
> FROM
> productslist AS p LEFT OUTER JOIN
> orders AS o
> ON (p.prod_code = o.prod_code AND
> o.timestamp >= $sdate AND
> o.timestamp <= $edate AND
> o.status not in ("p", "u", "n"))
> GROUP BY
> p.prod_code
> ORDER BY
> p.prod_code
> ";
>
> I transformated the query to a bit more readable form. Consider doing
> this next time (also with your php code) if you want answers to your
> questions.

It always amazes me when I see queries written like the OP's posted
here. I often take one look and give up.
Captain Paralytic [ Fr, 05 Oktober 2007 10:51 ] [ ID #1837530 ]

Re: Slow results

"Captain Paralytic" <paul_lautman [at] yahoo.com> wrote in message
news:1191574284.139144.113410 [at] r29g2000hsg.googlegroups.com.. .
> On 5 Oct, 09:18, Boris Stumm <st... [at] informatik.uni-kl.de> wrote:
>> Brian wrote:
>> > The 2 main tables are "orders" and "productslist" The orders table has
>> > 7000000 (yes that is 6 million) records and the productslist has 6000,
>> > all
>> > search fields have been indexed, the whole DB have 25 tables
>>
>> Does "search fields" include the "prod_code" field? try indexing this
>> in both tables. Anyways, what do you mean with "search fields"? According
>> to the query, you should try indexes on p.prod_code, o.prod_code,
>> o.timestamp and o.status.
>>
>> > The users select the search criteria, summits the form and the below
>> > PHP
>> > code it run , it can take upwards of 20 mins to return the results. So
>> > my
>> > question is, is there anything wrong with my code or the way I and
>> > running
>> > this, or have I just hit the limit of MySQL
>>
>> I do not know MySQL, but I can hardly believe you hit a limit of MySQL
>> with
>> this. After all, it claims itself to be a fast DBMS.
>>
>> > $query = "SELECT p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
>> > SUM(o.`del` ) AS totaldel, SUM(o.`total_cost_price`) AS totalcostprice,
>> > SUM(o.`total_invoice_price`) AS totalinvoiceprice FROM productslist AS
>> > p
>> > LEFT OUTER JOIN orders AS o ON ( p.prod_code = o.prod_code AND
>> > o.timestamp
>> > >= $sdate AND o.timestamp <= $edate AND o.status <> "p" AND (status
>> > NOT
>> > >IN
>> > ("u")) AND (status NOT IN ("n")) ) GROUP BY p.prod_code ORDER BY
>> > p.prod_code";
>>
>> $query = "
>> SELECT
>> p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
>> SUM(o.`del`) AS totaldel, SUM(o.`total_cost_price`) AS
>> totalcostprice,
>> SUM(o.`total_invoice_price`) AS totalinvoiceprice
>> FROM
>> productslist AS p LEFT OUTER JOIN
>> orders AS o
>> ON (p.prod_code = o.prod_code AND
>> o.timestamp >= $sdate AND
>> o.timestamp <= $edate AND
>> o.status not in ("p", "u", "n"))
>> GROUP BY
>> p.prod_code
>> ORDER BY
>> p.prod_code
>> ";
>>
>> I transformated the query to a bit more readable form. Consider doing
>> this next time (also with your php code) if you want answers to your
>> questions.
>
> It always amazes me when I see queries written like the OP's posted
> here. I often take one look and give up.


Thanks for your comments guys, sorry the query was so hard to read, will
try to remember that if i need to ever post again.

Thanks

Brian
Brian [ So, 07 Oktober 2007 01:34 ] [ ID #1838631 ]

Re: Slow results

You better!!!!! lol
"Brian" <brianNOSPAM [at] nrwp.co.uk> wrote in message
news:hYUNi.24667$aN2.22792 [at] newsfe2-gui.ntli.net...
> "Captain Paralytic" <paul_lautman [at] yahoo.com> wrote in message
> news:1191574284.139144.113410 [at] r29g2000hsg.googlegroups.com.. .
>> On 5 Oct, 09:18, Boris Stumm <st... [at] informatik.uni-kl.de> wrote:
>>> Brian wrote:
>>> > The 2 main tables are "orders" and "productslist" The orders table has
>>> > 7000000 (yes that is 6 million) records and the productslist has 6000,
>>> > all
>>> > search fields have been indexed, the whole DB have 25 tables
>>>
>>> Does "search fields" include the "prod_code" field? try indexing this
>>> in both tables. Anyways, what do you mean with "search fields"?
>>> According
>>> to the query, you should try indexes on p.prod_code, o.prod_code,
>>> o.timestamp and o.status.
>>>
>>> > The users select the search criteria, summits the form and the below
>>> > PHP
>>> > code it run , it can take upwards of 20 mins to return the results. So
>>> > my
>>> > question is, is there anything wrong with my code or the way I and
>>> > running
>>> > this, or have I just hit the limit of MySQL
>>>
>>> I do not know MySQL, but I can hardly believe you hit a limit of MySQL
>>> with
>>> this. After all, it claims itself to be a fast DBMS.
>>>
>>> > $query = "SELECT p.prod_code, p.description, SUM(o.`ord` ) AS
>>> > totalor,
>>> > SUM(o.`del` ) AS totaldel, SUM(o.`total_cost_price`) AS
>>> > totalcostprice,
>>> > SUM(o.`total_invoice_price`) AS totalinvoiceprice FROM productslist AS
>>> > p
>>> > LEFT OUTER JOIN orders AS o ON ( p.prod_code = o.prod_code AND
>>> > o.timestamp
>>> > >= $sdate AND o.timestamp <= $edate AND o.status <> "p" AND (status
>>> > NOT
>>> > >IN
>>> > ("u")) AND (status NOT IN ("n")) ) GROUP BY p.prod_code ORDER BY
>>> > p.prod_code";
>>>
>>> $query = "
>>> SELECT
>>> p.prod_code, p.description, SUM(o.`ord` ) AS totalor,
>>> SUM(o.`del`) AS totaldel, SUM(o.`total_cost_price`) AS
>>> totalcostprice,
>>> SUM(o.`total_invoice_price`) AS totalinvoiceprice
>>> FROM
>>> productslist AS p LEFT OUTER JOIN
>>> orders AS o
>>> ON (p.prod_code = o.prod_code AND
>>> o.timestamp >= $sdate AND
>>> o.timestamp <= $edate AND
>>> o.status not in ("p", "u", "n"))
>>> GROUP BY
>>> p.prod_code
>>> ORDER BY
>>> p.prod_code
>>> ";
>>>
>>> I transformated the query to a bit more readable form. Consider doing
>>> this next time (also with your php code) if you want answers to your
>>> questions.
>>
>> It always amazes me when I see queries written like the OP's posted
>> here. I often take one look and give up.
>
>
> Thanks for your comments guys, sorry the query was so hard to read, will
> try to remember that if i need to ever post again.
>
> Thanks
>
> Brian
>
>
SquidHead [ So, 07 Oktober 2007 17:29 ] [ ID #1838632 ]

Re: Slow results

SquidHead wrote:
> You better!!!!! lol
Please do not top post.
Paul Lautman [ So, 07 Oktober 2007 18:49 ] [ ID #1838633 ]

Re: Slow results

Post removed (X-No-Archive: yes)
Notifier Deamon [ Mo, 15 Oktober 2007 19:02 ] [ ID #1845456 ]
PHP » alt.php.sql » Slow results

Vorheriges Thema: Re: problems importing tables
Nächstes Thema: What's wrong with this?