Professional Documents
Culture Documents
Data Normalization Denormalization Techniques
Data Normalization Denormalization Techniques
Data Normalization Denormalization Techniques
(Term Paper)
Term Paper
1. 2. 3.
Abstract ........................................................................................................................................... 3 What is Normalization?.................................................................................................................... 3 Normalization Form ......................................................................................................................... 6 3.1. 3.2. 3.3. 3.4. 3.5. 3.6. First Normalization ................................................................................................................... 6 Second Normal Form................................................................................................................ 7 Third Normal Form ................................................................................................................... 9 Forth Normal Form ................................................................................................................ 10 Fifth Normal Form .................................................................................................................. 12 Sixth Normal Form ................................................................................................................. 16
4. 5. 6. 1. 2. 3. 7. 1. 2. 3. 8. 9. 10.
Denormalization ............................................................................................................................ 16 Deciding How Far to Normalize ...................................................................................................... 16 Should I Denormalize? ................................................................................................................... 17 Materialized Views..................................................................................................................... 18 Database Constraints ................................................................................................................. 18 Double Your Database Fun ......................................................................................................... 19 Denormalization Tactics ................................................................................................................. 20 Pre-Joined Tables ....................................................................................................................... 20 Mirror Tables ............................................................................................................................. 20 Split Tables ................................................................................................................................ 21 Denormalization in Hyperspace ..................................................................................................... 21 Conclusion ..................................................................................................................................... 21 Reference .................................................................................................................................. 22
Page 2 of 22
Term Paper
1. Abstract
The normalization and denormalization are the most common search terms bringing people to search in site. Typing normali into Google brings up suggestions like normalization and normalizing data high on the list. If you actually search for normalization, your top search results include Wikipedia, overviews, tutorials, and basics. All the bases are covered
I have chosen this topic because this is one of the areas many of these overviews just
skim the surface, never explaining why anyone would bother doing this. Some use examples which illustrate one principle while violating others, leading to confusion. Many use precisely the same examples for the higher forms, reworded slightly from each other, which might lead a more cynical person to wonder how well the writers grasp what they describe. If we cant create our own examples, do we really understand the principle? This paper covers the basics in a manner which I hope is both accurate and easy to understand, and also slithers into intermediate territory. We need at least an intermediate level of knowledge before we can decide how far to normalize, the possible efficiency tradeoffs, if denormalization is needed, and if it is, what denormalization methods to use.
2. What is Normalization?
Normalization just means making something more normal, which usually means bringing it closer to conformity with a given standard. Database normalization doesnt mean that you have have weird data, although you might. Its the name for an approach for reducing redundant data in databases. If the same data is stored in more than one place, keeping it synchronized is a pain. Furthermore, if you cant trust your synchronization process absolutely, you cant trust the data you retrieve. You may have heard that normalization is the enemy of search efficiency. This is kind of truism, but not really true. At the lower levels, imposing a bit of order on your chaotic heap of data will probably improve efficiency. At the higher levels you may see a performanceslowdown, but it probably wont be as bad as you fear and there are ways to address any problems you do find.
Page 3 of 22
Term Paper
Lets get to work on a data set. I am owning company which compares different products price from different sellers and sells widgets. We must track widget inventory, prices, order numbers, blah blah blah . . . . and more, Im falling tired just writing about it. You know what? You look trustworthy. Im going to tell you the sinister truth. Ive been retained by an organization called the pricesbolo.com, which stands alone against the forces ofevil and darkness which daily threaten to engulf our innocuous, daylit world. The Council of Light wants me to put their data into a SQL database so that they can retrieve it more easily. They have lists of monsters, types of weapons, historical records of monster outbreaks, a registry of currently available monster fighters, and a great deal more. Unfortunately, its all stored in a combinat ion of dusty tomes and piles of index cards. I must design a database which models this information accurately and usefully. Lets design in. A table of active monster fighters seems fairly obvious full name, date of birth, formal training, date of joining the Council, fighting skills, and whatever else we come up with
LAST NAME
FIRST NAME
DATE OF BIRTH
TRAINING
FIGHTING SKILLS blades, distance weapons, firearms, martial arts, munitions, stakes blades, stakes blades, distance weapons, martial arts
Buttkicker Cutthroat
Brenda Cathy
10/5/1978 17/2/1988
15/06/2005 15/06/2006
Killer
Karl
4/7/1972
Demon Dojo
15/06/2006
And Id better have a table of those who provide support from the sidelinesstudents of the arcane, rogue fighters who sometimes partner with the Council, etc. Two tables so far
Page 4 of 22
Term Paper
LAST NAME
FIRST NAME
DATE OF BIRTH
EXPERTISE antique weapons, American folklore, American history, blades, martial arts world folklore, world history demons, fey, lycanthropes, undead
Clueful
Clarence
10/5/1978
15/06/2005
Elder
Ellen
17/2/1988
15/06/2006
Learned
Lucy
4/7/1972
15/06/2006
But, Many of the sideline personnel have at least some fighting ability. Clarence Clueful has retired from active fighting because middle age took away his edge, but he still knows how to use those weapons as well as discuss them. Rodney Rogues index card is in th e half-toppled pile of other people instead of fighters because he isnt a Council of Light member, but he does fight. Wouldnt it be simpler just to merge these, and have one table for all our human resources? But then we have fields which are null for almost every entry, such as formal training, because only rows for full-fledged fighters need them. Those empty fields will lead to a performance penalty which is unacceptable when designing for a secret society charged with keeping the world safe from the forces of darkness. So are two tables the right choice? Isnt that a much worse performance hit, having to search at least two tables whenever we want to search all monster fighters? Maybe we should put this cowboy database design aside for the moment, to see if we can learn anything from the normalized approach.
Page 5 of 22
Term Paper
3. Normalization Form
We usually speak of five normalization forms. This is a little misleading. The standard numbering is useful for discussion, though, since everyone knows what Third Normal Form or Normal 3 means without needing an explanation. One might say the names are normalized.
3.1.
First Normalization
The First Normal Form is really just common sense. It tells us two things: to eliminate duplicate columns, and that some column or combination of columns should be a unique identifier. Eliminating columns which are flat-out duplicates is a no-brainer. Obviously you shouldnt create two different columns called birthdate and date of birth, since a given individual is generally born only once. Duplication may be a little less obvious than blatant copies, though. If my employee table has a firstname field and a lastname field, it might be tempting to have a derived fullname field to speed up searching. Derived data is allowed at the Normal 1 level.
LAST NAME DATE JOINED COUNCIL
FIRST NAME
DATE OF BIRTH
TRAINING
Natarajan Meganthan
Suganthi Saravaan
10/5/1978 17/2/1988
15/06/2013 15/06/2013
But what happens if employee Suganthi Natarajan gets married and takes his husbands name? Wouldnt you, if you were born with a name like Suganthi? Now we need to update two fields. We could rely on our application, and all other possible applications ever written by anyone else, to update both lastname and fullname. We could set up trigger functions to auto change fullname when lastname is changed, and hope nobody accesses the data through any other means. Or we could be normal and eliminate the duplication of the lastname and fullname columns by not having a fullname column, and combine firstname with lastname when we need a full name.
Page 6 of 22
Term Paper
A derivation as simple as bunging two strings together is really just duplication, trying to be sneaky by wearing a Richard Nixon mask. Duplication, sneaky or otherwise, is disallowed by Normal 1. The other requirement of the First Normal Form is that some column or combination of them be unique in the database. The birthdate field might seem like a good choice, since there are an awful lot of days in a span of several generations. But what if a pair of twins joins the Council? Also, as our team grows, chance makes it virtually certain that at least two were born on the same day. Consider the so-called Birthday Paradoxwhenever 23 people are considered, the odds are better than even at that least two of them share a birthday, so when several hundred people of one generation are considered the odds of birthdate overlap are unacceptably high. firstname+lastname+birthdate might be a safe enough choice for uniqueness, at the Normal 1 level. Why all this insistence on uniqueness at the most basic normalization level? Simple. We need a reliable way to retrieve any given single record.
3.2.
The concept of remove the delicacy of data comes in the Second Normal Form (2NF). A. It should meet all the requirements of the first normal form. B. It should remove subsets of data that apply to multiple rows of a table and place them in separate tables. C. It creates relationships between these new tables and their predecessors through the use of foreign keys. The First Normal form deals with the atomicity whereas the Second Normal Form deals with the relationship between the composite key columns and non-key columns. To achieve the next progressive level your table should satisfy the requirement of First Normal Form then move towards the Second Normal Form.
Page 7 of 22
Term Paper
Table is not in Second Normal Form because the price and tax depends on the item, but not color.
Item Pen Pen Scale Scale Bag Bag Colors red blue red yellow blue black
Page 8 of 22
Term Paper
3.3.
B. It should remove columns that are not dependent upon the primary key. In the Third Normal Form all columns depend upon the primary key. When one column depends upon the other column, table break the rule and turns into the
Item Pen Pen Scale Scale Bag Bag Colors red blue red yellow blue black Item Pen Scale Price 2.0 2.0 150.00 Tax 0.20 0.20 7.80
Tables are not in Second Normal Form because tax depends on price, not item.
Item Pen Pen Scale Scale Bag Bag Price 2.0 150.00 Colors red blue red yellow blue black Tax 0.20 7.80
Bag
Term Paper
3.4.
Under fourth normal form, a record type should not contain two or more independent multivalued facts about an entity. In addition, the record must satisfy third normal form. The term "independent" will be discussed after considering an example. Consider employees, skills, and languages, where an employee may have several skills and several languages. We have here two many-to-many relationships, one between employees and skills, and one between employees and languages. Under fourth normal form, these two relationships should not be represented in a single record such as
EMPLOYEE SKILL LANGUAGE
Note that other fields, not involving multi-valued facts, are permitted to occur in the record, as in the case of the QUANTITY field in the earlier PART/WAREHOUSE example. The main problem with violating fourth normal form is that it leads to uncertainties in the maintenance policies. Several policies are possible for maintaining two independent multi-valued facts in one record: (1) A disjoint format, in which a record contains either a skill or a language, but not both
EMPLOYEE
Smith Smith Smith Smith Smith
SKILL
cook type
LANGUAGE
Page 10 of 22
3 rd Semester - 122544013
Data Normalization & Denormalization (2) A random mix, with three variations (a) Minimal number of records, with repetitions:
EMPLOYEE
Smith Smith Smith
Term Paper
SKILL
cook type type
LANGUAGE
French
German
Greek
SKILL
cook type
LANGUAGE
French
German
Greek
(c) Unrestricted:
EMPLOYEE
Smith Smith Smith Smith type
SKILL
cook type
LANGUAGE
French
German
Greek
(3) A "cross-product" form, where for each employee, there must be a record for every possible pairing of one of his skills with one of his languages:
EMPLOYEE
Smith Smith Smith Smith Smith Smith
SKILL
cook cook cook type type type
LANGUAGE
French German Greek French German Greek
Other problems caused by violating fourth normal form are similar in spirit to those mentioned earlier for violations of second or third normal form. They take different variations depending on the chosen maintenance policy:
Page 11 of 22
3 rd Semester - 122544013
Term Paper
o if there are repetitions, then updates have to be done in multiple records, and they could become inconsistent. o Insertion of a new skill may involve looking for a record with a blank skill, or inserting a new record with a possibly blank language, or inserting multiple records pairing the new skill with some or all of the languages. o Deletion of a skill may involve blanking out the skill field in one or more records (perhaps with a check that this doesn't leave two records with the same language and a blank skill), or deleting one or more records, coupled with a check that the last mention of some language hasn't also been deleted.
3.5.
Fifth normal form deals with cases where information can be reconstructed from smaller pieces of information that can be maintained with less redundancy. Second, third, and fourth normal forms also serve this purpose, but fifth normal form generalizes to cases not covered by the others. We will not attempt a comprehensive exposition of fifth normal form, but illustrate the central concept with a commonly used example, namely one involving agents, companies, and products. If agents represent companies, companies make products, and agents sell products, then we might want to keep a record of which agent sells which product for which company. This information could be kept in one record type with three fields:
AGENT
Smith Smith COMPANY Ford GM PRODUCT car truck
This form is necessary in the general case. For example, although agent Smith sells cars made by Ford and trucks made by GM, he does not sell Ford trucks or GM cars. Thus we need the combination of three fields to know which combinations are valid and which are not.
Page 12 of 22
3 rd Semester - 122544013
Term Paper
But suppose that a certain rule was in effect: if an agent sells a certain product, and he represents a company making that product, then he sells that product for that company.
AGENT
Smith Smith Smith Smith Jones COMPANY Ford Ford GM GM Ford PRODUCT car truck car truck car
In this case, it turns out that we can reconstruct all the true facts from a normalized form consisting of three separate record types, each containing two fields:
AGENT Smith Smith Jones COMPANY Ford GM Ford COMPANY Ford Ford GM GM PRODUCT car truck car truck AGENT Smith Smith Jones PRODUCT car truck car
These three record types are in fifth normal form, whereas the corresponding three-field record shown previously is not. Roughly speaking, we may say that a record type is in fifth normal form when its information content cannot be reconstructed from several smaller record types, i.e., from record types each having fewer fields than the original record. The case where all the smaller records have the same key is excluded. If a record type can only be decomposed into smaller records which all have the same key, then the record type is considered to be in fifth normal form without decomposition. A record type in fifth normal form is also in fourth, third, second, and first normal forms. Fifth normal form does not differ from fourth normal form unless there exists a symmetric constraint such as the rule about agents, companies, and products. In the absence of such a constraint, a record type in fourth normal form is always in fifth normal form.
Page 13 of 22
3 rd Semester - 122544013
Term Paper
One advantage of fifth normal form is that certain redundancies can be eliminated. In the normalized form, the fact that Smith sells cars is recorded only once; in the unnormalized form it may be repeated many times. It should be observed that although the normalized form involves more record types, there may be fewer total record occurrences. This is not apparent when there are only a few facts to record, as in the example shown above. The advantage is realized as more facts are recorded, since the size of the normalized files increases in an additive fashion, while the size of the unnormalized file increases in a multiplicative fashion. For example, if we add a new agent who sells x products for y companies, where each of these companies makes each of these products, we have to add x+y new records to the normalized form, but xy new records to the unnormalized form. It should be noted that all three record types are required in the normalized form in order to reconstruct the same information. From the first two record types shown above we learn that Jones represents Ford and that Ford makes trucks. But we can't determine whether Jones sells Ford trucks until we look at the third record type to determine whether Jones sells trucks at all. The following example illustrates a case in which the rule about agents, companies, and products is satisfied, and which clearly requires all three record types in the normalized form. Any two of the record types taken alone will imply something untrue.
AGENT
Smith Smith Smith Smith Jones Jones Brown Brown Brown Brown COMPANY Ford Ford GM GM Ford Ford Ford GM Toyota Toyota PRODUCT car truck car truck car truck car car car bus
Page 14 of 22
3 rd Semester - 122544013
Term Paper
PRODUCT car truck car truck car bus
Observe that: Jones sells cars and GM makes cars, but Jones does not represent GM. Brown represents Ford and Ford makes trucks, but Brown does not sell trucks. Brown represents Ford and Brown sells buses, but Ford does not make buses. Fourth and fifth normal forms both deal with combinations of multivalued facts. One difference is that the facts dealt with under fifth normal form are not independent, in the sense discussed earlier. Another difference is that, although fourth normal form can deal with more than two multivalued facts, it only recognizes them in pairwise groups. We can best explain this in terms of the normalization process implied by fourth normal form. If a record violates fourth normal form, the associated normalization process decomposes it into two records, each containing fewer fields than the original record. Any of these violating fourth normal form is again decomposed into two records, and so on until the resulting records are all in fourth normal form. At each stage, the set of records after decomposition contains exactly the same information as the set of records before decomposition. In the present example, no pairwise decomposition is possible. There is no combination of two smaller records which contains the same total information as the original record. All three of the smaller records are needed. Hence an information-preserving pairwise decomposition is not possible, and the original record is not in violation of fourth normal form. Fifth normal form is needed in order to deal with the redundancies in this case.
Page 15 of 22
3 rd Semester - 122544013
Term Paper
3.6.
I havent said that the sixth normal earlier. But I am going to mention this for completeness. You may have heard of a Sixth Normal Form. This term is used in two different ways, and both of them are for academic use. Its intended to break down data relations until theyve been reduced to irreducible components. If youre designing real databases for real use, ignore it. Both of it. Please
4. Denormalization
Denormalization doesnt mean not normalizing. Its a step in the process. First we normalize, then we realize that we now have hundreds or thousands of small tables and that the performance cost of all those joins will be prohibitive, and then we carefully apply some denormalization techniques so that our application will return results before the werewolves have overrun the city.
Term Paper
organization which has more data than it could reasonably process by hand. Thats the crossover point at which we must treat the database as a vital part of the organizations mission. Normal 4 isnt always necessary, but if youve done Normal 3, why not have a guaranteedunique ID for each record? Unlike some of the other steps, this one is actually a performance win. Its faster to look up one field than to check several to see if the combinations unique, and you still have the option of searching by other means. Stringing data down rows instead of across columns may lead to performance slowdown, but there are ways to address this, and well look at some of them. Normal 4 is an excellent idea for any organization with large amounts of dataagain, because keeping the data straight is vital to the organizations mission. Normal 5 is rarely necessary. It reaches into rarified heights where the purity of the data model is more important than the real-world usability. Except in rare cases where permanent and inflexible real-world constraints match the logical data design beautifully, youll probably end up needing to denormalize anyway. Think it through, because youll realize some good stuff, but dont worry about meeting it to the letter. If you did Normal 4 correctly, you probably already met it. To summarize at least Normal 2 for tiny organizations, and at least Normal 4 for any operation where the datas critical. Ill be conforming to Normal 4 for the Council of Lights final database design, although thinking about Normal 5 did lead me to some realizations Ill incorporate into my design
6. Should I Denormalize?
Since normalization is about reducing redundancy, denormalizing is about deliberately adding redundancy to improve performance. Before beginning, consider whether or not its necessary. Is the systems performance unacceptable with fully normalized data? Mock up a client and do some testing.
Page 17 of 22
3 rd Semester - 122544013
Term Paper
If the performance is unacceptable, will denormalizing make it acceptable? Figure out where your bottlenecks are. If you denormalize to clear those bottlenecks, will the system and its data still be reliable? Unless the answers to all three are yes, denormalization should be avoided. Unfortunately for me, the Council of Light has some pretty old hardware and cant or wont upgrade to newer and more efficient SQL software. Id better get to work.
1.
Materialized Views
If were lucky, we wont need to denormalize our logical data design at all. We can let the database management system store additional information on disk, and its the responsibility of the DBMS software to keep the redundant information consistent. Oracle does this with materialized views, which are pretty much what they sound like SQL views, but made material on the disk. Like regular views, they change when the underlying data does. Microsoft SQL has something called indexed views, which I gather are similar although Ive never used them. Other SQL databases have standard hacks to simulate materialized views; a little Googling will tell you whether or not yours does.
2.
Database Constraints
The more common approach is to denormalize some carefully chosen parts of the database design, adding redundancies to speed performance. Danger lies ahead. It is now the database designers responsibility to create database constraints specifying how redundant pieces of data will be kept consistent. These constraints introduce a tradeoff. They do speed up reads, just as we wish, but they slow down writes. If the databases regular usage is write -heavy, such as the Council of Lights message forum, then this may actually be worse than the non-denormalized version.
Page 18 of 22
3 rd Semester - 122544013
Term Paper
3.
If enough storage space is available, we can keep one fully-normalized master database, and another denormalized database for querying. No app ever writes directly to the denormalized one; its read-only from the apps point of view. Similarly, no app ever reads directly from the normalized master; its write-only from the apps point of view. This is a good, if expensive, plan. The fully normalized master database updates the querying database upon change, or the querying database repopulates itself on a schedule. We no longer have slower writes, and we still have faster reads. Our problem now becomes keeping the two database models perfectly synchronized. That itself takes time, of course, but your particular situation may be such that you can do this. Most commonly, the denormalized version populates itself by querying the normalized master on a controlled schedule. It will thus be the only thing which ever reads from the normalized master. The danger is that data returned from the denormalized query base may be inaccurate if the data has changed in the normalized master but the denormalized query database hasnt repopulated itself yet. If your data doesnt generally change in a heartbeat, this may be acceptable. If the future of all humankind hinges on the currency of your data, this approach may not be acceptable. Another possibility is for the normalized master to change the denormalized query database whenever the normalized master is changed. This requires a complex set of triggers interfacing the normalized and denormalized data designs, and the trigger set must be updated when the denormalized design is altered. The need to handle triggers will slow down writes, so this isnt a good option for write-heavy applications, but the upside is that your two databases are much less likely to be out of synch. Space requirements often prohibit this strategy entirely
Page 19 of 22
3 rd Semester - 122544013
Term Paper
7. Denormalization Tactics
All right, so those are the basic approaches. All of them have one thing in common, which is that they deliberately introduce redundancies. How do we decide what redundancies to introduce? Simple, we find out exactly what is causing those performance problems we noted you know, the ones which must be present in order for us to consider denormalization. Then we introduce redundancies which will make those particular causes go away. The two most common bottlenecks are the cost of joining multiple tables and, on very active systems, the activity on a particular table being too high.
1. Pre-Joined Tables
Is it the joins? Which ones? Perhaps we find that our users do a lot of queries which require joining the vampire table and the zombie table. If so, we could combine them into a single prejoined table of the undead. If our users often search all available personnel, joining combatants and former combatants and noncombatants, then we may want to revert to our idea of keeping all Council members and other helpers in a single personnel table. This table is essentially the materialization of a join, so we populate if from the original tables either on a schedule or via triggers when changing the originals.
2. Mirror Tables
If a particular table is too active, with constant querying and updating leading to slowdowns and timeouts and deadlocks, consider a miniature and internal version of Double your database fun. You can have a background and foreground copy of the same table. The background table takes only writes, the foreground table is for reading, and synchronizing the two is a low-priority process that happens when less is going on. The obvious problem is that the data youre reading has no guaranteed currency. In fact, given that were doing this because a table is particularly active, its fairly likely not to be current. Its
Page 20 of 22 Manipal University MS(cs) 3 rd Semester - 122544013
Term Paper
something to consider when the data in question doesnt require up -to-the-second currency, though.
3. Split Tables
We may have distinct groups of users who query tables in distinct ways. Remember our table of werewolf clans? Monster fighters and coordinators may be interested only in clans which havent been exterminated, while historians studying the effectiveness of extermination methods may be interested only in those which have. I could split that single table in two. The original table could also be maintained as a master, in which case the split tables are special cases of mirror tables (above) which happen to contain a subset instead of a complete copy. If few people want to query the original table, I could maintain it as a view which joins the split tables and treat the split tables as masters
8. Denormalization in Hyperspace
These are elegant, but a great deal of design work because they require math that most of us never learned. Modeling data into cubes? It sure sounds interesting, but it also sounds suspiciously like one of those approaches where the approach turns into the real project. Im as likely to make things worse as I am to make them better. These things are fun to read about, but not suitable for most real-world deployments. If you think youre up for the task, Wikipedia is either a great starting place or a much-needed reality check.
9. Conclusion
How does this help me and you with my original problem? I started with one question: Should all put them in one table, or should split and noncombatants be treated separately? Well, Id just about concluded that I should have several separate tables for people in different roles and use a Postgres hack to simulate a materialized view for searching all personnel at once. That will avoid both the join performance hit and the many-nulls performance hit. After going through the normalization and denormalization process, I see that thats the best answer . If
Page 21 of 22 Manipal University MS(cs) 3 rd Semester - 122544013
Term Paper
they cant cough up a few bucks to save the world more efficiently, they can keep poring over their dusty tomes forever. While I have tried to present the normalization and denormalization in a simple and understandable way, we are by no means suggesting that the data design process is correspondingly simple. The design process involves many complexities which are quite beyond the scope of this paper. In the first place, an initial set of data elements and records has to be developed, as candidates for normalization. One need to think about the normalization is required or not. How does the denormalization benefit over the normalization. Then the factors affecting normalization have to be assessed: Single-valued vs. multi-valued facts. Dependency on the entire key. Independent vs. dependent facts. The presence of mutual constraints. The presence of non-unique or non-singular representations. And, finally, the desirability of normalization or denormalization has to be assessed, in terms of its performance impact on retrieval applications.
10.
Reference
1. E.F. Codd, "A Relational Model of Data for Large Shared Data Banks" 2. W. Kent, "A Primer of Normal Forms"
3. Wikipedia 4. Database Design for Mere Mortals(R): A Hands-On Guide to Relational Database Design
(2nd Edition)by Mike Hernandez 5. Fundamentals of Relational Database Design - By Paul Litwin
Page 22 of 22
3 rd Semester - 122544013