Database programming question.

Status
Not open for further replies.

klinzhai

Ars Tribunus Militum
1,889
I've been shanghaid by the company I work for to write a database for a client. I basically thought it would be little problem, but then a kicker came in that I'm not sure how to deal with.

I'll try to explain it as simply as possible.

Each user has a unique ID, I was using this ID to simply keep track of their data within the various databases and tables, but I'm starting to get a little worried that the data will actually start to get excessively large. I'm wondering how I should deal with this. Each user could potentially have data in over 500 non duplicating fields. Several fields will be related and I was simply trying to keep the user data separate by the user ID and have several tables for related data, ending with between a dozen fields per table or so up to closer to about 50 fields. I'm worried that this will actually slow down the access for the database. Is there a problem with making tables with upwards of 50 to potentially 80 fields if the data is commonly related (and thus usually accessed at the same time) or should I try to keep it down below 30?

Everything I've been doing with databases up until now hasn't needed more than 25 fields for the mostpart, so it hasn't been an issue. This needs to be a very fast db, so does anybody have a good idea on how I should deal with this?
 

slok

Ars Scholae Palatinae
619
without having more details of what you are designing, it is really a
stab in the dark to try to answer whether a table with more than 50 fields is an issue.

other factors that could affect performance
- the choice of database
- number of concurrent users
- etc...


all I can say is sometimes, people tradeoff normalisation and lump certain data together in one table to speed up performance.
Again, this is subjective. There are others who will design very
well normalised tables and do performance tuning from there.
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
I agree with Slok, it sounds like(from the limited information you've listed) that maybe you might consider building your database structure not based on normalization and relationships, but what is being done with the data. i.e. if you have a frequently executed query that's pulling back a certain set of data, it may be better to lump all that data into one table(if that is possible) even if it doesn't fall with in the normalization and relationship ideas.

Normally joining isn't a particularly intensive operation since it's so common, but it is one more operation. Given a large enough data set and query it will matter.

It's very situational, but be aware if you optimize your layout in that manner, you are trading off performance for future flexibility by building in rigidity. In effect, you are pre-optimizing for certain operations. If something changes, you'll likely have a bear of a time adjusting to it.

Oh, but to answer some parts more directly! Most modern databases shouldn't have any problems dealing with a table that large. It's just that most situations people will tell you that if you are building a table that large, you need to break your data up into more logical groups. The idea is if you put 50 fields in one table, do you ever have an operation(other then an insert - basically do you select on it) that is using anywhere near all of those fields at one time. If you don't, you'll be better off chunking the fields into smaller tables, and only querying those fields through relationships when needed. It might make the individual query perform marginally slower because of the join operation, but it will make overall performance better. The performance difference if built right should be very very minimal, modern db's are highly optimized for join ops. It'll also make managing it(something which is frequently overlooked) much much easier. Tracking a problem down when it could be one of 50 fields is much harder then when it could only be one of 10.

We're also assuming you are using a real dbms(SQL Server, Oracle, DB2 etc) not a desktop db like Access to pull this off...


-J
 

reboot

Ars Legatus Legionis
10,818
Each user could potentially have data in over 500 non duplicating fields.

first of all, I would question DB design here. 500 fields is a lot and quite uncommon.
what is this information being stored about users ??

largest number of potential column I've is 3500 which were test results on chips (3500 different possible tests) but since it was a sparse matrix, it was eventually reorganized into something much narrower.

See if there's another way to organize this data.

Is there a problem with making tables with upwards of 50 to potentially 80 fields if the data is commonly related

This is not a problem at all, provided you're using a *real* D/B...
that means forget access.
SQL Server or preferably DB2 or Oracle.
I'm listing commercial solutions because this is what I know but there could be some free solution that would work too depending on the size of the D/B.

How many records do you expect to be in that user table ?
Couple thousands ? you need not be concerned about performance.
More than that ? You shouldn't tackle this by yourself, we're talking DBA work then.
 

klinzhai

Ars Tribunus Militum
1,889
Let's see if I can help this out a little...

Let's say I have a table with 100 fields (not really out of the question), field b will be related to all other fields, field c will be related to b and maybe 12 others.

It's basically a tiered system where there will be several fields that may rarely be accessed, but when they are, they are accessed in relation to 1-5 others that would also be in the table. I was considering breaking it into groupings of data that would guaranteed go together, but if it's not really a performance issue of putting upwards of 250-300 fields in the same table, then I'll go ahead and do that, it would make more sense for the actual database.

There should only be one table that will require more than 75 fields organizing the data the way I'm thinking. I guess I'm just used to trying to keep the tables smaller for easy management and tracking. There is one small problem though that reboot stated and that's the number of records. I'm expecting it to be upwards of potentially 10-15 thousand records. I'm starting to think of ways to speed it up, but will it really be an issue where the users would see a pause of more than a second or two while data is being searched for, particularly if I have good key usage?

I'm using FreeBSD and MySQL right now for the initial development, but plan on going to full BSD and converting the db from MySQL over to Oracle when I get close to done with the database.

We might create a master db server that is centrally located and then use site servers to hold local data when a user logs in at a location to keep their info and occasionally updates the master server maybe every 3-10 minutes or so and when the user logs off.

I have a feeling that we're going to end up getting at least one DBA to handle this project in the end, but would prefer to avoid hearing him say "what moron designed this piece of crap?" when he sees the db structure that I built.

Any more suggestions? Thanks for those you've already given also, they've helped a lot.
 

TheJet

Ars Praefectus
4,598
Subscriptor++
I'm expecting it to be upwards of potentially 10-15 thousand records

Any reasonable database will handle this many records. You'll only need to start worrying about this when your tables get into the 100K-500K range. Anything less than 100K shouldn't cause significant performance problems unless the design is completely braindead (which is rather hard to do with today's query optimizers).

I have a feeling that we're going to end up getting at least one DBA to handle this project in the end, but would prefer to avoid hearing him say "what moron designed this piece of crap?" when he sees the db structure that I built.

Sadly, you'll never avoid this. There's always going to be some way in which a DBA can decide that your design is crap. It all depends on the DBA, if they are normal, or willing to ask you why the design decision was made, then you won't have a problem, and it may be a learning experience for the both of you. If you're DBA is like some I've dealt with, you could have produced a design that could provide world peace and they'd still complain that it wasn't normalized to the n+1'th degree. ;)

TheJet

P.S. Foreign keys can be your friend, but keep in mind that they also cause performance degradation on inserts and updates. If/When you do bulk inserts/updates of known-good data (or even suspected good data), I would suggest turning them off until the inserts/updates are complete. It may actually be faster to insert the bad data, then delete the ones which don't satisfy your key contraints.
 

reboot

Ars Legatus Legionis
10,818
Let's say I have a table with 100 fields (not really out of the question), field b will be related to all other fields, field c will be related to b and maybe 12 others.

then if I understood correctly, those 12 fields should go in a different table, not in the same one.

I'm expecting it to be upwards of potentially 10-15 thousand records.

that is nothing for any real D/B on an half-decent server.
index creation should be minimal and straight-forward for instant results.

Why develop on MySQL if you plan to go to Oracle ?
 

klinzhai

Ars Tribunus Militum
1,889
You'll only need to start worrying about this when your tables get into the 100K-500K range.

Great... now I know that it won't be a problem I can relax about that for now.

those 12 fields should go in a different table, not in the same one.

OK, if I'm understanding you correctly then I'm going to end up with a very large number of tables.

Example:

Field A is related to fields A1-A12, field A1 is related to A1a-A1h, field A1a is related to fields A1a_1-A1a_15. It's not quite that bad, but it can be in some situations. Are you saying that A-M should be in one table, A1-A12 in another, A1a-A1h in another and A1a_1-A1a_15 in yet another? Would this potentially cause performance issues when I'm looking at say, 3000 records in table A-M, 1200 in table A1-A12, another 350 in table A1a-A1h and around 60 in table A1a_1-A1a_15? If it would actually increase performance in the long run then I'd be more than happy to stick with that format, as it was kind of what I was looking at already, I think that I'd potentially end up with a lot of redundant data fields though. Then again, keeping the actual data size down isn't all that important, keeping it fast is.

Why develop on MySQL if you plan to go to Oracle?

I haven't worked with Oracle before yet and have most of my experience on MySQL. I figured that I'd design it on MySQL, where I know it better and then go to Oracle when the basic design is solid, so if I screw up something in Oracle I still have something to fall back on. Maybe I should just run out and get Oracle, but I'm not really sure if my boss will pony up the cash for it. I'll look around and see if I can find some good pricing for it. Maybe I can talk him into picking up a copy for dev purposes.
 

TheJet

Ars Praefectus
4,598
Subscriptor++
OK, if I'm understanding you correctly then I'm going to end up with a very large number of tables.

Example:

Field A is related to fields A1-A12, field A1 is related to A1a-A1h, field A1a is related to fields A1a_1-A1a_15. It's not quite that bad, but it can be in some situations. Are you saying that A-M should be in one table, A1-A12 in another, A1a-A1h in another and A1a_1-A1a_15 in yet another? Would this potentially cause performance issues when I'm looking at say, 3000 records in table A-M, 1200 in table A1-A12, another 350 in table A1a-A1h and around 60 in table A1a_1-A1a_15? If it would actually increase performance in the long run then I'd be more than happy to stick with that format, as it was kind of what I was looking at already, I think that I'd potentially end up with a lot of redundant data fields though. Then again, keeping the actual data size down isn't all that important, keeping it fast is.

Yes, that would probably be the best way to do it. As long as you ensure that the foreign-key style relationships (enforced or not) are known and assigned ahead of time (integer types are best for this) you're performance won't suffer much for the joins. The biggest question is then going to be:

If A1 has a value of "1" for user X and user Y, then will all the subsequent values of A1a-A1h be the same for users X and Y? If this isn't true, then splitting the tables like that won't be terribly useful (and may hurt you in the long run), except to minimize the amount of extra (empty) fields you're storing.

Another thing to keep in mind is that some DBMS's have maximum row lengths (I know that SQL Server's max is something like 8060 bytes), so if you try to store data rows with a total used length of more than that, it will puke. So keep that in mind. When designing these things, you sometimes have to take into account the hardset limitations of the DBMS that you're running.

TheJet
 

klinzhai

Ars Tribunus Militum
1,889
If A1 has a value of "1" for user X and user Y, then will all the subsequent values of A1a-A1h be the same for users X and Y? If this isn't true, then splitting the tables like that won't be terribly useful (and may hurt you in the long run), except to minimize the amount of extra (empty) fields you're storing.

All of the entries that I'm talking about are going to be numerical and an example would be as follows:

User X:
A = 950
A1 = 439
A2 = 148
A1a = 210
A1b = 135
A1c = [empty]
A1d = 42

There will be several tables that users won't even have entries in and if they do, sometimes the user data will be minimal in that particular table. The data won't be consistent from user to user and no two users should end up with the same data except by fluke.

It would vastly save space on storing empty data to split the fields but I'm more concerned about speed in the long run. The mandate that came down was "It can never be slow".


Oh yeah, got on Oracle's site and it would appear that they have copies for download but I'm wondering if these are timebombed versions or just a bit hamstrung. If it's just hamstrung to perform like crap, I can live with that if it means that I can do the actual development on Oracle instead of trying to import it later. I do have some time on this project so I can learn about Oracle as I do it without too much in the way of time constraints to hinder me.

Has anyone worked with the Oracle free download version?
 

TheJet

Ars Praefectus
4,598
Subscriptor++
And SQL server can have as many columns as it wants, so long as they don't exceed 8060 maximum byte length (i.e. 8054 bit fields, and an integer field for keying) ;)

Incidentally you can have more data than that per row, you just need to store it in text/binary fields (which only take up 16 bytes). You can create rows which are longer than the 8060 limit (i.e. many varchar type columns), just if you try to put in more than 8060 bytes worth of data, an error is thrown.

TheJet
 

klinzhai

Ars Tribunus Militum
1,889
The more I think about it the more tiering the db makes sense. Even though it will mean a lot more tables, it will mean less data per table, right?

The 'main' tables (I think that there will be 3 or 4 of them) will have data for every user... things like fname, lname, location, etc... while the other tables will be more specialized, specific data that not all users will use. Most users won't use the majority of the tables, so those tables will be smaller, maybe 300 rows of data.

Sorry for all the noob questions, but I haven't gotten into large dbs before and speed due to size was never a real issue for them.
 

reboot

Ars Legatus Legionis
10,818
without insulting you intelligence, what is your experience / formation with design / modeling ?

I would suggest reading a book on D/B design and/or data modeling first.

this job sounds like you need a solid model, you shouldn't just throw everything together.

as a general rule, very wide tables are rare and usually indicate a bad design.

In particular, there's only so much I can think of for user related data, it should really be narrow.
(name, adress, department_no, salary, etc...)

Can you give examples of what you're considering putting into a user table ?
 

klinzhai

Ars Tribunus Militum
1,889
without insulting you intelligence, what is your experience / formation with design / modeling?

Don't worry, I wouldn't consider it an insult and to be honest, my experience is minimal. I'm well organized and from what I know that's easily half the battle. I think that I've decided to go after the several tables model instead of one rediculously large table. I thought that it would be a mess that would be at best difficult to track, at worst a nightmare that is nearly impossible to get out of without several weeks preparation.

I would suggest reading a book on D/B design and/or data modeling first.

I've got a couple that I use for reference. I probably need to start getting deeper into the innards of the books though.

In particular, there's only so much I can think of for user related data, it should really be narrow.
(name, adress, department_no, salary, etc...)

Can you give examples of what you're considering putting into a user table ?

It's going to be a custom invoicing/mailing list/purchase tracking, etc... program. I guess they tried to get it on the market but they couldn't find anything that did everything that they wanted it to do and getting something modified so that it would do all they wanted would have cost as much, if not more than what we're supposed to be charging them. I think it's overkill, but at this stage of their planning they're not really open to input, so I'm more than happy to burn their money while learning as much as possible and trying to get the job done right (or near close as possible) the first time.
 
Status
Not open for further replies.