mysql - how many columns is too many?

SqlMysql

Sql Problem Overview


I'm setting up a table that might have upwards of 70 columns. I'm now thinking about splitting it up as some of the data in the columns won't be needed every time the table is accessed. Then again, if I do this I'm left with having to use joins.

At what point, if any, is it considered too many columns?

Sql Solutions


Solution 1 - Sql

It's considered too many once it's above the https://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html">maximum limit supported by the database.

The fact that you don't need every column to be returned by every query is perfectly normal; that's why SELECT statement lets you explicitly name the columns you need.

As a general rule, your table structure should reflect your domain model; if you really do have 70 (100, what have you) attributes that belong to the same entity there's no reason to separate them into multiple tables.

Solution 2 - Sql

There are some benefits to splitting up the table into several with fewer columns, which is also called Vertical Partitioning. Here are a few:

  1. If you have tables with many rows, modifying the indexes can take a very long time, as MySQL needs to rebuild all of the indexes in the table. Having the indexes split over several table could make that faster.

  2. Depending on your queries and column types, MySQL could be writing temporary tables (used in more complex select queries) to disk. This is bad, as disk i/o can be a big bottle-neck. This occurs if you have binary data (text or blob) in the query.

  3. Wider table can lead to slower query performance.

Don't prematurely optimize, but in some cases, you can get improvements from narrower tables.

Solution 3 - Sql

It is too many when it violates the rules of normalization. It is pretty hard to get that many columns if you are normalizing your database. Design your database to model the problem, not around any artificial rules or ideas about optimizing for a specific db platform.

Apply the following rules to the wide table and you will likely have far fewer columns in a single table.

  1. No repeating elements or groups of elements
  2. No partial dependencies on a concatenated key
  3. No dependencies on non-key attributes

Here is a link to help you along.

Solution 4 - Sql

That's not a problem unless all attributes belong to the same entity and do not depend on each other. To make life easier you can have one text column with JSON array stored in it. Obviously, if you don't have a problem with getting all the attributes every time. Although this would entirely defeat the purpose of storing it in an RDBMS and would greatly complicate every database transaction. So its not recommended approach to be followed throughout the database.

Solution 5 - Sql

Having too many columns in the same table can cause huge problems in the replication as well. You should know that the changes that happen in the master will replicate to the slave.. for example, if you update one field in the table, the whole row will be w

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBradView Question on Stackoverflow
Solution 1 - SqlChssPly76View Answer on Stackoverflow
Solution 2 - SqljonstjohnView Answer on Stackoverflow
Solution 3 - SqlJohnFxView Answer on Stackoverflow
Solution 4 - SqlZeeshan ChView Answer on Stackoverflow
Solution 5 - SqlTodayView Answer on Stackoverflow