<?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>Erics Tech Blog &#187; mysql tricks</title>
	<atom:link href="http://eric.lubow.org/tag/mysql-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Speeding Up Your Selects and Sorts</title>
		<link>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/</link>
		<comments>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 13:30:07 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql tricks]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=585</guid>
		<description><![CDATA[When you are using a framework, they typically set your VARCHAR size automatically to 255. This is normally fine since you are letting the framework abstract you away from most of the SQL. But if you interact with your SQL, there is a way to get a decent speed increase on your SELECTs and ORDER [...]]]></description>
			<content:encoded><![CDATA[<p>When you are using a framework, they typically set your VARCHAR size automatically to <strong>255</strong>.  This is normally fine since you are letting the framework abstract you away from most of the SQL.  But if you interact with your SQL, there is a way to get a decent speed increase on your SELECTs and ORDER BYs when you are working with VARCHARs.</p>
<p>The VARCHAR data type is only variable character size for storage, not for sorting and buffering.  In fact, since the MySQL optimizer doesn&#8217;t know how big the data in that column can be, it has to allocate the maximum size possible for that column.  So for sorting and buffering of the <em>name</em> and <em>email</em> columns below would take up <strong>310</strong> bytes per row.<br />
<span id="more-585"></span></p>
<p>To fix that you should alter the size of your columns.  Imagine having the optimizer have to go through an additional 310 bytes for every row.  If there are 500k rows in the table time 310 bytes for each row, that can add up in the amount of memory that the optimizer has to use to perform the sorting/buffering.</p>
<p>Consider the following table of businesses:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">DESC</span> businesses;<br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">FIELD</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">TYPE</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #66cc66;">|</span> Extra &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span><br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #66cc66;">|</span> id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> NO &nbsp; <span style="color: #66cc66;">|</span> PRI <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> url &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> email &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> description &nbsp; &nbsp;<span style="color: #66cc66;">|</span> text &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> created_at &nbsp; &nbsp; <span style="color: #66cc66;">|</span> datetime &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">|</span> updated_at &nbsp; &nbsp; <span style="color: #66cc66;">|</span> datetime &nbsp; &nbsp; <span style="color: #66cc66;">|</span> YES &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> &nbsp; &nbsp;<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------+--------------+------+-----+---------+----------------+</span><br />
<span style="color: #cc66cc;">7</span> <span style="color: #993333; font-weight: bold;">ROWS</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span><br />
<br />
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> businesses;<br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LENGTH</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span><br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">53</span> <span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">36</span> <span style="color: #66cc66;">|</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">40</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------------------+--------------------+------------------+</span><br />
<span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ROW</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.40</span> sec<span style="color: #66cc66;">&#41;</span></div></div>
<p>You can change the name to column sizes to 75 (name), 50 (email), and 100 (url).  But realistically you don&#8217;t want to chance things getting cut off, so it may be better off to settle on each column here being a VARCHAR(100).  Even that would save drastically on the space required to perform a sort and buffer the results.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/' rel='bookmark' title='MySQL Encoded URI Search and Replace'>MySQL Encoded URI Search and Replace</a></li>
<li><a href='http://eric.lubow.org/2010/perl/perl-modules/using-unique-keys-and-key-groups-with-background-jobs-in-gearmanclient/' rel='bookmark' title='Using Unique Keys and Key Groups with Background Jobs in Gearman::Client'>Using Unique Keys and Key Groups with Background Jobs in Gearman::Client</a></li>
<li><a href='http://eric.lubow.org/2009/system-administration/howto-recreate-devnull/' rel='bookmark' title='HOWTO Recreate /dev/null'>HOWTO Recreate /dev/null</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL Encoded URI Search and Replace</title>
		<link>http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/</link>
		<comments>http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 14:00:53 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[mysql tricks]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=73</guid>
		<description><![CDATA[So I can definitely not take credit for this trick. The credit goes to Augusto Bott of the Pythian Group for this. I have a table in my DB that has columns of encoded URIs. The list of URI encoding character translations is available here. To accomplish this, the MySQL REPLACE function was used. mysql&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>So I can definitely not take credit for this trick.  The credit goes to Augusto Bott of the <a href="http://www.pythian.com/">Pythian Group</a> for this.  I have a table in my DB that has columns of encoded URIs.  The list of URI encoding character translations is available <a href="http://www.w3schools.com/TAGS/ref_urlencode.asp">here</a>.</p>
<p>To accomplish this, the MySQL <em>REPLACE</em> function was used.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql&gt; SET @test:='%20%27%7C%26%5E%2B%2D%25';<br />
Query OK, 0 rows affected (0.00 sec)<br />
<br />
mysql&gt; SELECT @test as original,<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;@test,'%20',' '),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%27','\''),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%7C','|'), &nbsp;-- REPLACE() is case sensitive<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%7c','|'), &nbsp;-- so we have <br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%26','&amp;'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%5E','^'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%5e','^'), &nbsp;-- to repeat <br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2D','-'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2d','-'), &nbsp;-- ourselves<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2B','+'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2b','+'), &nbsp;-- sometimes<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%25','%') as replaced;<br />
+--------------------------+----------+<br />
| original &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | replaced |<br />
+--------------------------+----------+<br />
| %20%27%7C%26%5E%2B%2D%25 | &nbsp;'|&amp;^+-% | <br />
+--------------------------+----------+<br />
1 row in set (0.01 sec)</div></div>
<p>To make our lives easier, let&#8217;s create a function from that.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql&gt; CREATE FUNCTION url_replace <br />
&nbsp; &nbsp; -&gt; (url VARCHAR(1024))<br />
&nbsp; &nbsp; -&gt; RETURNS VARCHAR(1025)<br />
&nbsp; &nbsp; -&gt; BEGIN<br />
&nbsp; &nbsp; -&gt; RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;@test,'%20',' '),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%27','\''),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%7C','|'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%7c','|'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%26','&amp;'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%5E','^'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%5e','^'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2D','-'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2d','-'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2B','+'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%2b','+'),<br />
&nbsp; &nbsp; -&gt; &nbsp; &nbsp; &nbsp; &nbsp;'%25','%');<br />
&nbsp; &nbsp; -&gt; END$$<br />
Query OK, 0 rows affected (0.02 sec)</div></div>
<p>And to wrap it up, let&#8217;s use it in an actual query.  This query finds all instances of a <strong>%</strong> and replaces it with its unencoded character equivalent.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql&gt; UPDATE website_page SET uri = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( uri,'%20',' '), '%27','\''), '%7C','|'), '%7c','|'), '%26','&amp;'), '%5E','^'), '%5e','^'), '%2D','-'), '%2d','-'), '%2B','+'), '%2b','+'), '%25','%'), '%2C',',') WHERE uri LIKE '%\%%';</div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2007/lua/mysql-proxy-query-profiling/' rel='bookmark' title='MySQL Proxy Query Profiling'>MySQL Proxy Query Profiling</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/speeding-up-your-selects-and-sorts/' rel='bookmark' title='Speeding Up Your Selects and Sorts'>Speeding Up Your Selects and Sorts</a></li>
<li><a href='http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/' rel='bookmark' title='MySQL Error 1033: Incorrect Information in File'>MySQL Error 1033: Incorrect Information in File</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2008/databases/mysql/mysql-encoded-uri-search-and-replace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

