{"id":1431,"date":"2017-03-15T17:18:12","date_gmt":"2017-03-15T17:18:12","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=1431"},"modified":"2017-04-01T22:08:12","modified_gmt":"2017-04-01T22:08:12","slug":"comparison-cardinality-estimate-oracle-sql-server","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/","title":{"rendered":"Comparison of a Cardinality Estimate in Oracle and SQL Server"},"content":{"rendered":"<p>This blog post is a short observation on assumptions that Oracle and SQL server optimizers make to estimate the cardinality of a rowset obtained after applying an equality predicate which contains different columns of the same table, for example:<\/p>\n<pre><code>select count(*) from t where <span style=\"color: #ff0000;\">n1 = n2<\/span> ;<\/code><\/pre>\n<p>The demo is set up as follows:<\/p>\n<ul>\n<li>The table t has 100,000 rows.<\/li>\n<li>The column n1 has 5 distinct values.<\/li>\n<li>The column n2 has 10,000 distinct values.<\/li>\n<li>The data distribution in both columns is uniform.<\/li>\n<li>The most recent versions of both database products are used: Oracle 12.2 and SQL Server 2016.<\/li>\n<\/ul>\n<p>The conclusions are consistent over various data sets.<\/p>\n<h1>Oracle<\/h1>\n<pre><code>drop table t ;\r\ncreate table t (n1 number , n2 number ) ; \r\n\r\ninsert into t\r\n  SELECT mod(level,5),mod(level,10000)\r\n  FROM   dual\r\n  CONNECT BY level &lt;= <span style=\"color: #ff0000;\">100000<\/span> ;\r\n\r\nexec dbms_stats.gather_table_stats( null , 'T' ) ;\r\n\r\ncolumn column_name format a3\r\nselect column_name,num_distinct ndv,density \r\n  from user_tab_columns where table_name='T' ;\r\nCOL        NDV    DENSITY\r\n--- ---------- ----------\r\nN1           5         .2\r\nN2       <span style=\"color: #ff0000;\">10000      .0001<\/span>\r\n\r\nselect count(*) from t where n1 = n2 ;\r\n  COUNT(*)\r\n----------\r\n        50\r\n\r\nSELECT * FROM table(DBMS_XPLAN.DISPLAY_CURSOR);\r\n\r\n---------------------------------------------------------------------------\r\n| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |\r\n---------------------------------------------------------------------------\r\n|   0 | SELECT STATEMENT   |      |       |       |    23 (100)|          |\r\n|   1 |  SORT AGGREGATE    |      |     1 |     7 |            |          |\r\n|*  2 |   TABLE ACCESS FULL| T    |    <span style=\"color: #ff0000;\">10<\/span> |    70 |    23   (0)| 00:00:01 |\r\n---------------------------------------------------------------------------\r\n\r\nPredicate Information (identified by operation id):\r\n---------------------------------------------------\r\n\r\n   2 - filter(\"N1\"=\"N2\")<\/code><\/pre>\n<p><strong>Oracle treats the less selective column (n1) as a constant and uses the density of the more selective column (n2) for the cardinality estimation.<\/strong><\/p>\n<h1>SQL Server<\/h1>\n<pre><code>drop table t ;\r\ncreate table t ( n1 integer , n2 integer ) ;\r\n\r\ninsert into t select top(<span style=\"color: #ff0000;\">100000<\/span>)\r\n  CONVERT(INT, ROW_NUMBER() OVER (ORDER BY s1.[object_id])) % 5 ,\r\n  CONVERT(INT, ROW_NUMBER() OVER (ORDER BY s1.[object_id])) % 10000\r\n  FROM sys.all_objects AS s1 CROSS JOIN sys.all_objects AS s2\r\nOPTION (MAXDOP 1);\r\n\r\nset showplan_all on\r\nselect count(*) from t where n1 = n2 ;\r\n\r\nEstimateRows  StmtText\r\n1\t      select count(*) from t where n1 = n2 ;\r\n1\t        |--Compute Scalar(DEFINE:([Expr1003]=CONVERT_IMPLICIT(int,[Expr1004],0)))\r\n1\t          |--Stream Aggregate(DEFINE:([Expr1004]=Count(*)))\r\n<span style=\"color: #ff0000;\">10000<\/span>\t           |--Table Scan(OBJECT:([master].[dbo].[t]), WHERE:([master].[dbo].[t].[n1]=[master].[dbo].[t].[n2]))<\/code><\/pre>\n<p><strong>SQL Server makes the assumption that the filtered rowset will contain 10% rows of the table.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post is a short observation on assumptions that Oracle and SQL server optimizers make for estimating cardinality of a rowset obtained after applying an equality predicate which contains different columns of the same table. <a href=\"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[21,11,5,17],"tags":[],"class_list":["post-1431","post","type-post","status-publish","format-standard","hentry","category-comparison-oracle-sql-server","category-cost-based-optimizer","category-oracle","category-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics<\/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:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"This blog post is a short observation on assumptions that Oracle and SQL server optimizers make for estimating cardinality of a rowset obtained after applying an equality predicate which contains different columns of the same table. Continue Reading &rarr;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-15T17:18:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-01T22:08:12+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\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\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Comparison of a Cardinality Estimate in Oracle and SQL Server\",\"datePublished\":\"2017-03-15T17:18:12+00:00\",\"dateModified\":\"2017-04-01T22:08:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/\"},\"wordCount\":145,\"commentCount\":0,\"articleSection\":[\"Comparison Oracle-SQL Server\",\"cost based optimizer\",\"Oracle\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/\",\"name\":\"Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-03-15T17:18:12+00:00\",\"dateModified\":\"2017-04-01T22:08:12+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/comparison-cardinality-estimate-oracle-sql-server\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Comparison of a Cardinality Estimate in Oracle and SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics","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:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics","og_description":"This blog post is a short observation on assumptions that Oracle and SQL server optimizers make for estimating cardinality of a rowset obtained after applying an equality predicate which contains different columns of the same table. Continue Reading &rarr;","og_url":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/","og_site_name":"All-round Database Topics","article_published_time":"2017-03-15T17:18:12+00:00","article_modified_time":"2017-04-01T22:08:12+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Comparison of a Cardinality Estimate in Oracle and SQL Server","datePublished":"2017-03-15T17:18:12+00:00","dateModified":"2017-04-01T22:08:12+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/"},"wordCount":145,"commentCount":0,"articleSection":["Comparison Oracle-SQL Server","cost based optimizer","Oracle","SQL Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/","url":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/","name":"Comparison of a Cardinality Estimate in Oracle and SQL Server - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2017-03-15T17:18:12+00:00","dateModified":"2017-04-01T22:08:12+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/comparison-cardinality-estimate-oracle-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Comparison of a Cardinality Estimate in Oracle and SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":1437,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions\/1437"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}