{"id":38,"date":"2009-03-06T04:56:07","date_gmt":"2009-03-06T11:56:07","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=27"},"modified":"2009-03-06T04:56:07","modified_gmt":"2009-03-06T11:56:07","slug":"a-perfectly-normalized-database-is-a-timebomb-waiting-to-explode","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2009\/03\/06\/a-perfectly-normalized-database-is-a-timebomb-waiting-to-explode\/","title":{"rendered":"A perfectly normalized database is a timebomb waiting to explode.."},"content":{"rendered":"<figure id=\"attachment_37\" aria-describedby=\"caption-attachment-37\" style=\"width: 100px\" class=\"wp-caption alignleft\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-37\" title=\"timebomb\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2009\/03\/timebomb.jpg\" alt=\"BOOM!\" width=\"100\" height=\"115\" \/><figcaption id=\"caption-attachment-37\" class=\"wp-caption-text\">BOOM!<\/figcaption><\/figure>\n<p>Once upon a time,  God came down on earth in the form of  <a title=\"EF Codd\" href=\"http:\/\/en.wikipedia.org\/wiki\/E.F._Codd\" target=\"_blank\">E.F.Codd <\/a>and gave us the Holy Tenets of Database Design.<\/p>\n<figure id=\"attachment_35\" aria-describedby=\"caption-attachment-35\" style=\"width: 82px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-35\" title=\"codd\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2009\/03\/codd.jpg\" alt=\"EF Codd\" width=\"82\" height=\"116\" \/><figcaption id=\"caption-attachment-35\" class=\"wp-caption-text\">EF Codd<\/figcaption><\/figure>\n<p>Among the most important thing that every database designer or application developer had to learn was<\/p>\n<p style=\"text-align: center;\"><strong><span style=\"color: #ff0000;\"><em>&#8220;Thou shalt design your database so that it at least fulfills the conditions of 3NF (3rd normal form)&#8221;<\/em><\/span><\/strong><\/p>\n<p style=\"text-align: left;\"><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Since then thousands of people have used that adage as the basis of <a title=\"Db design\" href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=30646\" target=\"_blank\">good database design <\/a>(myself included). And , it works, you know! Without following those basic principles of good database design, it just would not be possible to build databased applications in any sane manner.<\/span><\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">So, what exactly is the problem then? The problem becomes apparent to anyone who ends up with a at least few thousand rows of data in multiple tables. <\/span><\/span><\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff0000;\"><span style=\"color: #000000;\"><strong>The more optimized a database is, the more effort and time it takes to retrieve data. <\/strong><\/span><\/span><\/p>\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\"><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">You see, database normalization is aimed at storing data in the most optimal manner &#8211; preserving referential integrity, and removing data redundancy, and making inserts, updates and deletes as painless as possible. But this is just looking at one part of the picture. <\/span><\/span><\/p>\n<p style=\"text-align: center;\"><strong><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Due to the conceptual idea of a database being what it is, optimizing it for storage automatically makes it unoptimized for retrieval.<\/span><\/span><\/strong><\/p>\n<p style=\"text-align: left;\"><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">So lets say you have stored all the relevant details of Employees across 4 tables:<\/span><\/span><\/p>\n<ul>\n<li><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Employee master<\/span><\/span><\/li>\n<li><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Committees part of<br \/>\n<\/span><\/span><\/li>\n<li><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Salary Transactions<\/span><\/span><\/li>\n<li><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">Projects Handled<br \/>\n<\/span><\/span><\/li>\n<\/ul>\n<p>So , its all neatly stored in 4 tables with all the tables linked by primary and foreign keys and indexed and retrofitted with the relevant triggers. So now , what do we do when we want to see an employee&#8217;s details in one page. We form 4 JOIN operations and then retrieve the data. This is of course, assuming that the other fields in the tables themselves do not require accessing still more tables.<\/p>\n<p>This above scenario is not so much of a performance issue, with a table of 40 employees records. But when you have 150,000 employee records then things start to crash. Imagine doing those join operations across 4 tables with that kind of data. No amount of indexing is going to save you from the massive load the database server will go through. Lets take this scenario a little further&#8230; Imagine 40 people trying to see the details of 40 different employees at the same time, when the database has over 500,000 records in each table.<\/p>\n<figure id=\"attachment_33\" aria-describedby=\"caption-attachment-33\" style=\"width: 105px\" class=\"wp-caption alignleft\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-33\" title=\"lamp\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2009\/03\/lamp.jpg\" alt=\"Magic Lamp\" width=\"105\" height=\"121\" \/><figcaption id=\"caption-attachment-33\" class=\"wp-caption-text\">Magic Lamp<\/figcaption><\/figure>\n<p>Wouldn&#8217;t it have been ideal if , we could just get all the employee data in a single table for retrieval, but yet keep them normalized for storage?<\/p>\n<p>There was a time when I didn&#8217;t even give a second thought to normalization. Like everyone else, I took it for granted that once normalized, I dont need to really worry about database design for the rest of the project.<\/p>\n<p>I was forced to reconsider that assumption, when I was developing a web application for a major newspaper a few years back. The job was to put everything they had for the last 10 years into databases and provide a web application for users to see old issues and search and retrieve articles etc.<\/p>\n<p>Very early into the project, we realized that even with just 10,000 articles in the database, all normalized, to search using keywords was taking far too long. It was just not working out.<\/p>\n<p>To cut a long story short, we ended up having two databases  &#8211; one for storage which was to get all the data into the database and the second for retrieval which was used by the website to simply retrieve data. I wrote some offline code in Java to process and migrate the data from the storage db to the retrieval db. This approach was possible because updates were done only once at the end of the day when the newspaper was ready to go for print.<\/p>\n<p>Due to this approach, (and a couple of other innovations which are not really relevant to this article), the search database worked very fast for retrievals &#8211; we got a almost a 100-fold increase in performance.  We had simply denormalized the articles retrieval database enough so that it would take no more than 2 tables to retrieve the main article data.<\/p>\n<p>I personally feel, that the optimal solution, is to keep a different database for storage and another one for retrieval, but I also know how impractical it would be to do so, given our current approach to creating databased applications. Unless someone comes up another radical approach to storing data, or maybe some new magic middeware which automatically takes care of separating and optimizing storage and retrieval,  we will still be following the normalization process for quite some to come.<\/p>\n<p>Oh and by the way I still cant think of designing a database without normalization \ud83d\ude42 . Its just too much trouble.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Once upon a time, God came down on earth in the form of E.F.Codd and gave us the Holy Tenets of Database Design. Among the <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2009\/03\/06\/a-perfectly-normalized-database-is-a-timebomb-waiting-to-explode\/\" title=\"A perfectly normalized database is a timebomb waiting to explode..\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10],"tags":[36,88,91,190,191,250,257],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-misc","category-programming","tag-amit","tag-database","tag-db","tag-normalised","tag-normalized","tag-timebomb","tag-truelogic"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}