简体   繁体   English

MySQL的速度:具有多个列的表或使用联接的2个表

[英]Mysql speed: table with many columns or 2 tables using a join

I tried searching for this but I could not find anything about this but design choices. 我尝试搜索此内容,但除了设计选择之外,找不到任何其他内容。

So my question is like the title. 所以我的问题就像标题一样。 What is faster? 什么更快? Create 1 table with many columns or create 2 or 3 (for many to many) tables with join(s). 用多个列创建1个表,或使用连接创建2或3个(多对多)表。

I like the idea of have multiple tables so the data is separated. 我喜欢有多个表的想法,所以数据是分开的。 Mostly for many to many like data. 大多数情况下,对于许多喜欢的数据。 But my friend told me having 5 columns with boolean is just fine. 但是我的朋友告诉我,用布尔值创建5列就可以了。 But I like the idea of have a table with the settings and then a table between with user.id and setting.id. 但是我喜欢这样的想法:有一个带有设置的表,然后是一个位于user.id和setting.id之间的表。 But my question is also, does it have a impact on the query? 但是我的问题也是,它对查询有影响吗?

example: 例:

Users
- id
- Email
- SettingA
- SettingB
- SettingC

OR example: 或示例:

Uers
- id
- email
Users_Settings
- user_id
- setting_id
Settings
- id
- someSettingsValue

What woult be faster for Mysql to query the data? Mysql查询数据会更快吗? (retrieving settings for user) (为用户检索设置)

与单个表字段相比,仅2 r 3之间的连接将花费时间。

It's not about preferring single table with many columns or preferring multiple tables. 这与选择具有多个列的单个表或选择多个表无关。 It's about Normalization. 关于规范化。

According to your provided schema, if all users will always have three settings, ie Setting A, B and C, then it's better to create single table with these three columns for settings. 根据您提供的模式,如果所有用户将始终具有三个设置,即设置A,B和C,则最好使用包含这三列的设置来创建单个表。

users table:
    +----------+--------------+------+-----+---------+----------------+
    | Field    | Type         | Null | Key | Default | Extra          |
    +----------+--------------+------+-----+---------+----------------+
    | id       | int(11)      | NO   | PRI | NULL    | auto_increment |
    | Email    | varchar(128) | NO   |     | NULL    |                |
    | SettingA | tinyint(1)   | NO   |     | NULL    |                |
    | SettingB | tinyint(1)   | NO   |     | NULL    |                |
    | SettingC | tinyint(1)   | NO   |     | NULL    |                |
    +----------+--------------+------+-----+---------+----------------+

But if any of the setting is saved is null, then better is to create separate table for settings and then a pivot table for maintaining users' settings without primary key. 但是,如果保存的任何设置为null,则最好是为设置创建单独的表,然后再创建一个数据透视表以保持用户的设置而无需主键。

users table:
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | int(11)      | NO   | PRI | NULL    | auto_increment |
    | Email | varchar(128) | NO   |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+

settings table:
    +---------------+------------+------+-----+---------+----------------+
    | Field         | Type       | Null | Key | Default | Extra          |
    +---------------+------------+------+-----+---------+----------------+
    | id            | int(11)    | NO   | PRI | NULL    | auto_increment |
    | setting_value | tinyint(1) | NO   |     | NULL    |                |
    +---------------+------------+------+-----+---------+----------------+

setting_user pivot table:
    +------------+---------+------+-----+---------+-------+
    | Field      | Type    | Null | Key | Default | Extra |
    +------------+---------+------+-----+---------+-------+
    | setting_id | int(11) | NO   |     | NULL    |       |
    | user_id    | int(11) | NO   |     | NULL    |       |
    +------------+---------+------+-----+---------+-------+

Where setting_user is the pivot table. 其中setting_user是数据透视表。

One more thing is considered when creating schema, that, will there be always three settings, or will there be more in future, when application is expanded! 创建模式时,还要考虑一件事,即,在扩展应用程序时,总会有三个设置,或者将来会有更多设置!

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

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