mysql - 将小型静态表合并到数据库中是一种好习惯吗?

标签 mysql database django

我正在开发一个数据库来存储测试数据。每条数据有 11 个元数据标签。目前我为每个元数据选项都有一个单独的表。我在这里看到了一些关于许多小表的最佳实践的问题,但我想我应该为我自己的项目提出这个问题,因为我没有从其他问题中得到明确的答案。

这是我的表列表,每个表中的字段:

Source Type - id, name, description
For Flight - id, name, description
Site - id, name, abrv, description
Stand - id, site (FK site table), name, abrv, descrition
Sensor Type - id, name, channels, descrition
Vehicle - id, name, abrv, descrition
Zone - id, vehicle (FK vehicle table), name, abrv, description
Event Type - id, name, description
Event - id, event type (FK to event type Table), name, descrition
Analysis - id, name, descrition
Bandwidth - id, name, descrition

您可以看到每个表中的字段大致相同。三个表引用了另一个表。

如果只有一个名为 Meta 的大表并包含以下字段会更好吗:

Meta: id, metavalue, name, abrv, FK, value, descrition

其中 metavalue = 上述表名之一
和 FK = 对 Meta 表中另一行的引用以代替外键?

我是数据库的新手,多张表似乎最直观,但一张表使编程更容易。

所以问题是:

  1. 减少表的数量并将所有静态值放在一个表中是否是一种好的做法。
  2. 有一个自引用表是不是很糟糕。

仅供引用,我在 NTFS 格式的 Windows 服务器上使用 django 和 mysql 制作这个网络数据库。

感谢提示和最佳实践。

谢谢。

最佳答案

“只有一张大 table 会更好吗”- 强调和断然,不!

这种反模式有时被称为“一张表统治一切”!

Ten Common Database Design Mistakes : 一个表来保存所有域值。

  • Using the data in a query is much easier

  • Data can be validated using foreign key constraints very naturally, something not feasible for the other solution unless you implement ranges of keys for every table – a terrible mess to maintain.

  • If it turns out that you need to keep more information about a ShipViaCarrier than just the code, 'UPS', and description, 'United Parcel Service', then it is as simple as adding a column or two. You could even expand the table to be a full blown representation of the businesses that are carriers for the item.

  • All of the smaller domain tables will fit on a single page of disk. This ensures a single read (and likely a single page in cache). If the other case, you might have your domain table spread across many pages, unless you cluster on the referring table name, which then could cause it to be more costly to use a non-clustered index if you have many values.

  • You can still have one editor for all rows, as most domain tables will likely have the same base structure/usage. And while you would lose the ability to query all domain values in one query easily, why would you want to? (A union query could easily be created of the tables easily if needed, but this would seem an unlikely need.)

关于mysql - 将小型静态表合并到数据库中是一种好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6052853/

相关文章:

java - 将纬度和经度( double )数据转换为字节数组,以便将其作为 java 中的 Geometry 或 GeometryCollection 值存储在 mySql 中

c# - 在 xamarin forms pcl 上执行查询时没有这样的表 sqlite

jquery - 如何使用 DJANGO REST 框架和 ajax 发出 POST 请求

php - 连接失败: Host 'xx.xx.xx.xx' is not allowed to connect to this MySQL server - AJAX and AWS

php - 将所选 radio 的值传递到下一页

php - 种子 Silverstripe 数据库

java - 连接池查询结果在实时数据和旧数据之间跳转?

python - 使用 SelectDateWidget 作为表单

python - 如何在 django-tables2 中调用非模型字段

mysql - 如何从 MySQL 表中获取同一 ID 的最新时间戳记录?