{"id":9765,"date":"2021-08-10T00:00:34","date_gmt":"2021-08-10T07:00:34","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=9765"},"modified":"2025-06-13T18:43:33","modified_gmt":"2025-06-14T01:43:33","slug":"cost-based-optimization-with-couchbase-json-database","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/","title":{"rendered":"Boost Query Efficiency with Cost-Based Optimization in Couchbase"},"content":{"rendered":"<p><strong>Database users of every kind have come to expect cost-based optimization<\/strong> for their queries.<\/p>\n<p>It&#8217;s a standard feature of all traditional relational databases (RDBMS), but there hasn&#8217;t yet been a cost-based optimizer (CBO) for NoSQL document databases. That is, until now.<\/p>\n<p>Cost-based optimization for Couchbase queries was released as a Developer Preview in Couchbase 6.5, and <a href=\"https:\/\/www.couchbase.com\/blog\/couchbase-server-7-0-release\/?ref=blog\" target=\"_blank\" rel=\"noopener\">it&#8217;s now ready for general availability with Couchbase 7.0<\/a>.<\/p>\n<p>Let&#8217;s take a closer look at the new CBO features of Couchbase Server 7.0.<\/p>\n<h2>The Old Way: Rule-Based Optimization<\/h2>\n<p>It&#8217;s impossible to discuss cost-based optimization without referring to the previous approach to database query optimization: rule-based optimization (RBO).<\/p>\n<p>With rule-based optimization, the query optimizer considers using <em>any<\/em> index that provides a path to retrieve the query result. However, when the optimizer is presented with more than one path to process the query \u2013 that is, when the query could use multiple indexes \u2013 it could end up using <em>all<\/em> indexes.<\/p>\n<p>With cost-based optimization, the query optimizer calculates the cost of using each index, and then decides on the execution plan with the lowest cost.<\/p>\n<h2>What Is Cost-Based Optimization?<\/h2>\n<p>Cost-based optimization enables the database query optimizer to choose the best (lowest-cost) plan to process a query.<\/p>\n<p>This matters most whenever a query has more than one potential execution path, i.e., more than one qualified index. Each query plan path is assigned a total cost for all of the operations that the query needs to perform. These costs particularly depend on the specified predicates&#8217; selectivity. To arrive at the selectivity value of a predicate, the statistics of the predicate fields need to have been pre-computed.<\/p>\n<p>Please note that the cost value is an arbitrary unit of statistics calculation: it does not equate to the resource cost or CPU usage.<\/p>\n<h2>Cost-Based Optimization vs Rule-Based Optimization<\/h2>\n<p>Cost-based query optimization always provides a better execution path when the optimizer is presented with more than one option. The computed statistics ensure that the optimizer is equipped with the knowledge about the data distribution, and subsequently the efficiency of one query plan versus another.<\/p>\n<p>For instance, imagine <a href=\"https:\/\/www.couchbase.com\/blog\/scopes-and-collections-for-modern-multi-tenant-applications-couchbase-7-0\/?ref=blog\" target=\"_blank\" rel=\"noopener\">a Couchbase Collection<\/a> that contains customer information and their addresses, with an index on <code>ix1(zipcode)<\/code> and <code>ix2(city)<\/code>. Statistically, there are more ZIP codes than there are cities in the U.S. But that doesn&#8217;t necessarily mean that using the <code>ix1(zipcode)<\/code> index has a lower cost (and therefore better performance) than the <code>ix2(city)<\/code> index. Why? Because we don&#8217;t yet know if the customers in the system are evenly distributed across all U.S. cities and ZIP codes. Chances are that they&#8217;re not.<\/p>\n<p>In this scenario, it&#8217;s the statistics collected by the cost-based optimizer beforehand that help it decide the lowest-cost query path at runtime.<\/p>\n<p>With rule-based optimization, if a query has both city and ZIP code predicates, then the query optimizer uses both indexes with intersect scan. However, the CBO only uses one index \u2013 whichever one has the lowest cost based on the data within the Collection.<\/p>\n<h2>How to Enable Cost-Based Optimization in Couchbase<\/h2>\n<p>There are two important steps you need to take in order to use cost-based optimization in Couchbase Server 7.0:<\/p>\n<p><strong>1. Enable the CBO Feature<\/strong><\/p>\n<p>Currently the cost-based optimizer is enabled by default in Couchbase 7.0.<\/p>\n<p>However, you can disable it with the <a href=\"https:\/\/www.couchbase.com\/products\/n1ql\/?ref=blog\" target=\"_blank\" rel=\"noopener\">N1QL<\/a> Feature Control value in Settings-&gt;Query Settings. If you plan to use cost-based optimization, then make sure it&#8217;s enabled.<\/p>\n<p><strong>2. Collect Statistics<\/strong><\/p>\n<p>In addition to having the cost-based optimizer enabled in your cluster, you must also have collected statistics on the underlying fields for the indexes used by your queries. Here&#8217;s a sample query on how you&#8217;d do that:<\/p>\n<pre class=\"lang:default decode:true\">UPDATE STATISTICS FOR&lt;keyspace&gt; (&lt;index_expr1&gt;,..) \r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>For example, using the <code>travel-sample<\/code> dataset, this above query would look like this:<\/p>\n<pre>UPDATE STATISTICS FOR `travel-sample`._default.hotel (city);\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>You can also collect the statistics according to the index name:<\/p>\n<pre>UPDATE STATISTICS FOR `travel-sample`._default.route.def_route_schedule_utc;\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>Note that you can also disable the cost-based optimizer by either using the <code>cbo_flag<\/code> in your API, or by deleting the statistics with <code>UPDATE STATISTICS &lt;keyspace&gt; DELETE (&lt;index_expr1&gt;,..)<\/code>.<\/p>\n<h2>The CBO System Bucket<\/h2>\n<p>When you&#8217;ve enabled cost-based optimization and you&#8217;ve collected statistics, you&#8217;ll notice an additional Bucket in your cluster called <code>N1QL_SYSTEM_BUCKET.N1QL_SYSTEM_SCOPE.N1QL_CBO_STAT<\/code>. Direct access to this Bucket is restricted to system administrators.<\/p>\n<p>Using the <code>UPDATE STATISTICS<\/code> command, you can either add more stats to the Bucket or delete the stats with <code>UPDATE STATISTICS FOR &lt;keyspace&gt; DELETE<\/code>.<\/p>\n<h2>Cost-Based Optimizer in Action<\/h2>\n<p>Let&#8217;s take a closer look at how cost-based optimization works in Couchbase 7.0 using the <code>`travel-sample`<\/code> dataset in the following scenario.<\/p>\n<h3>Improved Index Selection with Cost-Based Optimization<\/h3>\n<p>Let&#8217;s say you have defined two indexes for the <code>`travel-sample`._default.airport<\/code> Collection.<\/p>\n<pre class=\"lang:default decode:true\">CREATE INDEX def_airport_city_country ON airport(city,country); \r\nCREATE INDEX def_airport_faa ON airport(faa);\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>And you have a query below:<\/p>\n<pre class=\"lang:default decode:true\">SELECT * FROM airport WHERE city = \"New York\" AND faa = \"JFK\";\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>If the above cost-based optimization weren&#8217;t used, the rules-based optimizer execution plan for the above query would be:<\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9766\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM.png\" alt=\"rules-based optimization execution plan for Couchbase N1QL query\" width=\"2206\" height=\"478\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM.png 2206w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-300x65.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-1024x222.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-768x166.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-1536x333.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-2048x444.png 2048w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-20x4.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.49.53-PM-1320x286.png 1320w\" sizes=\"auto, (max-width: 2206px) 100vw, 2206px\" \/><\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>Note that since the query predicates are on both the <code>city<\/code> and <code>faa<\/code> fields, both indexes are used in the intersect scan. Learn more about intersect scans in this article: <a href=\"https:\/\/dzone.com\/articles\/performance-ingredients-for-nosql-intersect-scans\/?ref=hello-from-couchbase\" target=\"_blank\" rel=\"noopener\">Performance Ingredients for NoSQL: Intersect Scans in N1QL<\/a>.<\/p>\n<p>Next, you enable cost-based optimization for the database query. Since CBO is already enabled by default, all you need to do is to collect the statistics.<\/p>\n<pre class=\"lang:default decode:true \">UPDATE STATISTICS FOR airport(city, faa);\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>After the statistics are collected, run the <code>SELECT<\/code> query again.<\/p>\n<p>You&#8217;ll see that the execution plan changes to using only the <code>airport.faa<\/code> index. This change is because the statistics are telling the optimizer that the <code>faa<\/code> field has a better selectivity compared to the <code>city<\/code> field. Therefore the <code>airport.faa<\/code> index is the more efficient index for the query to use.<\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9767\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM.png\" alt=\"Couchbase cost-based optimization execution plan for N1QL query\" width=\"2234\" height=\"284\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM.png 2234w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-300x38.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-1024x130.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-768x98.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-1536x195.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-2048x260.png 2048w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-20x3.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.51.17-PM-1320x168.png 1320w\" sizes=\"auto, (max-width: 2234px) 100vw, 2234px\" \/><\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<h3>A Better JOIN with Cost-Based Optimization<\/h3>\n<p>Let\u2019s look at another cost-based query optimization example that illustrates how the cost-based optimizer chooses a better JOIN type between <code>airport<\/code> and <code>route<\/code>.<\/p>\n<p>First, we create an index to track the source airport:<\/p>\n<pre class=\"lang:default decode:true\">CREATE INDEX `def_route_sourceairport` ON `travel-sample`.`_default`.`route`(`sourceairport`)\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>And then we use the query below:<\/p>\n<pre class=\"lang:default decode:true\">SELECT DISTINCT route.destinationairport FROM airport JOIN route ON airport.faa = route.sourceairport;\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>With the old rules-based optimizer, the execution plan for the above query would look like:<\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9768\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM.png\" alt=\"rules-based optimization execution plan for database JOIN\" width=\"2482\" height=\"522\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM.png 2482w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-300x63.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-1024x215.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-768x162.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-1536x323.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-2048x431.png 2048w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-20x4.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.53.30-PM-1320x278.png 1320w\" sizes=\"auto, (max-width: 2482px) 100vw, 2482px\" \/><\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>Note that the RBO execution plan above uses a nested loop to JOIN the airport Collection to the route Collection.<\/p>\n<p>Next, you need to enable cost-based optimization for the query. Since CBO is already enabled by default, all you have to do is to collect the statistics for the field in the Collection.<\/p>\n<pre class=\"lang:default decode:true \">UPDATE STATISTICS FOR route(sourceairport);\r\nUPDATE STATISTICS FOR airport(faa);\r\n<\/pre>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>With cost-based optimization stats available on the two underlying fields for the indexes, the optimizer switches over to using a <code>HASH<\/code> for the Collection JOIN.<\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9769\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM.png\" alt=\"Couchbase CBO optimizer query execution plan using a HASH for the Collection JOIN\" width=\"2484\" height=\"466\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM.png 2484w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-300x56.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-1024x192.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-768x144.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-1536x288.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-2048x384.png 2048w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-20x4.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/Screen-Shot-2020-11-13-at-1.54.58-PM-1320x248.png 1320w\" sizes=\"auto, (max-width: 2484px) 100vw, 2484px\" \/><\/p>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<h2>Cost-Based Optimizer Troubleshooting in Couchbase<\/h2>\n<ol>\n<li>Cost-based optimization is enabled by default in Couchbase 7.0. However, statistics need to be present on the specific fields in your Bucket\/Collections before the cost-based optimizer functions effectively.<\/li>\n<li>You can disable cost-based optimization at the cluster level using the N1QL Feature Control field by adding 16 to the value. It is a set with x10 bit.<\/li>\n<li>CBO statistics are not updated automatically by the Query Service. You should collect statistics on a regular basis, or after a significant change to your Collection content. I recommend collecting statistics after a large upload of data; otherwise, a weekly run should suffice.<\/li>\n<li>Cost-based optimization is only considered for a query if the statistics are available for <em>all of the indexes that were being considered by the optimizer<\/em>. You can collect the statistics for all the indexes on a given keyspace using the following:\n<pre class=\"lang:default decode:true\">UPDATE STATISTICS FOR &lt;collection&gt; INDEX((SELECT RAW name from system:indexes \r\n    WHERE state='online' AND keyspace_id='&lt;collection_name&gt;' AND bucket_id='&lt;bucket_name&gt;' AND scope_id='&lt;scope_name&gt;'))\r\n<\/pre>\n<\/li>\n<\/ol>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<h2>More Resources<\/h2>\n<p>To learn more about the cost-based optimizer for JSON \u2013 and the Couchbase 7.0 release in general \u2013 check out the following resources:<\/p>\n<ul>\n<li style=\"list-style-type: none\">\n<ul>\n<li><a href=\"https:\/\/www.couchbase.com\/blog\/why-cost-based-optimizer-for-nosql-n1ql-couchbase\/?ref=blog\" target=\"_blank\" rel=\"noopener\">Less is More: Why Use a Cost-Based Optimizer?<\/a><\/li>\n<li><a href=\"https:\/\/www.couchbase.com\/blog\/cost-based-optimizer-for-couchbase-n1ql-sql-for-json\/?ref=blog\" target=\"_blank\" rel=\"noopener\">The Cost-based Optimizer for the N1QL Query Language<\/a><\/li>\n<li><a href=\"https:\/\/people.eecs.berkeley.edu\/~brewer\/cs262\/3-selinger79.pdf?ref=hello-from-couchbase\" target=\"_blank\" rel=\"noopener\">Access Path Selection in a Relational Database Management System<\/a><\/li>\n<li><a href=\"https:\/\/docs.couchbase.com\/server\/7.0\/introduction\/whats-new.html?ref=blog\" target=\"_blank\" rel=\"noopener\">What&#8217;s New in Couchbase Server 7.0<\/a><\/li>\n<li><a href=\"https:\/\/docs.couchbase.com\/server\/7.0\/release-notes\/relnotes.html?ref=blog\" target=\"_blank\" rel=\"noopener\">Couchbase 7.0 release notes<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"wp-block-spacer\" style=\"height: 30px\" aria-hidden=\"true\"><\/div>\n<div style=\"text-align: center\"><strong>Want to kick the tires on cost-based optimization with Couchbase?&lt;br\/ &gt;<a href=\"https:\/\/www.couchbase.com\/downloads\/?ref=blog\" target=\"_blank\" rel=\"noopener\">Get Started with Couchbase 7.0<\/a><\/strong><\/div>\n<div class=\"wp-block-spacer\" style=\"height: 15px\" aria-hidden=\"true\"><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Database users of every kind have come to expect cost-based optimization for their queries. It&#8217;s a standard feature of all traditional relational databases (RDBMS), but there hasn&#8217;t yet been a cost-based optimizer (CBO) for NoSQL document databases. That is, until [&hellip;]<\/p>\n","protected":false},"author":26326,"featured_media":11680,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1814,1815,1816,1812],"tags":[8562,9361,2312,1261,1725,1385,9301],"ppma_author":[8919],"class_list":["post-9765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-design","category-best-practices-and-tutorials","category-couchbase-server","category-n1ql-query","tag-cost-based-optimization","tag-cost-based-optimizer","tag-document-database","tag-json","tag-nosql-database","tag-rdbms","tag-relational-database"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.8 (Yoast SEO v25.8) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cost-Based Query Optimization Example to Boost Efficiency<\/title>\n<meta name=\"description\" content=\"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.\" \/>\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.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Boost Query Efficiency with Cost-Based Optimization in Couchbase\" \/>\n<meta property=\"og:description\" content=\"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-10T07:00:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T01:43:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/08\/cost-based-optimization-database-queries-couchbase-social.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"418\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Binh Le\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/08\/cost-based-optimization-database-queries-couchbase-social.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Binh Le\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\"},\"author\":{\"name\":\"Binh Le\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63\"},\"headline\":\"Boost Query Efficiency with Cost-Based Optimization in Couchbase\",\"datePublished\":\"2021-08-10T07:00:34+00:00\",\"dateModified\":\"2025-06-14T01:43:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\"},\"wordCount\":1249,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg\",\"keywords\":[\"Cost Based Optimization\",\"cost-based optimizer\",\"document database\",\"JSON\",\"NoSQL Database\",\"RDBMS\",\"relational database\"],\"articleSection\":[\"Application Design\",\"Best Practices and Tutorials\",\"Couchbase Server\",\"SQL++ \/ N1QL Query\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\",\"name\":\"Cost-Based Query Optimization Example to Boost Efficiency\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg\",\"datePublished\":\"2021-08-10T07:00:34+00:00\",\"dateModified\":\"2025-06-14T01:43:33+00:00\",\"description\":\"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg\",\"width\":1200,\"height\":628,\"caption\":\"Learn how cost-based optimization in the Couchbase 7.0 release improves database query efficiency\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Boost Query Efficiency with Cost-Based Optimization in Couchbase\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63\",\"name\":\"Binh Le\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/5b68c37e30928a9d7b2c8470b1a303b7\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g\",\"caption\":\"Binh Le\"},\"description\":\"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Cloud Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK.\",\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/binh-le-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Cost-Based Query Optimization Example to Boost Efficiency","description":"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.","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.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/","og_locale":"en_US","og_type":"article","og_title":"Boost Query Efficiency with Cost-Based Optimization in Couchbase","og_description":"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.","og_url":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/","og_site_name":"The Couchbase Blog","article_published_time":"2021-08-10T07:00:34+00:00","article_modified_time":"2025-06-14T01:43:33+00:00","og_image":[{"width":800,"height":418,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/08\/cost-based-optimization-database-queries-couchbase-social.jpg","type":"image\/jpeg"}],"author":"Binh Le","twitter_card":"summary_large_image","twitter_image":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/08\/cost-based-optimization-database-queries-couchbase-social.jpg","twitter_misc":{"Written by":"Binh Le","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/"},"author":{"name":"Binh Le","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63"},"headline":"Boost Query Efficiency with Cost-Based Optimization in Couchbase","datePublished":"2021-08-10T07:00:34+00:00","dateModified":"2025-06-14T01:43:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/"},"wordCount":1249,"commentCount":1,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg","keywords":["Cost Based Optimization","cost-based optimizer","document database","JSON","NoSQL Database","RDBMS","relational database"],"articleSection":["Application Design","Best Practices and Tutorials","Couchbase Server","SQL++ \/ N1QL Query"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/","url":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/","name":"Cost-Based Query Optimization Example to Boost Efficiency","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg","datePublished":"2021-08-10T07:00:34+00:00","dateModified":"2025-06-14T01:43:33+00:00","description":"Cost-based optimization for Couchbase queries is ready for general availability with Couchbase 7.0. Take a closer look at the new CBO features.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2020\/11\/cost-based-optimization-database-queries-couchbase.jpg","width":1200,"height":628,"caption":"Learn how cost-based optimization in the Couchbase 7.0 release improves database query efficiency"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/cost-based-optimization-with-couchbase-json-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Boost Query Efficiency with Cost-Based Optimization in Couchbase"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63","name":"Binh Le","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/5b68c37e30928a9d7b2c8470b1a303b7","url":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","caption":"Binh Le"},"description":"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Cloud Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK.","url":"https:\/\/www.couchbase.com\/blog\/author\/binh-le-2\/"}]}},"authors":[{"term_id":8919,"user_id":26326,"is_guest":0,"slug":"binh-le-2","display_name":"Binh Le","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/a939f48df6447844a8780bec264bb3be21d589336f3915fabc557075a68fa374?s=96&d=mm&r=g","author_category":"","last_name":"Le","first_name":"Binh","job_title":"","user_url":"","description":"Binh Le is a Principal Product Manager for Couchbase Query service. Prior to Couchbase, he worked at Oracle and led the product management team for Sales Clould Analytics and CRM OnDemand. Binh holds a Bachelor's Degree in Computer Science from the University of Brighton, UK."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/9765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/26326"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=9765"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/9765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/11680"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=9765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=9765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=9765"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=9765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}