{"id":786,"date":"2007-06-24T16:29:00","date_gmt":"2007-06-24T16:29:00","guid":{"rendered":"\/blogs\/bobb\/post\/And-the-EAV-winner-is-sparse-columns.aspx"},"modified":"2007-06-24T16:29:00","modified_gmt":"2007-06-24T16:29:00","slug":"and-the-eav-winner-is-sparse-columns","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/","title":{"rendered":"And the EAV winner is &#8230;. sparse columns"},"content":{"rendered":"<p>\nMany of you have already heard the &quot;hardware store&quot; story. What&#39;s the best way to model products in a hardware store, where new items arrive at the hardware store each day. Each item has a &quot;short list&quot; of similar properties (e.g. UPC, price) and a long list of dissimilar properties (e.g. paint has color, type, amount and curtain rods have width, metal, etc). How to model the dissimilar properties for each item in relational table(s)?\n<\/p>\n<p>\nThis isn&#39;t as unusual of a problem as you might think, examples I&#39;ve heard lately include:<br \/>\n&nbsp;Items in a directory system (like AD)<br \/>\n&nbsp;Readings for lab test results<br \/>\n&nbsp;Attributes for Sharepoint items\n<\/p>\n<p>\nI&#39;ve always thought of the main contenders as:<br \/>\n1. Sparse tables &#8211; one per product<br \/>\n2. Sparse columns &#8211; 90% of the column values would be NULL<br \/>\n3. Model as XML &#8211; similar properties are subelements, sparse properties are attributes<br \/>\n4. Entity-attribute-value (EAV) &#8211; also known as open schema. A separate &quot;properties&quot; table with name-value pairs.\n<\/p>\n<p>\nEAV is one of the most popular solutions, even supposedly endorsed by standard schemas in some industries. Many relational purists detest EAV because its non-relational. It&#39;s main drawbacks are that the &quot;name-value pair&quot; table gets huge fast, with the corresponding lack of performance, the need for careful editing (color and colour would be two different attributes), and the fact that the &quot;value&quot; column of name-value must have a data type of nvarchar or SQL-variant.\n<\/p>\n<p>\nSQL Server 2005 added the PIVOT keyword. One use for PIVOT is the change the EAV tables into something that looks like sparse tables.\n<\/p>\n<p>\nI even had the opportunity to ask Joe Celko (no fan of EAV) which he prefers, trying to ease him towards the &quot;model as XML&quot; mechanism. He stood up for sparse tables or sparse columns.\n<\/p>\n<p>\nSQL Server 2008 will include support for sparse columns. You can designate a column as\n<\/p>\n<p>\nSPARSE in the DDL, like this:\n<\/p>\n<p>\nCREATE TABLE products (product_num int, item_num int, price decimal(7,2), &#8230;,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color char(5) SPARSE, width float SPARSE&#8230;)\n<\/p>\n<p>\nYou can have a huge number of sparse columns per table, although the number of non-sparse columns remains at 1024. In addition, SQL Server 2008 will support sparse indexes (aka filtered indexes) defined like:\n<\/p>\n<p>\nCREATE INDEX coloridx ON products(color) WHERE product_num IN (21,22,42&#8230;)\n<\/p>\n<p>\nFinally, you can have an XML &quot;COLUMN SET&quot; column for each table; this exposes the sparse properties (or perhaps a subset of them?) for each item as a collection of XML elements, for those folks that like to model these as XML.\n<\/p>\n<p>\nALTER TABLE products ADD COLUMN properties XML COLUMN_SET FOR ALL_SPARSE_COLUMNS\n<\/p>\n<p>\nIt&#39;s an interesting idea; the proof will be in the perf as well as the usability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many of you have already heard the &quot;hardware store&quot; story. What&#39;s the best way to model products in a hardware store, where new items arrive at the hardware store each day. Each item has a &quot;short list&quot; of similar properties (e.g. UPC, price) and a long list of dissimilar properties (e.g. paint has color, type, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,29],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-sparse-columns","category-sql-server-2008"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>And the EAV winner is .... sparse columns - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"And the EAV winner is .... sparse columns - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"Many of you have already heard the &quot;hardware store&quot; story. What&#039;s the best way to model products in a hardware store, where new items arrive at the hardware store each day. Each item has a &quot;short list&quot; of similar properties (e.g. UPC, price) and a long list of dissimilar properties (e.g. paint has color, type, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2007-06-24T16:29:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/\",\"name\":\"And the EAV winner is .... sparse columns - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2007-06-24T16:29:00+00:00\",\"dateModified\":\"2007-06-24T16:29:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sparse Columns\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sparse-columns\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"And the EAV winner is &#8230;. sparse columns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"And the EAV winner is .... sparse columns - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/","og_locale":"en_US","og_type":"article","og_title":"And the EAV winner is .... sparse columns - Bob Beauchemin","og_description":"Many of you have already heard the &quot;hardware store&quot; story. What&#39;s the best way to model products in a hardware store, where new items arrive at the hardware store each day. Each item has a &quot;short list&quot; of similar properties (e.g. UPC, price) and a long list of dissimilar properties (e.g. paint has color, type, [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/","og_site_name":"Bob Beauchemin","article_published_time":"2007-06-24T16:29:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/","name":"And the EAV winner is .... sparse columns - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2007-06-24T16:29:00+00:00","dateModified":"2007-06-24T16:29:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/and-the-eav-winner-is-sparse-columns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Sparse Columns","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/sparse-columns\/"},{"@type":"ListItem","position":3,"name":"And the EAV winner is &#8230;. sparse columns"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}