<?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>Alex Stetsenko &#187; MySQL</title>
	<atom:link href="http://www.stetsenko.net/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stetsenko.net</link>
	<description>Life and software engineering :: thoughts and steps</description>
	<lastBuildDate>Tue, 17 Jan 2012 16:29:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL: How to add where clause to processlist</title>
		<link>http://www.stetsenko.net/2011/08/mysql-how-to-add-where-clause-to-processlist/</link>
		<comments>http://www.stetsenko.net/2011/08/mysql-how-to-add-where-clause-to-processlist/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 00:58:30 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=1140</guid>
		<description><![CDATA[MySQL&#8217;s show full processlist is a great way to monitor and diagnose what is going on with the database server at certain moment. However often it is needed to have an aggregated view or evaluate only specific type of users and\or queries. The solution turned out to be pretty simple since show full processlist is [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL&#8217;s <strong>show full processlist</strong> is a great way to monitor and diagnose what is going on with the database server at certain moment.</p>
<p>However often it is needed to have an aggregated view or evaluate only specific type of users and\or queries.</p>
<p>The solution turned out to be pretty simple since <strong>show full processlist</strong> is just an alias for a regular query to <strong>processlist table</strong> within <strong>information_schema database</strong>.</p>
<p>So the following queries are equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">FULL</span> PROCESSLIST;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist;</pre></div></div>

<p>As result it is possible to present the data from process list any convenient way. There are some basic examples below.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- display number of connections for each user</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`USER`</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`USER`</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- display number of connections for each host</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`HOST`</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`HOST`</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- display root user activity</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`USER`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'root'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- display processes associated with SELECT queries</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`INFO`</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'SELECT %'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- display average query time for each database</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">`DB`</span><span style="color: #66cc66;">,</span> AVG<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`TIME`</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> information_schema<span style="color: #66cc66;">.</span>processlist
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">`DB`</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/08/mysql-how-to-add-where-clause-to-processlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Current timestamp for new or updated rows</title>
		<link>http://www.stetsenko.net/2011/08/mysql-current-timestamp-for-new-or-updated-rows/</link>
		<comments>http://www.stetsenko.net/2011/08/mysql-current-timestamp-for-new-or-updated-rows/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 02:31:15 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=1112</guid>
		<description><![CDATA[Often it is quite reasonable to have the database to take care of some audit related columns like date added or updated. Unfortunately MySQL does NOT allow functions within table definitions like let&#8217;s say MS SQL does, e.g.: CREATE TABLE t &#40;f datetime NOT NULL DEFAULT now&#40;&#41;&#41;; The only work around is to use timestamp [...]]]></description>
			<content:encoded><![CDATA[<p>Often it is quite reasonable to have the database to take care of some audit related columns like date added or updated.<br />
Unfortunately MySQL does NOT allow functions within table definitions like let&#8217;s say MS SQL does, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t <span style="color: #66cc66;">&#40;</span>f datetime <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> now<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The only work around is to use <strong>timestamp</strong> column instead of date or datetime ones. There can be <strong>only one auto set timestamp column</strong> though.<br />
So I normally use it to store date updated and save the date created manually.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t
<span style="color: #66cc66;">&#40;</span>
  name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  date_updated <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
               <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Also there are <strong>two things to keep in mind</strong>:</p>
<ul>
<li>date_updated will be set based on MySQL server time. This is especially important if other data is stored using GMT or non-server time zone.</li>
<li>date_updated must NOT be a part of update or insert query.
        </li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- set date_updated to current time</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'test'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> t <span style="color: #993333; font-weight: bold;">SET</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'test'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- override date_updated to certain value</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t <span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">,</span> date_updated<span style="color: #66cc66;">&#41;</span>
       <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'test'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2011-08-02 20:00:00'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> t <span style="color: #993333; font-weight: bold;">SET</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'test'</span><span style="color: #66cc66;">,</span>
             date_updated <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'2011-08-02 20:00:00'</span>;</pre></div></div>

<p>Alternatively it is possible to use <strong>triggers</strong> to have as many auto updated datetime columns as needed. However personally I prefer to avoid triggers as much as possible. Mostly because often triggers are misused and as result application complexity is increased for no reason.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/08/mysql-current-timestamp-for-new-or-updated-rows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: recover root password</title>
		<link>http://www.stetsenko.net/2011/07/mysql-recover-root-password/</link>
		<comments>http://www.stetsenko.net/2011/07/mysql-recover-root-password/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 04:16:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=1094</guid>
		<description><![CDATA[Apparently there is a pretty straight forward way to recover MySQL password. Although server root access is mandatory it is great solution in certain cases like recovering password of abandoned test server or when server was configured by someone else etc. The key for success is to be able to (re)start MySQL using -skip-grant-tables option. [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently there is a pretty straight forward way to recover MySQL password. Although server root access is mandatory it is great solution in certain cases like recovering password of abandoned test server or when server was configured by someone else etc.</p>
<p>The key for success is to be able to (re)start MySQL using <strong>-skip-grant-tables</strong> option. In that case MySQL won&#8217;t ask for password.<br />
Then simply reset MySQL password and restart MySQL ordinarily.</p>
<p>Details for each step are described by Vivek Gite in <a href="http://www.cyberciti.biz/tips/recover-mysql-root-password.html">Recover MySQL root Password</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/07/mysql-recover-root-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to hide queries in the general log</title>
		<link>http://www.stetsenko.net/2011/04/mysql-how-to-hide-queries-in-the-general-log/</link>
		<comments>http://www.stetsenko.net/2011/04/mysql-how-to-hide-queries-in-the-general-log/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 02:31:12 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=963</guid>
		<description><![CDATA[There could be some cases when the general query log has to be kept running. This includes development stage, different flavors of debugging and query analysis and so on. However certain queries do not need to be stored there even though the logging is on. One of the examples is setting user password &#8211; it [...]]]></description>
			<content:encoded><![CDATA[<p>There could be some cases when the general query log has to be kept running. This includes development stage, different flavors of debugging and query analysis and so on. However certain queries do not need to be stored there even though the logging is on. One of the examples is setting user password &#8211; it will be logged in clear text.</p>
<p>A possible workaround is to turn off the general logging temporarily. MySQL 5.1 and higher allows to do it without the service restart.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">USER</span> john;
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">SESSION</span> sql_log_off <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #993333; font-weight: bold;">SET</span> PASSWORD <span style="color: #993333; font-weight: bold;">FOR</span> john <span style="color: #66cc66;">=</span> PASSWORD<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'smith'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">SESSION</span> sql_log_off <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</pre></div></div>

<p>However SUPER privilege is required to set the session variable <strong>sql_log_off</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/04/mysql-how-to-hide-queries-in-the-general-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Max allowed packet or file upload watch-out</title>
		<link>http://www.stetsenko.net/2010/11/mysql-max-allowed-packet-or-file-upload-watch-out/</link>
		<comments>http://www.stetsenko.net/2010/11/mysql-max-allowed-packet-or-file-upload-watch-out/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 21:02:00 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=878</guid>
		<description><![CDATA[Apparently some web applications do store uploaded files in the database as blob field by default (for instance it is true for Mantis bug tracker). While it is acceptable (but not too common?) to store files in the database, it may cause certain problems like uploading large files (particularly 1Mb+). Normally the first thing to [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently some web applications do store uploaded files in the database as blob field by default (for instance it is true for Mantis bug tracker). While it is acceptable (but not too common?) to store files in the database, it may cause certain problems like uploading large files (particularly 1Mb+).</p>
<p>Normally the first thing to look at would be the actual application configuration. OK, Mantis is configured to accept files up to 5Mb by default. I.e. no problems here. Wait&#8230; but PHP is configured to accept files up to 2Mb by default&#8230; So where does 1Mb restriction come from?</p>
<p>The answer is that it comes from the database. MySQL has limits on packet size. The largest possible packet that can be transmitted to or from a MySQL 5.1 server or client is 1Gb.  <strong>However by default it is 1Mb</strong>! As result any packets larger than 1Mb will cause <a href="http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html">packet too large</a> error.</p>
<p>There is <strong>max_allowed_packet</strong> option in the configuration which allows to set the packet size. For example:</p>
<pre>
[mysqld]
max_allowed_packet=16M
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/11/mysql-max-allowed-packet-or-file-upload-watch-out/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL temporary table watch-out &#8211; unknown column error</title>
		<link>http://www.stetsenko.net/2010/11/mysql-temporary-table-watch-out-unknown-column-error/</link>
		<comments>http://www.stetsenko.net/2010/11/mysql-temporary-table-watch-out-unknown-column-error/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 02:53:04 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=868</guid>
		<description><![CDATA[Recently I’ve run into another MySQL bug. This time it is related to temporary tables. The bug was reported as #12257. Basically select * from tmpTable does not work within stored procedure if any column is altered since the table has been originally created. So MySQL keeps converting * to a old column list while [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I’ve run into another MySQL bug. This time it is related to temporary tables. The bug was reported as <a href="http://bugs.mysql.com/bug.php?id=12257">#12257</a>.</p>
<p>Basically <em>select * from tmpTable</em> does not work within stored procedure if any column is altered since the table has been originally created. So MySQL keeps converting <em>*</em> to a old column list while some columns may not exist anymore.</p>
<p>A simple workaround allows to eliminate impact of this bug as well as using <em>select *</em> is not recommended anyway. However the most surprising part of this story that this bug cannot be resolved for more than 5 years: it was reported in 2005.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/11/mysql-temporary-table-watch-out-unknown-column-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to store IP address</title>
		<link>http://www.stetsenko.net/2010/10/mysql-how-to-store-ip-address/</link>
		<comments>http://www.stetsenko.net/2010/10/mysql-how-to-store-ip-address/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 01:13:20 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=843</guid>
		<description><![CDATA[It is still common to store IP addresses as a varchar(15) field though it is possible to use integer type instead. Unlike the varchar type, integer has fixed size and uses only 4 bytes. INET_ATON() is used to convert an IP address to a number and INET_NTOA() &#8211; for the reverse operation. SELECT INET_ATON&#40;'127.0.0.1'&#41;; SELECT [...]]]></description>
			<content:encoded><![CDATA[<p>It is still common to store IP addresses as a <em>varchar(15)</em> field though it is possible to use <em>integer</em> type instead. Unlike the <em>varchar</em> type, <em>integer</em> has fixed size and uses only 4 bytes.</p>
<p><strong>INET_ATON()</strong> is used to convert an IP address to a number and <strong>INET_NTOA()</strong> &#8211; for the reverse operation.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">INET_ATON</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'127.0.0.1'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">INET_NTOA</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2130706433</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>It is important to use <strong>INT UNSIGNED</strong> with <strong>INET_ATON()</strong> so that IP addresses for which the first octet is greater than 127 is stored correctly.</p>
<p>Also PHP has similar functions &#8211; <strong>ip2long()</strong> and <strong>long2ip()</strong>. However <strong>ip2long()</strong> function may return negative results in certain cases. To make it always positive unsigned intereger <strong>ip2long()</strong> call has to be paired with <em>printf()</em> or <em>sprintf()</em> function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%u'</span><span style="color: #339933;">,</span> <span style="color: #990000;">ip2long</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'128.0.0.1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus plan the IP address column datatype accordingly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/10/mysql-how-to-store-ip-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to find an optimal datatype for the columns</title>
		<link>http://www.stetsenko.net/2010/10/mysql-how-to-find-an-optimal-datatype-for-the-columns/</link>
		<comments>http://www.stetsenko.net/2010/10/mysql-how-to-find-an-optimal-datatype-for-the-columns/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 00:31:10 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=836</guid>
		<description><![CDATA[PROCEDURE ANALYSE examines the result from a query and returns an analysis of the results that suggests optimal data types for each column that may help reduce table sizes. Simply append PROCEDURE ANALYSE to the end of a select statement: SELECT f1, f2 FROM t PROCEDURE ANALYSE&#40;20, 2000&#41;; where 10 is is the maximum number [...]]]></description>
			<content:encoded><![CDATA[<p><strong>PROCEDURE ANALYSE</strong> examines the result from a query and returns an analysis of the results that suggests optimal data types for each column that may help reduce table sizes. Simply append <strong>PROCEDURE ANALYSE</strong> to the end of a select statement:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> f1<span style="color: #66cc66;">,</span> f2 <span style="color: #993333; font-weight: bold;">FROM</span> t <span style="color: #993333; font-weight: bold;">PROCEDURE</span> ANALYSE<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>where</p>
<ul>
<li>10 is is the maximum number of distinct values which is checked for columns;</li>
<li>2000 is the maximum amount of memory which can be allocated per column while trying to find all distinct values.</li>
</ul>
<p>This MySQL feature can be helpful after importing new data or for checking your existing tables to verify whether columns datatype was designed providently.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/10/mysql-how-to-find-an-optimal-datatype-for-the-columns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: partition watchout</title>
		<link>http://www.stetsenko.net/2010/09/mysql-partition-watchout/</link>
		<comments>http://www.stetsenko.net/2010/09/mysql-partition-watchout/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 00:38:34 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=829</guid>
		<description><![CDATA[When designing partitions keep in mind that it will be required to have the number of partitions and open file limit system variable in harmony. Thus each partition creates an overhead of extra two files (#p.myd and #p.myi). I.e. partition by hash of month will produce 2 * 12 months = 24 extra files. Not [...]]]></description>
			<content:encoded><![CDATA[<p>When designing partitions keep in mind that it will be required to have the number of partitions and <em>open file limit</em> system variable in harmony.</p>
<p>Thus each partition creates an overhead of extra two files (#p.myd and #p.myi). I.e. partition by hash of month will produce 2 * 12 months = 24 extra files. Not too bad for the default MySQL settings. However if there is plan for much more partitions, the <em>open file limit</em> has to be tuned respectively. Otherwise the error <strong>&#8220;mysql out of resources when opening file errcode 24&#8243;</strong> may occur.</p>
<p>To change the number of file descriptors available to MySQL, you can set <strong>open-files=2048</strong> (or to any other reasonable number) in the  MySQL configuration file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/09/mysql-partition-watchout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to force a new unique index to drop duplicated rows</title>
		<link>http://www.stetsenko.net/2010/08/mysql-how-to-force-a-new-unique-index-to-drop-duplicated-rows/</link>
		<comments>http://www.stetsenko.net/2010/08/mysql-how-to-force-a-new-unique-index-to-drop-duplicated-rows/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 01:06:58 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=802</guid>
		<description><![CDATA[If table is not empty, there may be duplicated records. If so a regular alter query to create a new unique index will return an error like below. ERROR 1062 (23000): Duplicate entry &#8217;132653-47&#8242; for key 1 However keyword IGNORE allows to force a new unique index to drop duplicated rows. ALTER IGNORE TABLE `t` [...]]]></description>
			<content:encoded><![CDATA[<p>If table is not empty, there may be duplicated records. If so a regular alter query to create a new unique index will return an error like below.</p>
<blockquote><p>ERROR 1062 (23000): Duplicate entry &#8217;132653-47&#8242; for key 1</p></blockquote>
<p>However keyword <strong>IGNORE</strong> allows to force a new unique index to drop duplicated rows.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">IGNORE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">ADD</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #ff0000;">`i`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`f1`</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`f2`</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In this case the output will be something like this:</p>
<blockquote><p>Query OK, 507 rows affected (0.02 sec)<br />
Records: 507 Duplicates: 0 Warnings: 0</p></blockquote>
<p>So simply using <strong>IGNORE</strong> helps to save time on writing a custom script to clean up duplicates.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/08/mysql-how-to-force-a-new-unique-index-to-drop-duplicated-rows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

