<?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</title>
	<atom:link href="http://eric.lubow.org/category/databases/mysql/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>Database Read/Write Splitting in Frameworks/ORMs</title>
		<link>http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/</link>
		<comments>http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:00:27 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[scaling]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=563</guid>
		<description><![CDATA[Although one of the primary ideas behind frameworks is to keep things as simple as possible, sometimes they create issues in the long run. What I am about to discuss is something of a luxury problem (as scaling usually is), but it is a problem nonetheless. When initially starting a project, whether you are using [...]]]></description>
			<content:encoded><![CDATA[<p>Although one of the primary ideas behind frameworks is to keep things as simple as possible, sometimes they create issues in the long run.  What I am about to discuss is something of a luxury problem (as scaling usually is), but it is a problem nonetheless.</p>
<p>When initially starting a project, whether you are using <a href="http://rubyonrails.org/">Ruby on Rails</a> (Ruby), <a href="http://www.djangoproject.com/">Django</a> (Python), <a href="http://cakephp.org/">CakePHP</a> (PHP), <a href="http://www.catalystframework.org/">Catalyst</a> (Perl), or any of the other 100s of frameworks in any of the languages out there, the first and most important thing to do is to get it out the door.  Once you have done that, it&#8217;s time to get users, fix bugs, and add features.  After you have done all that and you have a great web app, its time to think scaling. (Yes I realize that I have trivialized this process immensely, but its for a point, I promise).<br />
<span id="more-563"></span><br />
When starting to scale (whether its out or up) and you decide its time to add another database, its necessary to analyze your app and decide whether its read heavy or write heavy.  A lot of scaling comes in knowing your application and where its bottlenecks are.  Let&#8217;s assume that you are at the point that you need to add a database server.  What would be great is if you had a framework that allowed you to set some database servers as read-only in order to take load off the master.</p>
<p>In an abstract format, it would be a good idea to break out your SQL requirements into 2 functions: <strong>sql_write_query</strong> and <strong>sql_read_query</strong>.  Then have the functions go to your primary database server and slave database servers respectively.  The reason that you should do this instead of using a single function that sends the query to the &#8220;correct&#8221; location based on the SQL it finds is that your slave servers may be behind the master (which is the nature of replication) which could give you an incorrect result in your query.  This way, depending on the importance and type of the query, in your code you can choose the location that you want to send the query to.  The read queries where accuracy is extremely important can be sent to the database using <strong>sql_write_query</strong> and all others can be executed normally using your <strong>sql_read_query</strong> function.</p>
<p>How does this relate to frameworks and ORMs?  It would be very handy if frameworks provided a method to expand an application into splitting read and write queries that is native to the frameworks.  If it is native and isn&#8217;t hacked on afterward (like below), then you don&#8217;t have to muck around in the core code or write a plugin and you can stick to what you know best (which is your application).  That is not to say that one should prematurely optimize an application (which is a whole other issue that you need to be careful of) and build it out in a split read/write fashion from the beginning, but that there should be a native way for the application to be faster should you reach that point.</p>
<p>Building this out as a afterthought can be done like what&#8217;s below (the example is in Python but can be extrapolated to the language specific to the framework).</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> write_mysql_query<span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;dbvip1.example.com&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">user</span> <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;root&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; passwd <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;pass&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;myapp&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SET AUTOCOMMIT=1&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> read_mysql_query<span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; conn <span style="color: #66cc66;">=</span> MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;slavedb1.example.com&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">user</span> <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;root&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; passwd <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;pass&quot;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;myapp&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor <span style="color: #66cc66;">=</span> conn.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>query<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; result <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; cursor.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; conn.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>You can even do things for the writer in a more transactional fashion using <em>START TRANSACTION</em> and <em>COMMIT</em> if you don&#8217;t like using <em>AUTOCOMMIT</em>.  You&#8217;ll also notice that there is a connect every time a query is executed.  A lot of people will have an initial gut reaction of a problem here.  In fact, since most of your queries will be taking place over a LAN with a pretty fast backplane (or some other variation of a high speed network), it&#8217;s probably negligible.  Taking the load off of your master and dispersing it onto slaves will make the most difference here.</p>
<p>All this is a very oversimplified way of taking this step, but it is something that frameworks should consider.  Even if its just in a plugin fashion which can be taken advantage of if the database server is getting overloaded.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/python/when-to-use-mysql-cursor-classes-in-python/' rel='bookmark' title='When To Use MySQL Cursor Classes In Python'>When To Use MySQL Cursor Classes In Python</a></li>
<li><a href='http://eric.lubow.org/2009/python/python-multiprocessing-pools-and-mysql/' rel='bookmark' title='Python Multiprocessing Pools and MySQL'>Python Multiprocessing Pools and MySQL</a></li>
<li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/databases/mysql/database-readwrite-splitting-in-frameworksorms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Error 1033: Incorrect Information in File</title>
		<link>http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/</link>
		<comments>http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 14:15:43 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[errors]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=496</guid>
		<description><![CDATA[If you&#8217;ve ever been plagued by an error 1033 issue in MySQL (replication will show it as well), then I might be able to help you out. The error reads something like, &#8220;Incorrect information in file: &#8216;./mydb/table.frm&#8217;. I classify this as another one of MySQLs cryptic error messages. Here is how I determined that this [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever been plagued by an error 1033 issue in MySQL (replication will show it as well), then I might be able to help you out.  The error reads something like, &#8220;Incorrect information in file: &#8216;./mydb/table.frm&#8217;.  I classify this as another one of MySQLs cryptic error messages.  Here is how I determined that this was my problem.</p>
<p>Googling around got me an answer, but I had to read a bunch of different responses to piece together the answer. Essentially this issue (in my case) was a result of the InnoDB engine not loading up when MySQL was restarted.  Therefore when MySQL tried to read the frm file (table description) which was written for an InnoDB table with the MyISAM reader, it didn&#8217;t like it.  Since MyISAM is the fallback engine, it went to that and the table became unusable.<br />
<span id="more-496"></span></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">Last_Errno: 1033<br />
Last_Error: Error 'Incorrect information in file: './st/table.frm'' on query. Default database: 'mydb'. Query: 'INSERT INTO `table` (`id`,`col1`) VALUES (1,'foobar')'<br />
# or<br />
mysql&gt; REPAIR TABLE table;<br />
+-------------+--------+----------+----------------------------------------------------+<br />
| Table &nbsp; &nbsp; &nbsp; | Op &nbsp; &nbsp; | Msg_type | Msg_text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |<br />
+-------------+--------+----------+----------------------------------------------------+<br />
| mydb.table &nbsp;| repair | Error &nbsp; &nbsp;| Incorrect information in file: './mydb/table.frm' &nbsp;| <br />
| mydb.table &nbsp;| repair | error &nbsp; &nbsp;| Corrupt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| <br />
+-------------+--------+----------+----------------------------------------------------+<br />
2 rows in set (0.02 sec)</div></div>
<p>I already knew my table <strong>table</strong> is an InnoDB table.  To be sure that this was the issue, I simply checked to see which engines were loaded (removed some for brevity).</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; SHOW ENGINES;<br />
+------------+----------+----------------------------------------------------------------+<br />
| Engine &nbsp; &nbsp; | Support &nbsp;| Comment &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
+------------+----------+----------------------------------------------------------------+<br />
| MyISAM &nbsp; &nbsp; | DEFAULT &nbsp;| Default engine as of MySQL 3.23 with great performance &nbsp; &nbsp; &nbsp; &nbsp; | <br />
| MEMORY &nbsp; &nbsp; | YES &nbsp; &nbsp; &nbsp;| Hash based, stored in memory, useful for temporary tables &nbsp; &nbsp; &nbsp;| <br />
| InnoDB &nbsp; &nbsp; | DISABLED | Supports transactions, row-level locking, and foreign keys &nbsp; &nbsp; | <br />
| CSV &nbsp; &nbsp; &nbsp; &nbsp;| YES &nbsp; &nbsp; &nbsp;| CSV storage engine &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | <br />
+------------+----------+----------------------------------------------------------------+</div></div>
<p>So here I notice that InnoDB is disabled.  (Note: I skipped the step where I check my <strong>my.cnf</strong> to make sure the <em>skip-innodb</em> line in the <em>[mysqld]</em> section was commented out.  I already knew it was, but if you are unsure, check.)  So I pop over to the error log and I see this:</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">InnoDB: Unable to lock ./ibdata1, error: 11<br />
InnoDB: Check that you do not already have another mysqld process<br />
InnoDB: using the same InnoDB data or log files.<br />
091222 15:21:55 &nbsp;InnoDB: Unable to open the first data file<br />
InnoDB: Error in opening ./ibdata1<br />
091222 15:21:55 &nbsp;InnoDB: Operating system error number 11 in a file operation.<br />
InnoDB: Error number 11 means 'Resource temporarily unavailable'.<br />
InnoDB: Some operating system error numbers are described at<br />
InnoDB: http://dev.mysql.com/doc/refman/5.0/en/operating-system-error-codes.html<br />
&nbsp;79InnoDB: Could not open or create data files.</div></div>
<p>This says to me that it is likely that the MySQL restart didn&#8217;t go as well as the initscript would have liked me to believe.  So I see what files are open and what&#8217;s running:</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">[root@db mysql]# lsof | grep ibdata1<br />
COMMAND &nbsp; &nbsp; PID &nbsp; &nbsp; &nbsp; &nbsp; USER &nbsp; FD &nbsp; &nbsp; &nbsp;TYPE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEVICE &nbsp; &nbsp; &nbsp; &nbsp;SIZE &nbsp; &nbsp; &nbsp; NODE NAME<br />
mysqld &nbsp; &nbsp;24574 &nbsp; &nbsp; &nbsp; &nbsp;mysql &nbsp; &nbsp;4uW &nbsp; &nbsp; REG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8,3 &nbsp;5018484736 &nbsp; 61538308 /var/lib/mysql/ibdata1<br />
[root@db5 mysql]# ps ax | grep mysqld<br />
24536 pts/0 &nbsp; &nbsp;S &nbsp; &nbsp; &nbsp;0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql/ --pid-file=/var/lib/mysql//db.example.com.pid<br />
24574 pts/0 &nbsp; &nbsp;Sl &nbsp; &nbsp; 7:58 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql/ --user=mysql --pid-file=/var/lib/mysql//db.example.com.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock<br />
26635 pts/0 &nbsp; &nbsp;S &nbsp; &nbsp; &nbsp;0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql/ --pid-file=/var/lib/mysql//db.example.com.pid<br />
26666 pts/0 &nbsp; &nbsp;Sl &nbsp; &nbsp; 0:06 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql/ --user=mysql --pid-file=/var/lib/mysql//db.example.com.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock</div></div>
<p>Well look at that, 2 versions of MySQL running and <em>ibdata1</em> is being held open by one of them.  So now I do the ugly thing and kill the mysqld process that holding the file lock and then restart MySQL:</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">[root@db mysql]# kill -9 24574<br />
[root@db mysql]# ps ax | grep mysqld<br />
24536 pts/0 &nbsp; &nbsp;S &nbsp; &nbsp; &nbsp;0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql/ --pid-file=/var/lib/mysql//db.example.com.pid<br />
27051 pts/0 &nbsp; &nbsp;Rl &nbsp; &nbsp; 0:02 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql/ --user=mysql --pid-file=/var/lib/mysql//db.example.com.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock<br />
27075 pts/0 &nbsp; &nbsp;S+ &nbsp; &nbsp; 0:00 grep mysqld<br />
[root@db mysql]# /etc/init.d/mysql restart<br />
MySQL manager or server PID file could not be found! &nbsp; &nbsp; &nbsp; [FAILED]<br />
Starting MySQL............... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ &nbsp;OK &nbsp;]</div></div>
<p>So back over to MySQL:</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; SHOW ENGINES;<br />
+------------+----------+----------------------------------------------------------------+<br />
| Engine &nbsp; &nbsp; | Support &nbsp;| Comment &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|<br />
+------------+----------+----------------------------------------------------------------+<br />
| MyISAM &nbsp; &nbsp; | DEFAULT &nbsp;| Default engine as of MySQL 3.23 with great performance &nbsp; &nbsp; &nbsp; &nbsp; | <br />
| MEMORY &nbsp; &nbsp; | YES &nbsp; &nbsp; &nbsp;| Hash based, stored in memory, useful for temporary tables &nbsp; &nbsp; &nbsp;| <br />
| InnoDB &nbsp; &nbsp; | YES &nbsp; &nbsp; &nbsp;| Supports transactions, row-level locking, and foreign keys &nbsp; &nbsp; | <br />
| CSV &nbsp; &nbsp; &nbsp; &nbsp;| YES &nbsp; &nbsp; &nbsp;| CSV storage engine &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | <br />
+------------+----------+----------------------------------------------------------------+</div></div>
<p>There it is.  Now you should be able to start up replication again (if that was the issue).  Or if you didn&#8217;t discover this issue with replication, you should just be able to use your DB like normal.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/python/pythons-mysqldb-2014-error-commands-out-of-sync/' rel='bookmark' title='Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync'>Python&#8217;s MySQLdb 2014 Error &#8211; Commands out of sync</a></li>
<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/2009/databases/mysql/counting-email-addresses-by-domain-in-mysql/' rel='bookmark' title='Counting Email Addresses By Domain in MySQL'>Counting Email Addresses By Domain in MySQL</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/databases/mysql/mysql-error-1033-incorrect-information-in-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Counting Email Addresses By Domain in MySQL</title>
		<link>http://eric.lubow.org/2009/databases/mysql/counting-email-addresses-by-domain-in-mysql/</link>
		<comments>http://eric.lubow.org/2009/databases/mysql/counting-email-addresses-by-domain-in-mysql/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 13:50:21 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=92</guid>
		<description><![CDATA[Every so often I find some statistical need that although Perl program would be easy to write for it, its probably something the database should just handle. So I have a column in an email management table that has just the email addresses in the format user@domain.tld. I want to know which domains make up [...]]]></description>
			<content:encoded><![CDATA[<p>Every so often I find some statistical need that although Perl program would be easy to write for it, its probably something the database should just handle.  So I have a column in an email management table that has just the email addresses in the format <strong>user@domain.tld</strong>.</p>
<p>I want to know which domains make up the majority of the users.  (The numbers have been changed to protect the innocent):</p>
<pre>
mysql> SELECT SUBSTRING_INDEX(email, '@', -1) as Domain, count(*) as Total
      FROM email_list
GROUP BY Domain
ORDER BY Total DESC
       LIMIT 15;
+----------------+---------+
| Domain         | Total   |
+----------------+---------+
| yahoo.com      | 1304000 |
| hotmail.com    |  908400 |
| aol.com        |  800000 |
| msn.com        |  168000 |
| gmail.com      |  161000 |
| comcast.net    |  143000 |
| sbcglobal.net  |  110000 |
| bellsouth.net  |   62000 |
| cox.net        |   58000 |
| verizon.net    |   56000 |
| earthlink.net  |   52000 |
| charter.net    |   46000 |
| juno.com       |   30000 |
| optonline.net  |   22000 |
| netzero.com    |   17000 |
+----------------+---------+
</pre>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/mail/list-of-feedback-loops/' rel='bookmark' title='List of Feedback Loops'>List of Feedback Loops</a></li>
<li><a href='http://eric.lubow.org/2009/mail/transferring-email-from-gmailgoogle-apps-to-dovecot-with-larch/' rel='bookmark' title='Transferring Email From Gmail/Google Apps to Dovecot With Larch'>Transferring Email From Gmail/Google Apps to Dovecot With Larch</a></li>
<li><a href='http://eric.lubow.org/2010/seo/seo-and-cross-domain-content-syndication/' rel='bookmark' title='SEO and Cross-Domain Content Syndication'>SEO and Cross-Domain Content Syndication</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/databases/mysql/counting-email-addresses-by-domain-in-mysql/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Character Encoding</title>
		<link>http://eric.lubow.org/2008/databases/mysql/character-encoding/</link>
		<comments>http://eric.lubow.org/2008/databases/mysql/character-encoding/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 21:44:58 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[character encoding]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=77</guid>
		<description><![CDATA[I recently ran into some character encoding issues and I wanted to share the fun. The default character encoding for MySQL on Gentoo is latin-1 or iso-8859-1. This wasn&#8217;t a problem until we recently started putting content straight from the DB through Java and onto the web. Java connects to the DB with a character [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into some character encoding issues and I wanted to share the fun.  The default character encoding for MySQL on Gentoo is latin-1 or iso-8859-1.  This wasn&#8217;t a problem until we recently started putting content straight from the DB through Java and onto the web.  Java connects to the DB with a character encoding (typically UTF-8).  Since UTF-8 is roughly a superset of iso-8859-1, it generally wasn&#8217;t a problem.  Except when UTF-8 and UTF-16 characters were put into an iso-8859-1 database without translation.</p>
<p>What was essentially happening was that the data was being stored as iso-8859-1.  The Java code was connecting to the DB in UTF-8 and pulling it into Java (which is usually UTF-16, but in this case was being handled as UTF-8).  It was then being sent to the browser as URL encoded UTF-8 when in reality, it hadn&#8217;t even properly been put into UTF-8 character encoding.  This then gave the web browser some funny yen symbols and question marks.  This was not quite what we were aiming for.</p>
<p>The moral of this story is that it is necessary to realize the character encoding of the start point and end point of your data.  It is crucial that the code points match up otherwise they could potentially make for an interesting screen given to the reader.  All this could have been avoided with a simple: <strong>ALTER TABLE myTable MODIFY myColumn VARCHAR(255) CHARACTER SET utf8;</strong>.</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>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2008/databases/mysql/character-encoding/feed/</wfw:commentRss>
		<slash:comments>0</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>

