<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bagesh Singh&#039;s Blog -  A blog about PHP, Ajax, JavaScript, CSS, Web 2.0 and Web Development &#187; mysql</title>
	<atom:link href="http://www.bageshsingh.com/bagesh-blog/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bageshsingh.com/bagesh-blog</link>
	<description>The shortest distance to a software &#38; web solutions</description>
	<lastBuildDate>Tue, 07 Sep 2010 16:42:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How did I reduce CPU overhead problem caused by MySql?</title>
		<link>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql-2/</link>
		<comments>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql-2/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 11:22:51 +0000</pubDate>
		<dc:creator>Bagesh Singh</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.bageshsingh.com/bagesh-blog/?p=213</guid>
		<description><![CDATA[From last day, We were having problem with a project which was shut  down in the middle due to heavy traffic. As a technical manager, I was  the person who to take charge over the project bring it down to the  track. After doing few benchmark test, I came to know that [...]]]></description>
			<content:encoded><![CDATA[<p>From last day, We were having problem with a project which was shut  down in the middle due to heavy traffic. As a technical manager, I was  the person who to take charge over the project bring it down to the  track. After doing few benchmark test, I came to know that the MySql  access from a PHP file was creating the overhead to the CPU of the  server.</p>
<h4>Problem of CPU Overhead with MySql</h4>
<p>Let me explain the problem clearly first,</p>
<p>We’re working on a website which was  similar to <a title="www.php2php.com" href="http://www.php2php.com">www.php2php.com</a> and as you can see clearly  in that website the most recent data should be fetched form MySql  database in each second. in the PHP page, which was being called from  Ajax every second, had around 5 SQL queries to retrieve data from  server.</p>
<p>First of all, I optimized that page reducing the 5 queries into  single query by using left outer joins among 3 tables. And then, I did  benchmark test by using Apache  benchmark tool(ab) 50 request with concurrency of 50 with the  following command.</p>
<blockquote><p>ab -n 50 -c 50  http://xyz.com/ajax_page.php</p></blockquote>
<p>And then in another SSH shell prompt, I run the <a href="http://linux.about.com/od/commands/l/blcmdl1_top.htm">top</a> command to view the CPU usages parallelly.</p>
<p>I was still horrified that the CPU usages by mysql after 50  concurrent user was going out of control(100%) despite of optimized  query. But many joins have been used in that single query and lots of  data were there in those tables so the database overhead was high even  though it was a single query.</p>
<h4>How did I reduced the CPU overload drastically caused by MySql?</h4>
<p>Now, the first challenge was to reduce the database access. It was  clear that it was caused by concurrent database overhead in the PHP page  which was being called in every second. Here is the simple steps what I  did to reduce the database overhead.</p>
<ol>
<li>I created another PHP file in which I’ve transferred the mysql query  causing CPU overhead and called it from Cronjob.</li>
<li>Created a temporary table for storing the output given by  PHP page  called from CronJob.</li>
<li>Then, I scheduled the CronJob in every second, don’t tell me that  CronJob can’t be run less than a minute, take a look at this post before saying this.And from  each call, the output data was stored in the temporary table.</li>
<li>And, finally from the Ajax the a new PHP page was called which was  only accessing the data from temporary table with single row.</li>
</ol>
<p>I did the same benchmarking again to the newly made page(which is  accessing data only from temporary table) and saw that CPU usages after  the this process reduced drastically.</p>
<p>I know I can further reduce the CPU overhead by storing the temporary  data in a text file. You guyz know that I’m lazy so that part is left  for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How did I reduce CPU overhead problem caused by MySql?</title>
		<link>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql/</link>
		<comments>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 16:02:41 +0000</pubDate>
		<dc:creator>Bagesh Singh</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.bageshsingh.com/bagesh-blog/?p=88</guid>
		<description><![CDATA[From last day, We were having problem with a project which was shut down in the middle due to heavy traffic. As a technical manager, I was the person who to take charge over the project bring it down to the track. After doing few benchmark test, I came to know that the MySql access [...]]]></description>
			<content:encoded><![CDATA[<p>From last day, We were having problem with a project which was shut down in the middle due to heavy traffic. As a technical manager, I was the person who to take charge over the project bring it down to the track. After doing few benchmark test, I came to know that the MySql access from a PHP file was creating the overhead to the CPU of the server.</p>
<h4>Problem of CPU Overhead with MySql</h4>
<p>Let me explain the problem clearly first,</p>
<p>We’re working on a website which was similar to http://www.swoopo.co.uk/ and as you can see clearly in that website the most recent data should be fetched form MySql database in each second. in the PHP page, which was being called from Ajax every second, had around 5 SQL queries to retrieve data from server.</p>
<p>First of all, I optimized that page reducing the 5 queries into single query by using left outer joins among 3 tables. And then, I did benchmark test by using Apache benchmark tool(ab) 50 request with concurrency of 50 with the following command.</p>
<blockquote><p>ab -n 50 -c 50 http://xyz.com/ajax_page.php</p></blockquote>
<p>And then in another SSH shell prompt, I run the top command to view the CPU usages parallelly.</p>
<p>I was still horrified that the CPU usages by mysql after 50 concurrent user was going out of control(100%) despite of optimized query. But many joins have been used in that single query and lots of data were there in those tables so the database overhead was high even though it was a single query.</p>
<h4>How did I reduced the CPU overload drastically caused by MySql?</h4>
<p>Now, the first challenge was to reduce the database access. It was clear that it was caused by concurrent database overhead in the PHP page which was being called in every second. Here is the simple steps what I did to reduce the database overhead.</p>
<ol>
<li>I created another PHP file in which I’ve transferred the mysql query causing CPU overhead and called it from Cronjob.</li>
<li>Created a temporary table for storing the output given by  PHP page called from CronJob.</li>
<li>Then, I scheduled the CronJob in every second, don’t tell me that CronJob can’t be run less than a minute, take a look at this post before saying this.And from each call, the output data was stored in the temporary table.</li>
<li>And, finally from the Ajax the a new PHP page was called which was only accessing the data from temporary table with single row.</li>
</ol>
<p>I did the same benchmarking again to the newly made page(which is accessing data only from temporary table) and saw that CPU usages after the this process reduced drastically.</p>
<p>I know I can further reduce the CPU overhead by storing the temporary data in a text file. You guyz know that I’m lazy so that part is left for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bageshsingh.com/bagesh-blog/2010/03/how-did-i-reduce-cpu-overhead-problem-caused-by-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
