MySql DB设计-两种设计中哪一种最好-规范化与否

MySql DB设计-两种设计中哪一种最好-规范化与否,mysql,database,Mysql,Database,请看下面的分析,让我知道这两种设计中最好的数据库设计(InnoDB)。要求-当存在多个并发数据库连接时,用户无需等待即可更快地进行写和读操作,而并发数据库连接的数量预计会呈指数增长。如果用户必须等待,则磁盘空间优势是无关紧要的 假设–单CPU(仅用于比较) 方法1(M1)表1用户配置文件->用户ID、城市、州、国家 方法2(M2)(标准化)表2A用户配置文件->用户ID,位置SID表2B位置->位置SID,城市,州,国家 写入(顺序不正确) a。写入表格 M1直接写入=t1 M2-(搜索表2B以

请看下面的分析,让我知道这两种设计中最好的数据库设计(InnoDB)。要求-当存在多个并发数据库连接时,用户无需等待即可更快地进行写和读操作,而并发数据库连接的数量预计会呈指数增长。如果用户必须等待,则磁盘空间优势是无关紧要的

假设–单CPU(仅用于比较)

方法1(M1)表1用户配置文件->用户ID、城市、州、国家

方法2(M2)(标准化)表2A用户配置文件->用户ID,位置SID表2B位置->位置SID,城市,州,国家

写入(顺序不正确)

a。写入表格

M1直接写入=t1 M2-(搜索表2B以查看记录是否存在=t2+如果不匹配则插入=表2a中的t1写入用户ID和位置SID=t3) (t1+t2+t3)>t1

b、 CPU中断

M1=1,M2=2

c、 磁盘I/O

M1=1,M2=2

d、 行锁定和释放

M1=1,M2=2

e。磁盘空间

M1=更多,M2=更少(仅在M2中占优势)

读取(假设记录不在缓存中)

a。从表中读出

M1直接读取=t4,M2-Join-t5 t5>t4

b。CPU中断

M1=1,M2=2

c、 磁盘I/O

M1=1,M2=2

我相信,如果表2B是预先填充的,或者如果国家、州、城市下拉列表是数字标记的,那么方法2所花费的时间可以得到改善。 即使您认为负载平衡M1似乎是一个有吸引力的设计。增加BW可能会使情况恶化,因为会有更多的并发DB连接。让我知道你的想法

方法2(M2)(标准化)表2A用户配置文件->用户ID,位置SID表2B位置->位置SID,城市,州,国家

您已将城市、州和国家替换为id号。虽然在某些情况下,这可能是一个好的设计决策,但并不总是一个好的设计决策。这与正常化无关。(没有“我用了一个身份证号码”这样的标准形式。)

当有国际标准时,使用它通常是有意义的。 看见三个字母的代码可能更有意义

-- Untested code.
create table countries (
  iso_country_code char(2) not null,
  country_name varchar(35) not null,
  primary key (iso_country_code),
  unique (country_name)
);

create table states (
  state_code char(2) not null,          -- application-dependent, consider ISO 3166-2
  state_abbrev varchar(7) not null,
  state_name varchar(35) not null,
  iso_country_code char(2) not null,
  primary key (state_code, iso_country_code),
  unique (state_abbrev, iso_country_code),
  unique (state_name, iso_country_code),
  foreign key (iso_country_code) references countries (iso_country_code)
);

create table cities (
  city_name varchar(35) not null,
  state_code char(2) not null,
  iso_country_code char(2) not null,
  primary key (city_name, state_code, iso_country_code),
  foreign key (state_code, iso_country_code) 
    references states (state_code, iso_country_code)
);

create table UserProfile (
  UserID integer not null,
  city_name varchar(35) not null,
  state_code char(2) not null,
  iso_country_code char(2) not null,
  primary key (UserID),
  foreign key (city_name, state_code, iso_country_code) 
    references cities (city_name, state_code, iso_country_code)
);
国家、州和城市的单独表格使使用SELECT语句填充组合框变得容易。它们不需要数字“标签”。所有这三个表都是关键的;它们没有非素数属性。我想他们在5NF

根据经验,不要搜索某一行以查看它是否存在,如果不存在则插入。这需要两次往返数据库

相反,只需插入行,并捕获重复的错误。无论如何,您都必须捕获错误——除了重复之外,还有很多因素会阻止插入成功

方法2(M2)(标准化)表2A用户配置文件->用户ID,位置SID表2B位置->位置SID,城市,州,国家

您已将城市、州和国家替换为id号。虽然在某些情况下,这可能是一个好的设计决策,但并不总是一个好的设计决策。这与正常化无关。(没有“我用了一个身份证号码”这样的标准形式。)

当有国际标准时,使用它通常是有意义的。 看见三个字母的代码可能更有意义

-- Untested code.
create table countries (
  iso_country_code char(2) not null,
  country_name varchar(35) not null,
  primary key (iso_country_code),
  unique (country_name)
);

create table states (
  state_code char(2) not null,          -- application-dependent, consider ISO 3166-2
  state_abbrev varchar(7) not null,
  state_name varchar(35) not null,
  iso_country_code char(2) not null,
  primary key (state_code, iso_country_code),
  unique (state_abbrev, iso_country_code),
  unique (state_name, iso_country_code),
  foreign key (iso_country_code) references countries (iso_country_code)
);

create table cities (
  city_name varchar(35) not null,
  state_code char(2) not null,
  iso_country_code char(2) not null,
  primary key (city_name, state_code, iso_country_code),
  foreign key (state_code, iso_country_code) 
    references states (state_code, iso_country_code)
);

create table UserProfile (
  UserID integer not null,
  city_name varchar(35) not null,
  state_code char(2) not null,
  iso_country_code char(2) not null,
  primary key (UserID),
  foreign key (city_name, state_code, iso_country_code) 
    references cities (city_name, state_code, iso_country_code)
);
国家、州和城市的单独表格使使用SELECT语句填充组合框变得容易。它们不需要数字“标签”。所有这三个表都是关键的;它们没有非素数属性。我想他们在5NF

根据经验,不要搜索某一行以查看它是否存在,如果不存在则插入。这需要两次往返数据库

相反,只需插入行,并捕获重复的错误。无论如何,您都必须捕获错误——除了重复之外,还有很多因素会阻止插入成功