Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP-MySql Script: High CPU Usage
Old 04-05-2011, 06:45 AM PHP-MySql Script: High CPU Usage
Novice Talker

Posts: 11
Trades: 0
Hi,

I have a following script.
PHP Code:
for($i=8$i<=23$i++)
{
    
$q1 "select count(*), sum(billsec) from cdr where lastapp = 'Dial' AND calldate>'2011-04-04 ".$i.":00:00' AND calldate<'2011-04-04 ".$i.":29:59' AND disposition = 'ANSWERED' AND (dst='1001' OR dst='1002' OR dst='1003' OR dst='1004' OR dst='1005' OR dst='1006' OR dst='1007' OR dst='1008' OR dst='1009' OR dst='1010')";
    
$r1 mysql_query($q1);
    
$v1 mysql_fetch_row($r1);
    print(
'<td>');
    if(
$v1[0] > 0)
    {
        print(
$v1[0] . "<br>Calls<br><br>" round($v1[1]/60) . '<br>mins');
        print(
'</td>');
    }

When I run this script, the CPU usage peaks to 100% for a long time.
It takes almost 5-7 minutes for the results to be shown in the browser (Quad Xeon Server with 2GB RAM).
Individually, the sql queries takes just 0.27 seconds.

Code:
mysql> select count(*), sum(billsec) from cdr where lastapp = 'Dial' AND calldate>'2011-04-04 8:00:00' AND calldate<'2011-04-04 8:29:59' AND disposition = 'ANSWERED' AND (dst='1001' OR dst='1002' OR dst='1003' OR dst='1004' OR dst='1005' OR dst='1006' OR dst='1007' OR dst='1008' OR dst='1009' OR dst='1010');
+----------+--------------+
| count(*) | sum(billsec) |
+----------+--------------+
|        8 |         1592 |
+----------+--------------+
1 row in set (0.27 sec)
Can someone pls help me understand why this script takes 100% CPU resources and takes 5-7 minutes to show the results AND how can I resolve this.

Your inputs are appreciated.

Thx
Sans
rsgs is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-05-2011, 09:25 AM Re: PHP-MySql Script: High CPU Usage
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You are making 24 seperate calls to the SQL server with ostensibly the same query, surely using a UNION would be massively more efficent.

Realistically you should be using a stored procedure rather a PHP loop and allowing the SQL Server to do what it is best at.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-05-2011, 09:50 AM Re: PHP-MySql Script: High CPU Usage
Novice Talker

Posts: 11
Trades: 0
Thx Chris!
I am new and learning, so your suggestions are of help.

After a quick reading I tried the UNION query as below.
Code:
mysql> (select count(*) as mins, sum(billsec) as secs from cdr where lastapp = 'Dial' AND calldate>'2011-04-04 8:00:00' AND calldate<'2011-04-04 8:30:00' AND disposition = 'ANSWERED' AND (dst='1001' OR dst='1002' OR dst='1003' OR dst='1004' OR dst='1005' OR dst='1006' OR dst='1007' OR dst='1008' OR dst='1009' OR dst='1010')) UNION (select count(*) as mins, sum(billsec) as secs from cdr where lastapp = 'Dial' AND calldate>'2011-04-04 8:30:00' AND calldate<'2011-04-04 9:00:00' AND disposition = 'ANSWERED' AND (dst='1001' OR dst='1002' OR dst='1003' OR dst='1004' OR dst='1005' OR dst='1006' OR dst='1007' OR dst='1008' OR dst='1009' OR dst='1010'));
+------+------+
| mins | secs |
+------+------+
|    8 | 1592 |
|   16 | 2508 |
+------+------+
2 rows in set (0.57 sec)
Pls correct me if I am wrong.

I did a bit of a reading on Stored Procedure and it sort-of bounced over my head. Will need to sit with it and experiment.

Thx again.
rsgs is offline
Reply With Quote
View Public Profile
 
Old 04-05-2011, 08:35 PM Re: PHP-MySql Script: High CPU Usage
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
I'm more used to Oracle than MySQL but I'd say :

a) use a BETWEEN instead of all those ORs

b) select on date format giving hours - why do multiple queries when you can do one?

c) read up on GROUP BY
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 04-06-2011, 02:26 AM Re: PHP-MySql Script: High CPU Usage
Novice Talker

Posts: 11
Trades: 0
Quote:
Originally Posted by PaulW View Post
b) select on date format giving hours - why do multiple queries when you can do one?
Can you shed some light on this one pls.

Meanwhile, I am looking into the GROUP BY option

Thx
rsgs is offline
Reply With Quote
View Public Profile
 
Old 04-06-2011, 09:36 AM Re: PHP-MySql Script: High CPU Usage
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
When you select from a DATE field you can apply a format to the select - usually used to get the full date and time in a particular order but you can use it to, in this case, select hour only (you'll need a group by for this as well). Mixing Oracle and MySQL ... your query would look a bit like (I'm assuming a primary key called id)

SELECT count(id), to_char(colldate, 'HH24'), sum(billsec) from cdr
WHERE lastapp = 'Dial'
AND calldate > '2011-04-04 08:00:00'
AND calldate < '2011-04-04 23:29:59'
AND disposition = 'ANSWERED'
AND dst BETWEEN '1001' AND '1010'
GROUP BY to_char(colldate, 'HH24'), sum(billsec)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE

Last edited by PaulW; 04-06-2011 at 09:37 AM..
PaulW is online now
Reply With Quote
View Public Profile
 
Old 04-06-2011, 10:20 AM Re: PHP-MySql Script: High CPU Usage
Novice Talker

Posts: 11
Trades: 0
Thx PaulW.
But I may not have fully followed your query... Learning MySQL

I am not able to figure out how to eliminating the PHP `for` loop.

I need to get the count(id), sum(billsec) for every 30 minute slot between 8am & midnight.
Example:
count(id), sum(billsec) for time between 8.00 - 8.30
count(id), sum(billsec) for time between 8.30 - 9.00
count(id), sum(billsec) for time between 9.00 - 9.30
count(id), sum(billsec) for time between 9.30 - 10.00

For the above loop I am depending on php's `for` which I understand is wrong because it results in multiple mysql queries.

Need help with a more efficient method to do this.

Thx
rsgs is offline
Reply With Quote
View Public Profile
 
Old 04-06-2011, 11:07 AM Re: PHP-MySql Script: High CPU Usage
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
But I may not have fully followed your query... Learning MySQL
Good! Let's hope we've caught before you learn "PHP programmer SQL" rather than "DBA SQL".

SQL Servers are immensly more powerful and efficient at "data crunching" than PHP code is, so you need to make or LET MySql do the sorting and ordering as much as possible.

You may think that the query looks very long winded but what the SQL server can do in a couple of seconds might take ten times that in server side scripting.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-06-2011, 12:59 PM Re: PHP-MySql Script: High CPU Usage
Novice Talker

Posts: 11
Trades: 0
Thx Paul, Chris. Its a good neighborhood.

Can you share a mysql snippet which can substitute the `for` loop (as required int he example above) to get me started...

Thx again!
rsgs is offline
Reply With Quote
View Public Profile
 
Old 04-06-2011, 01:28 PM Re: PHP-MySql Script: High CPU Usage
Super Spam Talker

Posts: 880
Name: Paul W
Trades: 0
Apologies, misread the half hourly handling as hourly - whoops.

Half hourly:

group by hour(calldate), floor(minute(calldate)/30)

See http://mysql.bigresource.com/Group-B...-0hChkI2c.html and read some of the pages it links to lower down.

General point: any time you find yourself doing repetitive queries or processing data from a db read, chance are you're not using the power of the db - do a bit of poking around to see if you can save time and effort with a better query,
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE

Last edited by PaulW; 04-06-2011 at 01:52 PM..
PaulW is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP-MySql Script: High CPU Usage
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38417 seconds with 12 queries