2013-02-16 80 views
0

我有3个表格存储了用户注册的常用数据:语言,国家,国籍。每个表格都有字段:ID名称MySQL临时表或视图?

我得到了一个主表用户它存储几乎所有来自用户的数据。

另一台名为tableregistry它具有这样的结构:

id | tableName | tableValue 

1 | finalJoin | 0 

2 | language | 1 

3 | country | 2 

4 |nationality| 3 

还有一,它存储了一个名为巧合公共数据共享许多用户:

id | idUser | nTable | cValue 

因此,如果我们有第80名用户在荷兰居住,但他是来自秘鲁的本地人,并且会说中文,数据会以这种方式保存下来(考虑到荷兰拥有ID在国家表20,秘鲁国籍有识别码34在国籍表和中国语言对语言表中的id 22)

198 | 80 | 2  | 20 

199 | 80 | 3  | 34 

200 | 80 | 1  | 22 

所以,如果我们要进行搜索我使用存储过程搜索巧合常见的数据只是获得3个临时表来获取用户1.来自某个国家2.在任何国家生活都不是原生的,3.说某种语言。

通过表用户为这些临时表执行多重连接我们将获得此搜索的用户列表。

问题是。会更好地使用视图还是只保留临时表策略?

回答

0

你有一个奇怪的模式。这个怎么样:

CREATE TABLE users (
    id int(11) not null auto_increment, 
    ... 
); 

CREATE TABLE languages (
    id int(11) not null auto_increment, 
    name varchar(20) not null 
); 

CREATE TABLE countries (
    id int(11) not null auto_increment, 
    name varchar(20) not null 
); 

CREATE TABLE nationalities (
    id int(11) not null auto_increment, 
    name varchar(20) not null 
); 

CREATE TABLE user_rel_languages (
    user_id int(11) not null, 
    language_id int(11) not null 
); 

CREATE TABLE user_rel_countries (
    user_id int(11) not null, 
    country_id int(11) not null 
); 

CREATE TABLE user_rel_nationalities (
    user_id int(11) not null, 
    nationality_id int(11) not null 
); 

因此,要获得与语言+国家+国籍的特定配置的用户,你就将从users选择以及与每个通过关系表的表的加盟。例如:

select u.* from users u 
join user_rel_countries urc on urc.user_id = u.id 
join user_rel_languages url on url.user_id = u.id 
join user_rel_nationalities urn on urn.user_id = u.id 
where country_id = 1 and language_id = 2 and nationality_id = 3 
group by u.id ; 

或者,如果你不关心denormalisation,你可能会下降countriesuser_rel_countries之间的区别

+0

好了,这是一个标准的模式,这是完全确定,但怎么样,如果我们的表现谈论数千条记录。而更糟糕的情况是......如果总体实体长大,那该怎么办?那么,我必须为每个新实体和各自的关系userid_entity?创建更多的表。 对不起,如果我听起来有点讨厌,但我试图得到这个项目的最佳性能 – user1822528 2013-02-16 03:59:22

+0

如果他们是不同的类型,那么是的 - 我会为每个创建一个单独的表。只要您在关键列上添加索引,这种性能就会很好地扩展。 – troelskn 2013-02-16 18:00:23

+0

您的意思是将索引放在user_rel_ *表的nationality_id,country_id和language_id列上? – user1822528 2013-02-17 21:24:47