简体   繁体   English

数据库设计 - 选择查询的“单独的表与一个表”

[英]Database design - “Separate Tables Vs One table” for Select Queries

I have a MySQL table like following 我有一个类似于MySQL的表

Books Table 书籍表

book-id      category    author     author_place       book_name   book_price --------other 50 columns directly related to  book-id              
1           adventure    tom          USA               skydiving     300
2           spiritual    rom         Germany           what you are   500
3           adventure    som         India              woo woo       700
4           education    kom         Italy               boring       900
5           adventure    lom         Pak                 yo yo         90
.
.
4000        spiritual    tom          USA                you are          10

As you can see there are around 4000 rows and around 55 columns, I am using this table mostly for select query, Maybe add or update new book after2-3 weeks 你可以看到有大约4000行和大约55列,我使用这个表主要用于选择查询,也许在2-3周之后添加或更新新书

I have doubt about the category and author columns 我对类别和作者专栏有疑问

now if I need to select the table by category and author, I can simply do 现在,如果我需要按类别和作者选择表格,我可以简单地做

SELECT * from books Where author = 'tom'

Select * FROM books WHERE category='education'

It works fine, But according to standard database design I think I should separate the category and authors columns into separate tables (especially authors) and use their primary key as foreign key in the books table Something like this 它工作正常,但根据标准的数据库设计,我认为我应该将类别和作者列分隔成单独的表(尤其是作者),并将它们的主键用作books table中的外键这样的东西

Books Table 书籍表

book-id      categ_id    author_id          book_name   book_price --------other 50 columns directly related to  book-id              
1                   1          1             skydiving     300
2                   2          2             what you are   500
3                   1          3             woo woo       700
4                   3          4             boring       900
5                   1          5              yo yo         90
.
.
4000                3          1              you are          10

Category Table 分类表

categ_id      category_name                
1              advernture         
2              spiritual         
3              education                
.              .
.              .
30             something

Authors Table 作者表

author_id  author      country
 1         tom          USA               
 2         rom         Germany           
 3         som         India             
 4         kom         Italy              
 5         lom         Pak         

But then I have to use join the tables each time I make a select query by authors or category, Which I think will be inefficient, Something like this 但是每次我通过作者或类别进行选择查询时我都必须使用连接表,我觉得效率低,这样的东西

SELECT * FROM Books LEFT JOIN authors on authors.author_id = books.author_id WHERE books.author_id =1
SELECT * FROM Books LEFT JOIN categories on categories.categ_id = books.categ_id_id WHERE books.categ_id =1

So should I separate the first table into separate tables or first table design is better in this case? 那么我应该将第一个表分成单独的表还是第一个表设计在这种情况下更好?

This question has it's answer from Mr. Edgar F. Codd himself - the inventor of the relation model upon which all RDBMS are build. 这个问题得到了Edgar F. Codd先生本人的回答 - 他是所有RDBMS构建的关系模型的发明者。

Shortly after releasing the relational model papers he and his team followed with papers on the so called normal forms. 在发布关系模型论文后不久,他和他的团队随后发表了关于所谓正常形式的论文。 There are few of them but the first 3 (at least) should be generally considered mandatory: 它们很少,但前3个(至少)通常被认为是强制性的:

When you read them you'll see that your initial design is in violation of 2NF and you have come with a solution that more or less respects it. 当您阅读它们时,您会发现您的初始设计违反了2NF并且您已经找到了或多或少尊重它的解决方案。 Go ahead with a the NF-compliant design without any doubts. 继续使用符合NF标准的设计,毫无疑问。

To elaborate a bit on your concerns with Join's performance. 详细说明您对Join的表现的担忧。 This is not an issue as long as the following criteria is met: 只要满足以下条件,这不是问题:

  • your database schema is well designed (2NF compliant at least) 您的数据库架构设计得很好(至少符合2NF)
  • you use Foreign keys to link the tables ( MySQL's docs ) 你使用外键链接表( MySQL的文档
  • you join the tables by their FK 你加入他们的FK表
  • you have the hardware resources necessary to run your data efficiently 您拥有有效运行数据所需的硬件资源

eg on MySQL with InnoDB, on 2NF compliant schema using Foreign keys the join performance by the FK will be among the last things you'd ever be concerned. 例如,在使用InnoDB的MySQL上,在使用外键的2NF兼容模式上,FK的连接性能将是您最不关心的事情之一。

Historically there was a DB engine in MySQL - the MyISAM - that did not support foreign key constraints. 从历史上看,MySQL中有一个数据库引擎--MyISAM--它不支持外键约束。 Perhaps it's the main source of feedback about poor join performance (along poor schema designs of course). 也许它是关于糟糕的连接性能的反馈的主要来源(当然,不良的模式设计)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM