Comments on: When to use surrogate keys in InnoDB tables http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Nappstack http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/#comment-20049 Nappstack Wed, 09 May 2012 01:19:43 +0000 http://www.xaprb.com/blog/?p=128#comment-20049 I had a compound Primary key (CPK) in one of my table ‘A’. This CPK is also used as foreign keys to my other table ‘B’.

Now I create another database where this CPK is converted into surrogate key for table ‘A1′. Now, I want to reference the data in my table ‘B1′ with this surrogate key. (note A1 and B1 are in different database.)

Can anyone help me how to do it?

Please let me know if you need more information.

]]>
By: Vineet http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/#comment-19168 Vineet Sun, 20 Feb 2011 07:49:50 +0000 http://www.xaprb.com/blog/?p=128#comment-19168 (typo)

primary key(fk_author_id , blog_id),

would be

primary key(fk_blogger_id , blog_id),

]]>
By: Vineet http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/#comment-19167 Vineet Sun, 20 Feb 2011 06:51:52 +0000 http://www.xaprb.com/blog/?p=128#comment-19167 This was really very helpful.
But I have some doubt about the benchmark by Murray.
What if the table is like this

create table blog(
fk_blogger_id int(10),
blog_id int(10) auto_increment,
….other columns,
primary key(fk_author_id , blog_id),
unique key (blog_id),
foreign key (fk_blogger_id) references blogger(blogger_id)
) ENGINE=InnoDB;

“blogger” is a separate table of bloggers.

Now, say i have say 1-2 blog inserts every second posted by a random bloggers.
would it be a efficient table design?
I would prefer this design as it groups blogs per blogger. Direct access of a blog will use the unique index of blog_id and that would result in two index lookups. But as i would show other blogs of same blogger on the page, The grouping would speed up the overall efficiency.

Am I thinking right? are there any benchmarks around similar solution

great stuff though!

]]>
By: Dan Kamins http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/#comment-17227 Dan Kamins Wed, 04 Nov 2009 08:47:40 +0000 http://www.xaprb.com/blog/?p=128#comment-17227 Fantastic article. Your comment about “page splits and b-tree re-balancings” on inserts would be a great addition to the main text and was very helpful for me.

]]>
By: David Phillips http://www.xaprb.com/blog/2006/05/10/when-to-avoid-and-when-to-use-surrogate-keys-in-innodb-tables/#comment-14075 David Phillips Mon, 10 Dec 2007 22:17:12 +0000 http://www.xaprb.com/blog/?p=128#comment-14075 Murray,

As Xaprb said, your second insert is slow because you are inserting in a random order. Before loading, sort your data in primary key order and the two inserts should run in similar time.

]]>