简体   繁体   English

数据库设计:很多行与很多表?

[英]Database design: lots of rows vs lots of tables?

I'm doing this database design stuff for a system where i need to store some variable length arrays into mysql database. 我正在为一个系统做这个数据库设计的东西,我需要将一些可变长度数组存储到mysql数据库中。

The length of the arrays will be (at most) in hundreds if not thousands. 阵列的长度(最多)为数百(如果不是数千)。

New arrays will be created on a regular basis, maybe tens daily. 将定期创建新阵列,可能每天数十个。

  1. should I store these arrays into one table that will soon grow gigantic or 我应该将这些数组存储在一个很快会变得巨大的表中
  2. create a new table for each array and soon have a huge number or tables? 为每个数组创建一个新表,很快就有一个庞大的数字或表?
  3. something else? 别的什么? (like formatted text column for the array values) (就像数组值的格式化文本列一样)

to clarify, 1. means roughly 澄清,1。粗略表示

CREATE TABLE array (id INT, valuetype VARCHAR(64), ...)
CREATE TABLE arr_values (id INT, val DOUBLE, FK array_id)

and 2. 和2。

CREATE TABLE array (id INT, valuetype VARCHAR(64),...)
CREATE TABLE arr_values (id int, val DOUBLE, FK array_id) -- template table
CREATE TABLE arr1_values LIKE arr_values ...

The arr_values will be used as arrays that is queried by joining to a complete array. arr_values将用作通过连接到完整数组来查询的数组。 Any ideas on why some approach is better than other? 关于为什么某些方法比其他方法更好的任何想法?

Lots of rows in few tables. 几张桌子中有很多行。 Making a new table for each new structure/record is absolutely incorrect and the very worst way to use a relational database. 为每个新结构/记录创建一个新表是绝对不正确的,也是使用关系数据库的最糟糕的方法。

In fact, almost any time your code is dynamically creating tables, you are doing something terribly, terribly wrong. 实际上,几乎在你的代码动态创建表的任何时候,你都在做一些非常糟糕的事情。

As with all answers to these sort of questions, it always depends somewhat on what your end result needs to be, but personally, I would always favour a single table over dynamically created tables - it makes for much simpler querying. 正如对这些问题的所有答案一样,它总是在某种程度上取决于您的最终结果需要是什么,但就个人而言,我总是倾向于使用单个表而不是动态创建的表 - 这使得查询更加简单。 It also makes it somewhat simpler (I think) when you look at the database schema - many thousands of tables may make finding what you need when accessing the database directly a bit easier. 当你查看数据库模式时,它也使得它更简单(我认为) - 成千上万的表可能使得在直接访问数据库时更容易找到所需的内容。

Additionally, if you find you need to extend your 'array' at some point with another field, it means that there will be a single database table to alter, rather than many. 此外,如果您发现需要在某个位置使用另一个字段扩展“数组”,则意味着将有一个数据库表要更改,而不是很多。

It looks to me like each array of data has a distinct schema. 在我看来,每个数据数组都有一个不同的模式。 Have you considered using a NoSQL database? 您是否考虑过使用NoSQL数据库? It will be much easier to work with, in my opinion. 在我看来,这将更容易合作。

If you must stick with MySQL, then you definitely want as few tables as possible. 如果你必须坚持使用MySQL,那么你肯定希望尽可能少的表。 Based on what you've presented, you could have one table with three columns - 根据您提供的内容,您可以拥有一个包含三列的表格 -

array ;connects all the related records to the correct array
field ;the name of the field (array key)
value ;the actual value for that field

And, if you need multiple copies of the same array "type", add an instance column as well. 而且,如果您需要同一数组“type”的多个副本,请同时添加一个实例列。

Increase the number of tables until the schema is normalized (has very little or no redundancy/duplication). 增加表的数量,直到模式规范化(很少或没有冗余/重复)。 Don't increase the number of tables beyond that and be cautious about adding a new table for performance (modern databases, with a few exceptions, are faster than you think), or to deal with edge cases, such as duplication that occurs once per the life of the application, or for <1% of the rows. 不要增加表之外的数量,并且要谨慎添加新的性能表(现代数据库,除少数例外,比你想象的要快),或处理边缘情况,例如每次发生一次的重复应用程序的生命周期,或<1%的行。

It also would be easier to understand your question if we knew the domain of the question-- are these addresses, customers, books, or what? 如果我们知道问题的范围 - 这些地址,客户,书籍还是什么,那么理解您的问题也会更容易?

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

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