mson
mson

Reputation: 7822

schema design

Let's say you are a GM dba and you have to design around the GM models

Is it better to do this?

Or this?

Let's say that the business lines have the same columns for a model and that there are over a million records for each subtype.

EDIT:

EDIT:

Reasons for partitioning the structure into multiple tables - business lines may have different business rules regarding parts - addModelDetail() could be different for each business line (even though the data format is the same) - high add/update activity - better performance with partitioned structure instead of single structure (I'm guessing and not sure here)?

I think this is a variation of the EAV problem. When posed as a EAV design, the single table structure generally gets voted as a bad idea. When posed in this manner, the single table strucutre generally gets voted as a good idea. Interesting...

I think the most interesting answer is having two different structures - one for crud and one for reporting. I think I'll try concatenated/flattened view for reporting and multiple tables for crud and see how that works.

Upvotes: 0

Views: 795

Answers (11)

Jonathan Leffler
Jonathan Leffler

Reputation: 755044

@mson has asked the question "What do you do when a question is not satisfactorily answered on SO?", which is a direct reference to the existing answers to this question.

I contributed the following answer to that discussion, primarily critiquing the way the question was asked.


Quote (verbatim):

I looked at the original question yesterday, and decided not to contribute an answer.

One problem was the use of the term 'model' as in 'GM models' - which cited 'Chevrolet, Saturn, Cadillac' as 'models'. To my understanding, these are not models at all; they are 'brands', though there might also be an industry-insider term for them that I'm not familiar with, such as 'division'. A model would be a 'Saturn Vue' or 'Chevrolet Impala' or 'Cadillac Escalade'. Indeed, there could well be models at a more detailed level than that - different variants of the Saturn Vue, for example.

So, I didn't think that the starting point was well framed. I didn't critique it; it wasn't quite compelling enough, and there were answers coming in, so I let other people try it.

The next problem is that it is not clear what your DBMS is going to be storing as data. If you're storing a million records per 'model' ('brand'), then what sorts of data are you dealing with? Lurking in the background is a different scenario - the real scenario - and your question has used an analogy that failed to be sufficiently realistic. That means that the 'it depends' parts of the answer are far more voluminous than the 'this is how to do it' ones. There is just woefully too little background information on the data to be modelled to allow us to guess what might be best.

Ultimately, it will depend on what uses people have for the data. If the information is going to go flying off in all different directions (different data structures in different brands; different data structures at the car model levels; different structures for the different dealerships - the Chevrolet dealers are handled differently from the Saturn dealers and the Cadillac dealers), then the integrated structure provides limited benefit. If everything is the same all the way down, then the integrated structure provides a lot of benefit.

Are there legal reasons (or benefits) to segregating the data? To what extent are the different brands separate legal entities where shared records could be a liability? Are there privacy issues, such that it will be easier to control access to the data if the data for the separate brands is stored separately?

Without a lot more detail about the scenario being modelled, no-one can give a reliable general answer - at least, not more than the top-voted one already gives (or doesn't give).

  • Data modelling is not easy.
  • Data modelling without sufficient information is impossible to do reliably.

I have copied the material here since it is more directly relevant. I do think that to answer this question satisfactorily, a lot more context should be given. And it is possible that there needs to be enough extra context to make SO the wrong place to ask it. SO has its limitations, and one of those is that it cannot deal with questions which require long explanations.

From the SO FAQs page:

What kind of questions can I ask here?

Programming questions, of course! As long as your question is:

  • detailed and specific
  • written clearly and simply
  • of interest to at least one other programmer somewhere

...

What kind of questions should I not ask here?

Avoid asking questions that are subjective, argumentative, or require extended discussion. This is a place for questions that can be answered!

This question is, IMO, close to the 'require extended discussion' limit.

Upvotes: 0

Quibblesome
Quibblesome

Reputation: 25429

It depends on the datamodel and the use case. If you ever need to report on a query that wants data out of the "models" then the former is preferable because otherwise (with the latter) you'd have to change the query (to include the new table) every time you added a new model.

Oh and by "former" we mean this option:

table_model
* type {cadillac, saturn, chevrolet}

Upvotes: 0

Robert C. Barth
Robert C. Barth

Reputation: 23355

On data with a lot of writes, (e.g. an OLTP application), it is better to have more, narrower tables (e.g. tables with fewer fields). There will be less lock contention because you're only writing small amounts of data into different tables.

So, based on the criteria you have described, the table structure I would have is:

Vehicle
  VehicleType
  Other common fields

CadillacVehicle
  Fields specific to a Caddy

SaturnVehicle
  Fields specific to a Saturn

For reporting, I'd have an entirely different database on an entirely different server that does not have the normalized structure (e.g. just has CadillacVehicle and SaturnVehicle tables with all of the fields from the Vehicle table duplicated into them).

With proper indexes, even the OLTP database could be performant in your SELECT's, regardless of the fact that there are tens of millions of rows. However, since you mentioned that there are processor-intensive reports, that's why I would have a completely separate reporting database.

One last comment. About the business rules... the data store cares not about the business rules. If the business rules are different between models, that really shouldn't factor into your design decisions about the database schema (other than to help dictate which fields are nullable and their data types).

Upvotes: 3

Paul Mitchell
Paul Mitchell

Reputation: 3281

Definitely the former example. Do you want to be adding tables to your database whenever you add a new model to your product range?

Upvotes: 10

dtc
dtc

Reputation: 10296

I would say the first way looks better.

Are there reasons you would want to do it the second way?

The first way follows normalization better and is closer to how most relational database schema are developed.

The second way seems to be harder to maintain.

Unless there is a really good reason for doing it the second way I would go with the first method.

Upvotes: 1

yfeldblum
yfeldblum

Reputation: 65455

You could try having two separate databases.

One is an OLTP (OnLine Transaction Processing) system which should be highly normalized so that the data model is highly correct. Report performance must not be an issue, and you would deal with non-reporting query performance with indexes/denormalization etc. on a case-by-case basis. The data model should try to match up very closely with the conceptual model.

The other is a Reports system which should pull data from the OLTP system periodically, and massage and rearrange that data in a way that makes report-generation easier and more performant. The data model should not try to match up too closely with the conceptual model. You should be able to regenerate all the data in the reporting database at any time from the data currently in the main database.

Upvotes: 1

Oleg
Oleg

Reputation: 1100

Choice depends on required performance. The best database is normalized database. But there could be performance issues in normalized database then you have to denormalize it. Principle "Normalize first, denormalize for performance" works well.

Upvotes: 0

ConcernedOfTunbridgeWells
ConcernedOfTunbridgeWells

Reputation: 66702

Use the former. Setting up separate tables for the specialisations will complicate your code and doesn't bring any advantages that can't be achieved in other ways. It will also massively simplify your reports.

Upvotes: 2

Dave Markle
Dave Markle

Reputation: 97831

If the tables really do have the same columns, then the former is the best way to do it. Even if they had different columns, you'd probably still want to have the common columns be in their own table, and store a type designator.

Upvotes: 1

Onorio Catenacci
Onorio Catenacci

Reputation: 15343

Another thing to consider in defining "better"--will end users be querying this data directly? Highly normalized data is difficult for end-users to work with. Of course this can be overcome with views but it's still something to think about as you're finalizing your design.

I do agree with the other two folks who answered: which form is "better" is subjective and dependent on what you're hoping to achieve. If you're hoping to achieve very quick queries that's one thing. If you're hoping to achieve high programmer productivity--that's a different goal again and possibly conflicts with quick queries.

Upvotes: 0

Stevo
Stevo

Reputation: 462

Given the description that you have given us, the answer is either.

In other words you haven't given us enough information to give a decent answer. Please describe what kind of queries you expect to perform on the data.

[Having said that, I think the answer is going to be the first one ;-) As I imaging even though they are different models, the data for each model is probably going to be quite similar.

But this is a complete guess at the moment.]

Edit: Given your updated edit, I'd say the first one definitely. As they have all the same data then they should go into the same table.

Upvotes: 0

Related Questions