<?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>MySQL How 2 &#187; Formulas</title>
	<atom:link href="http://www.mysqlhow2.com/category/formula/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlhow2.com</link>
	<description>MySQL DBA information and helpful tips and tricks to make life easier.</description>
	<lastBuildDate>Wed, 14 Jul 2010 15:54:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=8090</generator>
		<item>
		<title>Having is not a replacement for a where clause!</title>
		<link>http://www.mysqlhow2.com/2010/02/06/having-is-not-a-replacement-for-a-where-clause/</link>
		<comments>http://www.mysqlhow2.com/2010/02/06/having-is-not-a-replacement-for-a-where-clause/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 10:00:34 +0000</pubDate>
		<dc:creator>Lee Thompson</dc:creator>
				<category><![CDATA[Formulas]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[having]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[where]]></category>

		<guid isPermaLink="false">http://www.mysqlhow2.com/?p=229</guid>
		<description><![CDATA[Even a query that is checking against an good index will result in a table scan if it is poorly written.  It seems some people think that HAVING can be a substitute for WHERE.  For example: SELECT word_id FROM forum.wordmatch GROUP BY word_id HAVING COUNT( word_id ) =53805; This seems pretty straightforward; the query is [...]]]></description>
			<content:encoded><![CDATA[<p>Even a query that is checking against an good index will result in a table scan if it is poorly written.  It seems some people think that HAVING can be a substitute for WHERE.  For example:</p>
<pre class="brush: sql;">
SELECT word_id
FROM forum.wordmatch
GROUP BY word_id
HAVING COUNT( word_id ) =53805;
</pre>
<p>This seems pretty straightforward; the query is trying to display how many entries have a word_id (indexed) greater than 53805.  But an explain shows that this is a table scan:</p>
<pre class="brush: plain;">
| table | type | possible_keys | key | key_len | ref | rows | Extra |
| wordmatch | index | NULL | word_id | 3 | NULL | 6374529 | Using index |
</pre>
<p>The problem is there is no WHERE clause, and HAVING just controls output.  So rewriting the query like so improves the performance by a factor of 10:</p>
<pre class="brush: sql;">
SELECT count( word_id )
FROM forum.wordmatch
WHERE word_id =53805;
</pre>
<pre class="brush: plain;">
| table | type | possible_keys | key | key_len | ref | rows | Extra |
| wordmatch | range | word_id | word_id | 3 | NULL | 641340 | Using where; Using index |
</pre>
<p>To quote mysql.com:</p>
<p>&#8220;Do not use HAVING for items that should be in the WHERE clause&#8221;. For example, do not write the following:</p>
<pre class="brush: sql;">
SELECT col_name FROM tbl_name HAVING col_name = 0;
</pre>
<p>Write this instead:</p>
<pre class="brush: sql;">
SELECT col_name FROM tbl_name WHERE col_name = 0;
</pre>
<p>The HAVING clause can refer to aggregate functions, which the WHERE clause cannot:</p>
<pre class="brush: sql;">
SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) ;
</pre>
<pre class="brush: sql;">
SELECT DISTINCT ac2.category, ac0.name
FROM snaggy.a2_category ac1, snaggy.a2_category ac2, snaggy.a2_catalog ac0
WHERE ac1.category = 'GAME'
AND ac2.vendor_code = ac1.vendor_code
AND ac2.item_id = ac1.item_id
AND ac0.category = ac2.category
ORDER BY ac0.name
</pre>
<pre class="brush: plain;">
| table | type | possible_keys | key | key_len | ref | rows | Extra |
| ac0 | ALL | NULL | NULL | NULL | NULL | 145 |
| ac1 | ALL | category | NULL | NULL | NULL | 2886 | Using where; Using temporary; Using filesort
| ac2 | ALL | category | NULL | NULL | NULL |2886 | Using where |
</pre>
<pre class="brush: sql;">
CREATE TABLE `a2_category` (
`vendor_code` varchar(4) default NULL,
`item_id` int(5) default NULL,
`category` varchar(8) default NULL,
FULLTEXT KEY `category` (`category`)
) TYPE=MyISAM
</pre>
<p>Just adding an index helps makes the same query complete in a realistic timeframe:</p>
<pre class="brush: sql;">
ALTER TABLE a2_category ADD INDEX (item_id);
</pre>
<pre class="brush: plain;">
| table | type | possible_keys | key | key_len | ref | rows | Extra |
| ac0 | ALL | NULL | NULL | NULL | NULL | 145 | Using where |
| ac1 | ALL | item_id,category | NULL | NULL | NULL | 2886 | Using where; Using temporary; Using filesort |
| ac2 | ref | item_id,category | item_id | 5 | ac1.item_id | 3 | Using where |
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.mysqlhow2.com/2010/02/06/having-is-not-a-replacement-for-a-where-clause/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL Regular Expressions</title>
		<link>http://www.mysqlhow2.com/2010/02/05/mysql-reg-expressions/</link>
		<comments>http://www.mysqlhow2.com/2010/02/05/mysql-reg-expressions/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 19:15:37 +0000</pubDate>
		<dc:creator>Lee Thompson</dc:creator>
				<category><![CDATA[Formulas]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Regular expressions]]></category>

		<guid isPermaLink="false">http://www.mysqlhow2.com/?p=237</guid>
		<description><![CDATA[I added a page with a reg expression cheat sheet, so I wanted to post how MySQL can use reg expressions in select queries. I use these and they can be powerful. Using reg expressions in selects is about as basic as it comes. The format for reg expression for a SELECT is: SELECT something [...]]]></description>
			<content:encoded><![CDATA[<p>I added a page with a<a title="reg expression" href="http://www.mysqlhow2.com/reg-expression-cheat-sheet/"> reg expression cheat</a> sheet, so I wanted to post how MySQL can use reg expressions in select queries. I use these and they can be powerful. Using reg expressions in selects is about as basic as it comes.</p>
<p>The format for reg expression for a SELECT is:</p>
<pre class="brush: sql;">
SELECT something FROM table WHERE column REGEXP 'regexp'
</pre>
<p>For example, to select all columns from the table events where the values in the column id end with 5309, use:</p>
<pre class="brush: sql;">
SELECT * FROM events WHERE id REGEXP '5309$'
</pre>
<p>A more elaborate example selects all columns of the table reviews where the values in the column description contain the word &#8220;mysql&#8221;:</p>
<pre class="brush: sql;">
SELECT * FROM reviews WHERE description REGEXP '[[:&lt;:]]mysql[[:&gt;:]]'
</pre>
<p>MySQL allows the following regular expression metacharacters:. match any character</p>
<ul>
<li> ? match zero or one</li>
<li> * match zero or more</li>
<li> + match one or more</li>
<li> {n} match n times</li>
<li> {m,n} match m through n times</li>
<li> {n,} match n or more times</li>
<li> ^ beginning of line</li>
<li> $ end of line</li>
<li> [[:&lt;:]] match beginning of words [[:&gt;:]] match ending of words</li>
<li> [:class:] match a character class</li>
<li> i.e., [:alpha:] for letters</li>
<li> [:space:] for whitespace</li>
<li> [:punct:] for punctuation</li>
<li> [:upper:] for upper case letters</li>
<li> [abc] match one of enclosed chars</li>
<li> [^xyz] match any char not enclosed</li>
<li> | separates alternatives</li>
</ul>
<p>MySQL interprets a backslash (\) character as an escape character. To use a backslash in a regular expression, you must escape it with another backslash (\\).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mysqlhow2.com/2010/02/05/mysql-reg-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finding cache hit ratio</title>
		<link>http://www.mysqlhow2.com/2010/01/31/finding-cache-hit-ratio/</link>
		<comments>http://www.mysqlhow2.com/2010/01/31/finding-cache-hit-ratio/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 06:25:11 +0000</pubDate>
		<dc:creator>Lee Thompson</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Formulas]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[cache ratio]]></category>

		<guid isPermaLink="false">http://www.mysqlhow2.com/?p=161</guid>
		<description><![CDATA[What is your cache hit ratio. This is a simple bash script to see your cache hit ratio. Data caches that hold often-referenced application data. Code caches that retain often-executed SQL or procedural code. Metadata caches that keep reference data such as object structure information, security information, and more. #!/bin/bash user=&#60;username&#62; pass=&#60;password&#62; SOCKET=$1 while [ [...]]]></description>
			<content:encoded><![CDATA[<p>What is your cache hit ratio. This is a simple bash script to see your cache hit ratio.</p>
<ol>
<li>Data caches that hold often-referenced application data.</li>
<li>Code caches that retain often-executed SQL or procedural code.</li>
<li>Metadata caches that keep reference data such as object structure information, security information, and more.</li>
</ol>
<pre class="brush: bash;">
#!/bin/bash
user=&lt;username&gt;
pass=&lt;password&gt;
SOCKET=$1

while [ 1 ] ; do
READ_REQ=$( mysql -u$user -p$pass -S ${SOCKET} -NBe &quot;SHOW GLOBAL STATUS LIKE 'Key_read_requests'&quot; | awk '{print $2}' )
READS=$( mysql -u$user -p$pass -S ${SOCKET} -NBe &quot;SHOW GLOBAL STATUS LIKE 'Key_reads'&quot; | awk '{print $2}' )
TOTAL=$( echo &quot;${READ_REQ} + ${READS}&quot; | bc )
HIT_RATIO=$( echo &quot;scale=6; ( ${READS} / ${TOTAL}) * 100&quot; | bc -l )
MISS_RATIO=$( echo &quot;scale=6; ( ${READ_REQ} / ${TOTAL}) * 100&quot; | bc -l )
echo &quot;Total Requests: ${TOTAL} - Key Hits: ${HIT_RATIO} - Key Misses: ${MISS_RATIO}&quot;
sleep 1;
done
</pre>
<p>Example Output:<br />
Total Requests: 12257352 &#8211; Key Hits: 7.696400 &#8211; Key Misses: 92.303500<br />
Total Requests: 12270080 &#8211; Key Hits: 7.691600 &#8211; Key Misses: 92.308300<br />
Total Requests: 12283029 &#8211; Key Hits: 7.686000 &#8211; Key Misses: 92.313900<br />
Total Requests: 12283031 &#8211; Key Hits: 7.686000 &#8211; Key Misses: 92.313900<br />
Total Requests: 12297598 &#8211; Key Hits: 7.680100 &#8211; Key Misses: 92.319800<br />
Total Requests: 12316584 &#8211; Key Hits: 7.671900 &#8211; Key Misses: 92.328000<br />
Total Requests: 12344366 &#8211; Key Hits: 7.658700 &#8211; Key Misses: 92.341200<br />
Total Requests: 12359157 &#8211; Key Hits: 7.652800 &#8211; Key Misses: 92.347100</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mysqlhow2.com/2010/01/31/finding-cache-hit-ratio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory Usage</title>
		<link>http://www.mysqlhow2.com/2009/09/10/memory-usage/</link>
		<comments>http://www.mysqlhow2.com/2009/09/10/memory-usage/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 07:32:49 +0000</pubDate>
		<dc:creator>Lee Thompson</dc:creator>
				<category><![CDATA[Formulas]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://www.mysqlhow2.com/?p=111</guid>
		<description><![CDATA[Memory usage, CPU usage, total memory allocoted, database size on so forth...]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;"><strong>These formula&#8217;s will determine the total memory used.</strong></p>
<p style="text-align: left;"><strong>Used MySQL memory<em>:</em></strong></p>
<p style="text-align: left;"><strong><em></em></strong><em>key_buffer + max_connections * (join_buffer </em></p>
<p style="text-align: left;"><em>+ record_buffer + sort_buffer + thread_stack </em></p>
<p style="text-align: left;"><em>+ tmp_table_size)</em></p>
<p><strong>If innodb is enabled.</strong></p>
<p><strong>max memory utilization:</strong></p>
<p style="text-align: left;"><strong></strong> <em>key_buffer_size + innoDB_buffer_pool_size +</em></p>
<p style="text-align: left;"><em>max_connections * ( read_buffer_size +<br />
</em></p>
<p style="text-align: left;"><em>sort_buffer_size + binlog_cache_size)<br />
</em></p>
<p style="text-align: left;"><em> + max_connections * 2M</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mysqlhow2.com/2009/09/10/memory-usage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->