技术开发 频道

测试InnoDB如何处理无主键表

  【IT168技术】测试InnoDB如何处理无主键表,如何生成clust index,如何产生clust index column

CREATE TABLE `DailyStatistics` (
     `UserId`
bigint(20) NOT NULL DEFAULT '0',
     `BlogCount`
int(11) NOT NULL DEFAULT '0',
     `PhotoCount`
int(11) NOT NULL DEFAULT '0',
     `SpaceUsed`
int(11) NOT NULL DEFAULT '0',
     `
DateTime` bigint(20) NOT NULL DEFAULT '0',
    
KEY `IDX_DAYSTAT_USERID_DATETIME` (`UserId`,`DateTime`)
   ) ENGINE
=INNODB DEFAULT CHARSET=gbk;

  语句一:

insert into dailystatistics values (1,2,3,4,5);

    生成clust index column(rowid)方法:

row_insert_for_mysql -> row_ins_step -> row_ins ->
row_ins_alloc_row_id_step(生成rowid)
->
dict_sys_get_new_row_id
->
mutex_enter(
&(dict_sys->mutex));
id
= dict_sys->row_id;
mutex_exit(
&(dict_sys->mutex));

  此时产生的row_id = 2050

  语句二:

update dailystatistics set
UPDATE DailyStatistics SET userid = 1021 WHERE (UserId = 1) AND (DateTime = 5);

  读取clust index column方法:

row_search_for_mysql -> row_sel_store_row_id_to_prebuilt ->

  此时取出的row_id = 520,不等于2050,这是为什么呢?

  分析

  rowid如何存储?

  2050 = 1000,0000,0010

  520 = 0010,0000,1000

  因此,只要将2050反向存储,既为520。

  2051 = 1000,0000,0011 取出的row_id应该是:0011,0000,1000 = 776

  2052 = 1000,0000,0100 ~ = 100,0000,1000 = 1032,验证通过。

  尽量为所有表指定主键。

  n 如果不指定主键,InnoDB会产生一个全局的rowid序列。所有InnoDB非主键表共享这一序列,并发性能较差,因此建议所有InnoDB表,指定主键。

  原文地址:http://www.mysqlops.com/2011/12/05/innodb-without-key.html

0
相关文章