{"id":241,"date":"2016-06-16T10:07:04","date_gmt":"2016-06-16T09:07:04","guid":{"rendered":"https:\/\/sqldoubleg.live-website.com\/?p=241"},"modified":"2016-06-16T10:07:04","modified_gmt":"2016-06-16T09:07:04","slug":"database-design-matters-seriously","status":"publish","type":"post","link":"https:\/\/www.sqldoubleg.com\/es\/2016\/06\/16\/database-design-matters-seriously\/","title":{"rendered":"Database design matters, seriously"},"content":{"rendered":"<p>All those rules and best practices about database design exist for a reason. Today I want to show you something very simple for you to see how a good design might improve the performance of certain queries.&nbsp;This might sound pretty obvious, but really, database design matters.<\/p>\n<p>And all those rules and best practices about database design exist for a reason. Today I want to show you something very simple for you to see how a good design might improve the performance of certain queries. <\/p>\n<p>The idea for this post is originally taken out of my coworker Shane&#8217;s (<a href=\"https:\/\/nocolumnname.wordpress.com\/\" target=\"_blank\">b<\/a>|<a href=\"https:\/\/twitter.com\/tiggerinside\" target=\"_blank\">t<\/a>) latest post <a href=\"https:\/\/nocolumnname.wordpress.com\/2016\/06\/10\/primary-foreign-key\/\" target=\"_blank\">Primary Foreign Key<\/a>.<\/p>\n<p>DBA&#8217;s sometimes have to deal with funny requests like don&#8217;t create that foreign key because developers will handle that in the code&#8230; \ud83d\ude42<\/p>\n<p>By definition, Foreign key constraints will enforce the existence of a value in a table prior to be inserted into another table which references it. <\/p>\n<p>That can be done programmatically in TSQL or any other language, but it is important to understand that for the SQL engine, knowing that information in the form of a foreign key constraint can help it to take decisions beforehand and avoid some extra work.<\/p>\n<p>But, let&#8217;s stop the talking and go to the point.<\/p>\n<pre class=\"brush: tsql; title: ; notranslate\" title=\"\">\r\nUSE AdventureWorks2014\r\nGO\r\n\r\nSELECT * INTO Person.Address_noFK FROM Person.Address\r\nGO\r\n<\/pre>\n<p>I&#8217;ve just created a copy of [Person].[Address] with no Foreign Keys as opposed to the original which contains one, [StateProvinceID] which references the table [Person].[StateProvince]<\/p>\n<p>If we run a simple query against both, the original and the one without Foreign Key, we can see how they behave completely different.<\/p>\n<pre class=\"brush: tsql; title: ; notranslate\" title=\"\">\r\nSELECT * \r\nFROM Person.Address\r\nWHERE StateProvinceID IN (SELECT [StateProvinceID] FROM [Person].[StateProvince])\r\nGO\r\n\r\nSELECT * \r\nFROM Person.Address_noFK\r\nWHERE StateProvinceID IN (SELECT [StateProvinceID] FROM [Person].[StateProvince])\r\nGO \r\n<\/pre>\n<p><a href=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqldoubleg.live-website.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan.png\" alt=\"01_Query_Plan\" width=\"1102\" height=\"487\" class=\"aligncenter size-full wp-image-242\" srcset=\"https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan.png 1102w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan-300x133.png 300w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan-1024x453.png 1024w, https:\/\/www.sqldoubleg.com\/wp-content\/uploads\/2016\/06\/01_Query_Plan-150x66.png 150w\" sizes=\"(max-width: 1102px) 100vw, 1102px\" \/><\/a><\/p>\n<p>So when SQL Server knows (thanks to the Foreign Key) that all values from [Person].[Address].[StateProvinceID] must exist in [Person].[StateProvince], there is no need to do the extra work like in the second query.<\/p>\n<p>In my opinion DBA&#8217;s should always enforce Foreign key relationships at the lower level using constraints for all described above and more.<\/p>\n<p>And that&#8217;s all for this post, hope you enjoyed reading and do not trust anyone who asks you not to create that FK!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All those rules and best practices about database design exist for a reason. Today I want to show you something very simple for you to see how a good design might improve&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[40,5,15],"_links":{"self":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/241"}],"collection":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/comments?post=241"}],"version-history":[{"count":0,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/241\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/media?parent=241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/categories?post=241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/tags?post=241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}