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?
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
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.
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.
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.
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.
Should be fine, unless you have
select * from yourHugeTable all over the place. Always only select the columns you need.
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:
- Define your column as small as possible.
- 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:
- name – CHAR(70)
- gender – TINYINT(1)
- age – TINYINT(2)
- 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.
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”?
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.
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.