你應該正常化您設計如下:
drop table if exists users;
create table users
(
user_id int unsigned not null auto_increment primary key,
username varbinary(32) unique not null
)
engine=innodb;
drop table if exists properties;
create table properties
(
property_id smallint unsigned not null auto_increment primary key,
name varchar(255) unique not null
)
engine=innodb;
drop table if exists user_property_values;
create table user_property_values
(
user_id int unsigned not null,
property_id smallint unsigned not null,
value varchar(255) not null,
primary key (user_id, property_id),
key (property_id)
)
engine=innodb;
insert into users (username) values ('f00'),('bar'),('alpha'),('beta');
insert into properties (name) values ('age'),('gender');
insert into user_property_values values
(1,1,'30'),(1,2,'Male'),
(2,1,'24'),(2,2,'Female'),
(3,1,'18'),
(4,1,'26'),(4,2,'Male');
從性能的角度來看,InnoDB的聚集索引工程奇蹟在這個類似的例子(COLD RUN):
select count(*) from product
count(*)
========
1,000,000 (1M)
select count(*) from category
count(*)
========
250,000 (500K)
select count(*) from product_category
count(*)
========
125,431,192 (125M)
select
c.*,
p.*
from
product_category pc
inner join category c on pc.cat_id = c.cat_id
inner join product p on pc.prod_id = p.prod_id
where
pc.cat_id = 1001;
0:00:00.030: Query OK (0.03 secs)
您所描述的內容稱爲分區。在數據庫中,您可以設計圍繞數據的可瀏覽結構。您設置硬件來管理這類問題。我相信parttioning(這是你正在談論的sql服務器術語)是一個不在mysql中的功能 - 或者它不是7年前當我切換到sql server - 我知道它支持它。 – 2010-11-07 11:23:16
@John Nicholas:http://dev.mysql.com/doc/refman/5.1/en/partitioning.html – 2010-11-07 11:26:48
coool tyvm;)mysql確實有分區 – 2010-11-07 13:19:53