<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Better Primary Keys, a Benefit to TokuDB&#8217;s Auto Increment Semantics</title>
	<atom:link href="http://tokutek.com/2009/08/better_primary_keys_a_benefit_to_tokudbs_auto_increment_semantics/feed/" rel="self" type="application/rss+xml" />
	<link>http://tokutek.com/2009/08/better_primary_keys_a_benefit_to_tokudbs_auto_increment_semantics/</link>
	<description>Database Performance</description>
	<lastBuildDate>Sat, 28 Aug 2010 12:16:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Kristian Nielsen</title>
		<link>http://tokutek.com/2009/08/better_primary_keys_a_benefit_to_tokudbs_auto_increment_semantics/comment-page-1/#comment-37</link>
		<dc:creator>Kristian Nielsen</dc:creator>
		<pubDate>Tue, 04 Aug 2009 15:39:18 +0000</pubDate>
		<guid isPermaLink="false">http://better_primary_keys_a_benefit_to_tokudbs_auto_increment_semantics#comment-37</guid>
		<description>My standard trick to do this in InnoDB (or other databases) is this:

CREATE TABLE t (
  c1 int,
  c2 varchar(30),
  c3 date,
  idx smallint,
  PRIMARY KEY (c3, idx)
);

Then you insert rows like this:

    INSERT INTO t (c1,c2,c3,idx) SELECT 42, &quot;my c2 value&quot;, &quot;2007-01-10&quot;, IFNULL(MAX(idx)+1, 0) FROM t WHERE c3 = &quot;2007-01-10&quot;

(Don&#039;t get fooled by the INSERT ... SELECT; this inserts a single row with constant values except for the `idx` column, which gets the next number in sequence).

This gets the same clustered index storage without the need for an autoincrement key.

An advantage is that values of `idx` counts from 0 for each distinct date value, so data looks nicer and may be able to use a smaller integer size to save space and index btree size. A disadvantage is the need to compute the MAX() for inserts, so bulk insertion is more complicated.</description>
		<content:encoded><![CDATA[<p>My standard trick to do this in InnoDB (or other databases) is this:</p>
<p>CREATE TABLE t (<br />
  c1 int,<br />
  c2 varchar(30),<br />
  c3 date,<br />
  idx smallint,<br />
  PRIMARY KEY (c3, idx)<br />
);</p>
<p>Then you insert rows like this:</p>
<p>    INSERT INTO t (c1,c2,c3,idx) SELECT 42, &#8220;my c2 value&#8221;, &#8220;2007-01-10&#8243;, IFNULL(MAX(idx)+1, 0) FROM t WHERE c3 = &#8220;2007-01-10&#8243;</p>
<p>(Don&#8217;t get fooled by the INSERT &#8230; SELECT; this inserts a single row with constant values except for the `idx` column, which gets the next number in sequence).</p>
<p>This gets the same clustered index storage without the need for an autoincrement key.</p>
<p>An advantage is that values of `idx` counts from 0 for each distinct date value, so data looks nicer and may be able to use a smaller integer size to save space and index btree size. A disadvantage is the need to compute the MAX() for inserts, so bulk insertion is more complicated.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
