42

Is there a performance cost to having large numbers of columns in a table, aside from the increase in the total amount of data? If so, would splitting the table into a few smaller ones help the situation?

divibisan
  • 11,659
  • 11
  • 40
  • 58
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

9 Answers9

31

I don't agree with all these posts saying 30 columns smells like bad code. If you've never worked on a system that had an entity that had 30+ legitimate attributes, then you probably don't have much experience.

The answer provided by HLGEM is actually the best one of the bunch. I particularly like his question of "is there a natural split....frequently used vs. not frequently used" are very good questions to ask yourself, and you may be able to break up the table in a natural way (if things get out of hand).

My comment would be, if your performance is currently acceptable, don't look to reinvent a solution unless you need it.

Wade
  • 319
  • 3
  • 2
  • 1
    Everyone is entitled to have his own opinion. To debase someone because he shares an opinion, that's found in about each book just doesn't seem justified. I've worked on many systems and each and every one of them had tables with more than 30 columns but the smell remains. Just because it's there and in production doesn't make it right. – Nicktar Jul 28 '11 at 13:29
  • 1
    Right, I'm working on an ERP developed by oracle having 50+ columns in their most used table. – Muhammad Saqib Sep 04 '19 at 15:50
29

I'm going to weigh in on this even though you've already selected an answer. Yes, tables that are too wide could cause performance problems (and data problems as well) and should be separated out into tables with one-one relationships. This is due to how the database stores the data (well at least in SQL Server not sure about MySQL but it is worth doing some reading in the documentation about how the database stores and accesses the data).

Thirty columns might be too wide and might not, it depends on how wide the columns are. If you add up the total number of bytes that your 30 columns will take up, is it wider than the maximum number of bytes that can be stored in a record?

Are some of the columns ones you will need less often than others (in other words is there a natural split between required and frequently used info and other stuff that may appear in only one place not everywhere else), then consider splitting up the table.

If some of your columns are things like phone1, phone2, phone3 - then it doesn't matter how many columns you have you need a related table with a one-to-many relationship instead.

In general, though 30 columns are not unusually big and will probably be OK.

Amirmasoud
  • 590
  • 4
  • 11
  • 27
HLGEM
  • 94,695
  • 15
  • 113
  • 186
22

If you really need all those columns (that is, it's not just a sign that you have a poorly designed table) then by all means keep them.

It's not a performance problem, as long as you

  • use appropriate indexes on columns you need to use to select rows
  • don't retrieve columns you don't need in SELECT operations

If you have 30, or even 200 columns it's no problem to the database. You're just making it work a little harder if you want to retrieve all those columns at once.

But having a lot of columns is a bad code smell; I can't think of any legitimate reason a well-designed table would have this many columns and you may instead be needing a one-many relationship with some other, much simpler, table.

thomasrutter
  • 114,488
  • 30
  • 148
  • 167
  • 1
    I see one reason that I consider legitimate: when loading a legacy or proprietary (indexed or csv) file in a table to leverage the database power in order to exploit it. – snowflake Aug 13 '10 at 08:29
  • 3
    @snowflake: That's how these things happen, but the bad code smell remains and the data/schema should be examined for potential refactorings. – Donal Fellows Aug 13 '10 at 11:51
  • 3
    I don't understand what 'bad smell' or 'poorly designed' means exept a subjective opinion.... please explain – TOPKAT Jun 16 '16 at 16:36
  • 1
    There is indeed some subjectivity in these terms. "Bad smell" refers to a sign in some code that your application may be poorly designed. It doesn't necessarily mean it is, but somebody else reading your code is likely to jump to that conclusion. Poorly designed means not coding something in a sensible or efficient way, using tools how they weren't intended to be used, etc. In this case it may indicate that you need to reconsider how to normalise the database design. – thomasrutter Jun 17 '16 at 00:36
9

Technically speaking, 30 columns is absolutely fine. However, tables with many columns are often a sign that your database isn't properly normalized, that is, it can contain redundant and / or inconsistent data.

tdammers
  • 20,353
  • 1
  • 39
  • 56
6

30 doesn't seem too many to me. In addition to necessary indexes and proper SELECT queries, for wide tables, 2 basic tips apply well:

  1. Define your column as small as possible.
  2. Avoid using dynamic columns such as VARCHAR or TEXT as much as possible when you have large number of columns per table. Try using fixed length columns such as CHAR. This is to trade off disk storage for performance.

For instance, for columns 'name', 'gender', 'age', 'bio' in 'person' table with as many as 100 or even more columns, to maximize performance, they are best to be defined as:

  1. name - CHAR(70)
  2. gender - TINYINT(1)
  3. age - TINYINT(2)
  4. bio - TEXT

The idea is to define columns as small as possible and in fixed length where reasonably possible. Dynamic columns should be to the end of the table structure so fixed length columns are ALL before them.

It goes without saying this would introduce tremendous disk storage wasted with large amount of rows, but as you want performance I guess that would be the cost.

Another tip is as you go along you would find columns that are much more frequently used (selected or updated) than the others, you should separate them into another table to form a one to one relationship to the other table that contains infrequent used columns and perform the queries with less columns involved.

Community
  • 1
  • 1
datasn.io
  • 12,564
  • 28
  • 113
  • 154
5

Should be fine, unless you have select * from yourHugeTable all over the place. Always only select the columns you need.

Vincent Buck
  • 16,462
  • 2
  • 21
  • 21
3

Usage wise, it's appropriate in some situations, for example where tables serve more than one application that share some columns but not others, and where reporting requires a real-time single data pool for all, no data transitions. If a 200 column table enables that analytic power and flexibility, then I'd say "go long." Of course in most situations normalization offers efficiency and is best practice, but do what works for your need.

BarryDevSF
  • 395
  • 3
  • 12
  • "for example where tables serve more than one application that share some columns but not others". Bingo! That's exactly my case when I searched for this question. Great that you point this out. One user database is shared by two applications, then yes, add the columns that are needed by both to it. Otherwise you end up taking care of the same columns in two separate tables and that gets messy. – Avatar Apr 20 '23 at 16:11
3

30 columns would not normally be considered an excessive number.

Three thousand columns, on the other hand... How would you implement a very wide "table"?

Community
  • 1
  • 1
2

Beyond performance, DataBase normalization is a need for databases with too many tables and relations. Normalization gives you easy access to your models and flexible relations to execute diffrent sql queries.

As it is shown in here, there are eight forms of normalization. But for many systems, applying first, second and third normal forms is enough.

So, instead of selecting related columns and write long sql queries, a good normalized database tables would be better.

Mp0int
  • 18,172
  • 15
  • 83
  • 114
  • I read such docs long ago, and i know about them... But as i said, mostly used normalization forms are first three. The rest is not commonly used. My porpose was show some general info about normalization. And yes it tells about 8, but its really hard to find information about normalization beyond 5th normal form and BCNF and DKNF. But you are right (: – Mp0int Aug 13 '10 at 11:15
  • @mp0int - if you edit your answer, I can remove the downvote - it's currently locked in. –  Aug 13 '10 at 11:50