Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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

and 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. Any ideas on why some approach is better than other?

like image 745
PLane Avatar asked Jul 28 '11 12:07

PLane


People also ask

What's the better database design more tables or more columns?

The usual result of these rules is that the initial design will favor tables over columns, with a focus on eliminating redundancy.

What is the reason of having multiple table in databases why not a huge single table?

What is the reason of having Multiple table in databases,why not a huge single table? In relational databases this would take up too much resources to store not only data but also relations between them. It would also hamper data retrieval efficiency, especially in a multiuser environment.

Which database is best for large amount of data?

MongoDB is also considered to be the best database for large amounts of text and the best database for large data.

How many tables in a database is too many?

The number of tables is limited only by the number of database objects, currently 2, 147, 483, 647. A couple of hundred tables isn't going to make a difference to anything except the clarity of your data model.


3 Answers

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.

like image 197
meagar Avatar answered Sep 18 '22 16:09

meagar


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.

like image 23
Paddy Avatar answered Sep 22 '22 16:09

Paddy


It looks to me like each array of data has a distinct schema. Have you considered using a NoSQL database? 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. 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.

like image 44
MattBelanger Avatar answered Sep 19 '22 16:09

MattBelanger