views:

2765

answers:

12

Hi all. The current project that I'm working on have table with 126 columns and the least that i saw is at least 50 columns. Should a table hold less columns per table or separate them out into a new table and use relationship?

In your experience what is the max columns per table? Does it affect the database with such design?

Jack

+3  A: 

Usually excess columns points to improper normalization, but it is hard to judge without having some more details about your requirements.

Otávio Décio
+1  A: 

It sounds like you have potential normalization issues.

If you really want to, you can create a new table for each of those columns (a little extreme) or group of related columns, and join it on the ID of each record.

Joe Philllips
+1  A: 

It could certainly affect performance if people are running around with a lot of "Select * from GiantTableWithManyColumns"...

Jason Punyon
+3  A: 

I can picture times when it might be necessary to have this many, or more columns. Examples would be if you had to denormalize and cache data - or for a type of row with many attributes. I think the keys are to avoid select * and make sure you are indexing the right columns and composites.

Scott Miller
A: 

A table should have as few columns as possible.....

in SQL server tables are stored on pages, 8 pages is an extent

in SQL server a page can hold about 8060 bytes, the more data you can fit on a page the less IOs you have to make to return the data

You probably want to normalize (AKA vertical partitioning) your database

SQLMenace
Normalization != vertical partitioning.
Bill Karwin
Remember I did not say Horizontal Partitioning!
SQLMenace
Normalization is NOT AKA vertical partitioning.
Roberto Russo
But it could be, the two types of vertical partitioning are normalization and row splitting
SQLMenace
You may randomly vertically divide your tables without leading to any normal form. Vertical partition is not an alias for Normalization, so your "AKA" is out of place.
Roberto Russo
A: 

The UserData table in SharePoint has 201 fields but is designed for a special purpose.
Normal tables should not be this wide in my opinion.

You could probably normalize some more. And read some posts on the web about table optimization.

It is hard to say without knowing a little bit more.

tyndall
+1  A: 

Here are the official statistics for SQL Server 2005 http://msdn.microsoft.com/en-us/library/ms143432.aspx

Keep in mind these are the maximums, and are not necessarily the best for usability.

Think about splitting the 126 columns into sections. For instance, if it is some sort of "person" table you could have

Person ID, AddressNum, AddressSt, AptNo, Province, Country, PostalCode, Telephone, CellPhone, Fax

But you could separate that into Person ID, AddressID, PhoneID

Address ID, AddressNum, AddressSt, AptNo, Province, Country, PostalCode

Phone ID, Telephone, Cellphone, fax

In the second one, you could also save yourself from data replication by having all the people with the same address have the same addressId instead of copying the same text over and over.

+8  A: 

Generally it's better to design your tables first to model the data requirements and to satisfy rules of normalization. Then worry about optimizations like how many pages it takes to store a row, etc.

I agree with other posters here that the large number of columns is a potential red flag that your table is not properly normalized. But it might be fine in this case. We can't tell from your description.

In any case, splitting the table up just because the large number of columns makes you uneasy is not the right remedy. Is this really causing any defects or performance bottleneck? You need to measure to be sure, not suppose.

Bill Karwin
+1  A: 

If you had an object detailing the data in the database, would you have a single object with 120 fields, or would you be looking through the data to extract data that is logically distinguishable? You can inline Address data with Customer data, but it makes sense to remove it and put it into an Addresses table, even if it keeps a 1:1 mapping with the Person.

Down the line you might need to have a record of their previous address, and by splitting it out you've removed one major problem refactoring your system.

Are any of the fields duplicated over multiple rows? I.e., are the customer's details replicated, one per invoice? In which case there should be one customer entry in the Customers table, and n entries in the Invoices table.

One place where you need to not fix broken normalisation is where you have a facts table (for auditing, etc) where the purpose is to aggregate data to run analyses on. These tables are usually populated from the properly normalised tables however (overnight for example).

JeeBee
+2  A: 

A good rule of thumb that I've found is simply whether or not a table is growing rows as a project continues,

For instance:

On a project I'm working on, the original designers decided to include site permissions as columns in the user table.

So now, we are constantly adding more columns as new features are implemented on the site. obviously this is not optimal. A better solution would be to have a table containing permissions and a join table between users and permissions to assign them.

However, for other more archival information, or tables that simply don't have to grow or need to be cached/minimize pages/can be filtered effectively, having a large table doesn't hurt too much as long as it doesn't hamper maintenance of the project.

At least that is my opinion.

Chris J
A: 

Well, I don't know how many columns are possible in sql but one thing for which I am very sure is that when you design table, each table is an entity means that each table should contain information either about a person, a place, an event or an object. So till in my life I don't know that a thing may have that much data/information.

Second thing that you should notice is that that there is a method called normalization which is basically used to divide data/information into sub section so that one can easily maintain database. I think this will clear your idea.

A: 

I'm in a similar position. Yes, there truly is a situation where a normalized table has, like in my case, about 90, columns: a work flow application that tracks many states that a case can have in addition to variable attributes to each state. So as each case (represented by the record) progresses, eventually all columns are filled in for that case. Now in my situation there are 3 logical groupings (15 cols + 10 cols + 65 cols). So do I keep it in one table (index is CaseID), or do I split into 3 tables connected by one-to-one relationship?