MySQL JOIN 在 1 个大表和多个小表上的性能

标签 mysql join

我打算建立一个巨大的数据库。我以前有一个客户,他的数据库超过 100M 行。假设我们有一个包含 100M 行的表 A 和多个包含 250 行的表。

我想知道通常哪种方法更快(我知道这取决于很多事情):

  1. 根据 ID 将小表连接到大表
  2. 在大表中包含小表值

例如:

第一个选项:

id  |   data1   |   data2   |   data3   |   table1_foreign_key  |   table2_foreign_key  |   table3_foreign_key
--------------------------------------------------------------------------------------------------------------
1   |   test    |   test    |   test    |   12                  |   34                  |   22
2   |   test    |   test    |   test    |   34                  |   67                  |   63
3   |   test    |   test    |   test    |   43                  |   34                  |   18
4   |   test    |   test    |   test    |   23                  |   21                  |   22
5   |   test    |   test    |   test    |   22                  |   34                  |   22
6   |   test    |   test    |   test    |   22                  |   34                  |   13
7   |   test    |   test    |   test    |   23                  |   54                  |   12
8   |   test    |   test    |   test    |   11                  |   57                  |   43
9   |   test    |   test    |   test    |   3                   |   34                  |   22

在这里,我将根据 ID 将所有这些小表连接到大表。例如,我会在这里存储城市、国家、设备等。

第二个选项:

id  |   data1   |   data2   |   data3   |   table1_foreign_key  |   table2_foreign_key  |   table3_foreign_key
--------------------------------------------------------------------------------------------------------------
1   |   test    |   test    |   test    |   Oklahoma            |   sample_text         |   sample_text
2   |   test    |   test    |   test    |   New York            |   sample_text         |   sample_text
3   |   test    |   test    |   test    |   New York            |   sample_text         |   sample_text
4   |   test    |   test    |   test    |   New York            |   sample_text         |   sample_text
5   |   test    |   test    |   test    |   Washington          |   sample_text         |   sample_text
6   |   test    |   test    |   test    |   Mitchigan           |   sample_text         |   sample_text
7   |   test    |   test    |   test    |   Oklahoma            |   sample_text         |   sample_text
8   |   test    |   test    |   test    |   Kansas              |   sample_text         |   sample_text
9   |   test    |   test    |   test    |   Dallas              |   sample_text         |   sample_text

在第二个选项中,没有 JOIN,但数据将包含在主大表中。每列的预期数据大小约为 2-20 个字符。


问题:

如果我们有相同的环境并且有适当的索引,上述哪个选项会更快?这里建议使用哪种方法? (我的客户想在这个数据库和表中存储点击和点击数据。)

最佳答案

由于是“一对多”关系,我会将它们存储在一个单独的表中。 SQL Server 查询优化器(在引擎盖下)将能够足够快地解析 250 条记录,这不应该是一个问题。此外,根据较小表中值的长度,您可以通过不将它们额外存储数亿次来节省存储空间。但是,如果报告性能最重要,您可以选择将它们存储在一个“扁平化”表中——就像数据仓库结构一样,没有连接。这肯定会更快,但您会牺牲存储空间和结构良好的关系数据库。

综上所述,我会选择选项 1。但是您应该能够使用选项 2 格式轻松地将数据存储在新表中 - 针对这两种格式进行查询 - 然后自行衡量性能。我希望这不会有太大区别,尤其是考虑到您的小 table 的容量。

关于MySQL JOIN 在 1 个大表和多个小表上的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45019188/

相关文章:

php - 根据我的查询,mysql 结果不显示

mysql - Laravel 中的分区查询

javascript - 在 Acumatica 中单击按钮更改表中的值

mysql - 与 phpmyadmin 相比,$wpdb->get_results 查询返回不同(错误)的结果

MySQL:带有 JOIN 的 SUM() 返回不正确的值

mysql - 从多个表中检索数据的 SQL 查询

sql - 我应该如何加入才能实现这一目标

python pandas,转换数据集,将行移动到列中

php - MySQL 性能规划

php - 按日期比较不同表中的行数