<?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>JJB Blog &#187; clustered_index</title>
	<atom:link href="http://blog.johnjosephbachir.org/tag/clustered_index/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.johnjosephbachir.org</link>
	<description></description>
	<lastBuildDate>Wed, 04 Jan 2012 05:47:01 +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>Everything you need to know about designing MySQL InnoDB primary keys</title>
		<link>http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/</link>
		<comments>http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/#comments</comments>
		<pubDate>Sun, 22 Oct 2006 18:53:12 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[b_tree]]></category>
		<category><![CDATA[clustered_index]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[key]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[primary]]></category>
		<category><![CDATA[primary_key]]></category>

		<guid isPermaLink="false">http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/</guid>
		<description><![CDATA[There are two aspects of how MySQL/InnoDB use a table&#8217;s primary key on the file system that have important ramifications for the key&#8217;s design: The primary key determines the order in which the data is physically stored in the main data file, aka &#8220;the clustered index&#8221;. Another way of saying this: the main data file [...]]]></description>
			<content:encoded><![CDATA[<p>There are two aspects of how MySQL/InnoDB use a table&#8217;s primary key on the file system that have important ramifications for the key&#8217;s design:</p>
<ul>
<li>The primary key determines the order in which the data is physically stored in the main data file, aka &#8220;the clustered index&#8221;. Another way of saying this: the main data file is a B-tree index that directly contains all of a table&#8217;s columns, and the key on this B-tree is the primary key. This can result in one less I/O operation (compared to MyISAM) on many queries.</li>
<li>The primary key is what is used to associate <em>all</em> of a table&#8217;s indexes with the main data file. So the primary key is replicated in every row of every index.</li>
</ul>
<p>How to design your InnoDB primary keys:</p>
<ol>
<li><strong>Do explicitly define a primary key, and make it numeric.</strong> If no primary key is specified, or a non-numeric column is used as a primary key, InnoDB will used its own internal auto-incremented row ID to link between the main data table and the other indexes. This row ID is 6 bytes. The biggest numeric type in MySQL, BIGINT, is 8 bytes, and the next smallest type, INT, is 4 bytes. So not only is this &#8220;hidden&#8221; row ID an entire extra redundant column, it is also bigger than the data type that would be used in the vast majority of cases where a primary key is explicitly specified. Furthermore, when InnoDB falls back to using this internal row ID, <em>the rows in the main data file are ordered by insertion order instead of your primary key, rendering InnoDB&#8217;s clustering behavior useless, and adding an entire extra I/O operation on some queries</em>. This is a big loss.</li>
<li>As alluded to above, <strong>the primary key should be sequential.</strong> Because the structure of the data on the disk is dictated by the primary key, it should be sequential, to avoid extremely expensive I/O on writes (when the B-Tree has to be reorganized), and to take advantage of the native clustering.</li>
<li><strong>The primary key should be as small as possible.</strong> Because the primary key is replicated in every entry of every index, designing the primary key to be as small as possible impacts the efficiency of all the indexes.</li>
</ol>
<p>Using these 3 simple guidelines on a well-designed schema, InnoDB can perform at its full potential.</p>
<p>Above I mentioned that the primary key is stored in every row of every index. This includes the entire contents of a multi-column primary key. In a future article I will discuss scenarios/designs that can take advantage of this behavior.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

