Link to home
Start Free TrialLog in
Avatar of Alw1n
Alw1n

asked on

PHP loop and high cpu usage

Hi,
I have a loop in a php script (5.2.11 on IIS) that builds data from a database, there could be many lines (20k+) and it can run for several seconds. I have noticed that the cpu usage goes up to 100% while this is busy which is understandable, I was wondering:
1 - Is this cpu usage is going to affect the server badly (for other requests) or will it give resources to other requests as needed?
2 - What I could do about this to free up some cpu time (ala vb6 DoEvents type thing)
ASKER CERTIFIED SOLUTION
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alw1n
Alw1n

ASKER

thanks for the info, it is kinda what I thought. Could you explain a little more about "give a break of 2-5 sec break. and later run the code"? Do you mean a 'sleep' in the loop?
Memory is fine as I have set this correctly, it's just the cpu that is taking a beating.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Beverley Portlock
Heavy CPU usage during database retrival normally means that you have not defined sufficient indexes for your retrieval. Ensure that EVERY column mentioned in a WHERE, ORDER BY or JOIN clause has an index in the database.
Avatar of Alw1n

ASKER

Thanks, I haven't really noticed that a lack of indexes causes high cpu usage, just that the queries take longer but I had created the indexes already anyway for performance reasons
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alw1n

ASKER

Thanks All, I split the points to be fair