{"id":13392,"date":"2022-06-15T11:58:20","date_gmt":"2022-06-15T18:58:20","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=13392"},"modified":"2025-06-13T17:32:04","modified_gmt":"2025-06-14T00:32:04","slug":"lower-tco-sql-flatten_keys-json-arrays","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/","title":{"rendered":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays"},"content":{"rendered":"<p><span style=\"font-weight: 400\">An RDBMS with a well-established schema serves well when application functions remain static. While this rigid structure can guarantee stability, it doesn&#8217;t lend itself to fast-changing business requirements. Organizations today must consider modernizing their application infrastructure and moving from RDBMS to NoSQL.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This article looks into some benefits of the NoSQL document model, and how data and indexes can be organized to optimize query performance and index configuration. I will also discuss the index strategy that you can consider when migrating your RDBMS data model to the Couchbase NoSQL database, and how the Couchbase 7.1 FLATTEN_KEYS feature can help improve query performance and reduce the number of indexes.<\/span><\/p>\n<p><span style=\"font-weight: 400\">An effective index strategy is one of the most important factors in operating a database. It helps us get the right balance between query performance and resource management. The database should not only efficiently capture data, it should also provide the most optimal access to that data. NoSQL databases are no different from RDBMS when it comes to an effective index strategy.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">An RDBMS use case<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Let&#8217;s consider the <\/span><b>hotel <\/b><span style=\"font-weight: 400\">object in the Couchbase <\/span><b>travel-sample<\/b><span style=\"font-weight: 400\"> dataset and assume that we use the RDBMS model to capture this information. The relational model would look like this:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13393\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/06\/sql-flatten-keys-json-arrays-1.png\" alt=\"Relational model relationships\" width=\"512\" height=\"274\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-1.png 512w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-1-300x161.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-1-20x11.png 20w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p><span style=\"font-weight: 400\">The <\/span><b>hotel<\/b><span style=\"font-weight: 400\"> object has all the detailed information about the <\/span><b>hotel.<\/b><span style=\"font-weight: 400\"> There is also a <\/span><b>hotel_like<\/b> <span style=\"font-weight: 400\">object that records the names of customers who have clicked on <em>like<\/em>\u00a0on the hotel\u2019s social media page. There is also a<\/span><b> hotel_review<\/b><span style=\"font-weight: 400\"> object that records all hotel reviews, including comments and detailed ratings for the different services and amenities.<\/span><\/p>\n<h2><span style=\"font-weight: 400\">Business requirement<\/span><\/h2>\n<p><span style=\"font-weight: 400\">Create a query to allow users to get a summary view of all the hotels in a particular city that they plan to visit. The summary should include the review ratings of each hotel, and how many people have <em>liked<\/em> the hotel. To narrow down the list, the query should focus on the recent reviews (2015 for this data set) and the review ratings should be 4 or above (5 is the highest rating).<\/span><\/p>\n<h3><span style=\"font-weight: 400\">The relational model query<\/span><\/h3>\n<pre class=\"decode-attributes:false lang:default decode:true\">SELECT  h.name hotel,\r\n  count(distinct l.name) liked_by,\u00a0\r\n  count(distinct r.author)\u00a0 review_author,\r\n  avg(r.ratings.Overall) avg_ratings\r\nFROM\u00a0 hotel h\r\n  INNER JOIN hotel_review r ON r.hotel_id = h.id\r\n  INNER JOIN hotel_like l ON l.hotel_id = h.id\r\nWHERE h.city='London'\r\n  AND \u00a0 r.ratings.Overall &gt;= 4\r\n  AND \u00a0 r.date &gt; '2015-01-01'\r\n  GROUP BY h.name\r\n  ORDER BY avg(r.ratings.Overall) DESC<\/pre>\n<h3><span style=\"font-weight: 400\">The index for a relational model query<\/span><\/h3>\n<p><b>hotel<\/b><span style=\"font-weight: 400\">:\u00a0 Since there is a filter on city and name, an index should exist. Note that the hotel <\/span><b>id<\/b><span style=\"font-weight: 400\"> is appended to the index because it can help with the JOIN.<\/span><\/p>\n<pre class=\"expand-toggle:false decode-attributes:false lang:default decode:true\" style=\"padding-left: 40px\">CREATE INDEX adv_city_name_id ON `hotel`(`city`,`name`,`id`)<\/pre>\n<p><b>hotel_review<\/b><span style=\"font-weight: 400\">: There are two filters on the hotel reviews: the review date and the review ratings. There is also a need to eliminate double counting, because there is no explicit relationship between <\/span><b>hotel_like <\/b><span style=\"font-weight: 400\">and <\/span><b>hotel_review<\/b><span style=\"font-weight: 400\">, so the review author is added here for that purpose. The <em>hotel_id<\/em> field is also added to the index because it is the foreign key and can help with the <em>JOIN<\/em>.<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\" style=\"padding-left: 40px\">CREATE INDEX adv_ratings_Overall_date_hotel_id_author ON `hotel_review`(`ratings`.`Overall`,`date`,`hotel_id`,`author`)<\/pre>\n<p><b>hotel_review<\/b><span style=\"font-weight: 400\">: While there are no filters on <\/span><b>hotel_like<\/b><span style=\"font-weight: 400\">, there is a need to eliminate double counting of likes, because there is no relationship between review and like. The <em>hotel_id <\/em>field is added to the index because it is the foreign key and can help with the JOIN.<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\" style=\"padding-left: 40px\">CREATE INDEX adv_hotel_id_name ON `hotel_like`(`hotel_id`,`name`)<\/pre>\n<h3><span style=\"font-weight: 400\">The execution plan for the relational model query<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-13394\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/06\/sql-flatten-keys-json-arrays-2-1024x469.png\" alt=\"Execution plan for relation model JSON query\" width=\"900\" height=\"412\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-1024x469.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-300x137.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-768x352.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-1536x704.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-20x9.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2-1320x605.png 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-2.png 1999w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p><span style=\"font-weight: 400\">Notice the need to perform:<\/span><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Two JOINs<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Three index scans (one for each object)<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2><span style=\"font-weight: 400\">The Document Model view<\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13396\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/06\/sql-flatten-keys-json-arrays-4.png\" alt=\"Document model view\" width=\"344\" height=\"392\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-4.png 344w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-4-263x300.png 263w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-4-300x342.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-4-18x20.png 18w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400\">For the document model, both the <\/span><b>hotel_like <\/b><span style=\"font-weight: 400\">and the <\/span><b>hotel_review <\/b><span style=\"font-weight: 400\">objects are stored as arrays in the hotel object.\u00a0 There is no strict rule that you should always include child objects as an array in the parent object, but it does make sense to do so in this case because these objects are always accessed together.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"font-weight: 400\">The denormalized document model query<\/span><\/h3>\n<pre class=\"decode-attributes:false lang:default decode:true\">SELECT h.name hotel,\r\n    COUNT(distinct l) liked_by,\r\n    COUNT(distinct r.author)\u00a0 review_author,\r\n    AVG(r.ratings.Overall) avg_ratings\r\n  FROM\u00a0 hotel h\r\n    UNNEST h.reviews AS r\u00a0\r\n    UNNEST\u00a0 h.public_likes AS l\u00a0\r\n  WHERE h.city='London'\r\n    AND \u00a0 r.ratings.Overall &gt;= 0\u00a0\r\n    AND \u00a0 r.date &gt; '2015-01-01'\r\n  GROUP BY h.name\r\n  ORDER BY avg(r.ratings.Overall) DESC<\/pre>\n<h3><span style=\"font-weight: 400\">The index for the denormalized document model<\/span><\/h3>\n<pre class=\"decode-attributes:false lang:default decode:true \">CREATE INDEX adv_ALL_reviews_ratings_Overall_date_city\u00a0\r\n\u00a0ON `hotel`(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ALL ARRAY\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0FLATTEN_KEYS(`r`.`ratings`.`Overall`,`r`.`date` DESC)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0FOR r IN `reviews` END,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0`city`)<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400\">A few points to note:<\/span><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The <a href=\"https:\/\/www.couchbase.com\/sqlplusplus\/\">SQL++<\/a> query references a single <\/span><b>hotel<\/b><span style=\"font-weight: 400\"> object, so there is no need to perform any explicit JOIN between the parent <\/span><b>hotel<\/b> <span style=\"font-weight: 400\">object and the child <\/span><b>like <\/b><span style=\"font-weight: 400\">or r<\/span><b>eview<\/b><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The query uses a single index that covers all the query predicates, i.e. the <\/span><b>hotel.city,<\/b><span style=\"font-weight: 400\"> the <\/span><b>reviews.ratings<\/b><span style=\"font-weight: 400\">, and the <\/span><b>reviews.date<\/b><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">When an array is indexed, the index can only be on a single key. FLATTEN_KEYS() allows composite fields from the array, thus allowing the predicates to be on multiple array fields.<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h3><span style=\"font-weight: 400\">The execution plan for the document model query<\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-13397\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/06\/sql-flatten-keys-json-arrays-5-1024x162.png\" alt=\"execution plan for the document model query\" width=\"900\" height=\"142\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-1024x162.png 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-300x47.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-768x121.png 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-1536x243.png 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-20x3.png 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5-1320x209.png 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-5.png 1999w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p><span style=\"font-weight: 400\">Notice the need to perform<\/span><\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Two UNNEST (no JOINs are needed as it is in single document)<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Only ONE index scan\u00a0<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-weight: 400\">Summary<\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13395\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/06\/sql-flatten-keys-json-arrays-3.png\" alt=\"Relational model converted to document model\" width=\"560\" height=\"194\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-3.png 560w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-3-300x104.png 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/06\/sql-flatten-keys-json-arrays-3-20x7.png 20w\" sizes=\"auto, (max-width: 560px) 100vw, 560px\" \/><\/p>\n<p><span style=\"font-weight: 400\">When a relational model is denormalized into a single object in the document model:<\/span><\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"list-style-type: none\">\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The SQL++ query is simpler because there is no need to perform any JOINs<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Multiple relational model indexes could be combined into a single document model<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">The Couchbase 7.1 FLATTEN_KEYS feature combines multiple array element predicates into a single index.<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>This is just one of the new features we have delivered recently &#8211; read more about <a href=\"https:\/\/www.couchbase.com\/blog\/whats-new-in-couchbase-server-7-1\/\" target=\"_blank\" rel=\"noopener\">What&#8217;s New in Couchbase Server 7.1<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An RDBMS with a well-established schema serves well when application functions remain static. While this rigid structure can guarantee stability, it doesn&#8217;t lend itself to fast-changing business requirements. Organizations today must consider modernizing their application infrastructure and moving from RDBMS [&hellip;]<\/p>\n","protected":false},"author":26326,"featured_media":11334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1816,1812],"tags":[9632,9654],"ppma_author":[8919],"class_list":["post-13392","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-couchbase-server","category-n1ql-query","tag-database-tco","tag-flatten_keys"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.7.1 (Yoast SEO v25.7) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays<\/title>\n<meta name=\"description\" content=\"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.\" \/>\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\/lower-tco-sql-flatten_keys-json-arrays\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays\" \/>\n<meta property=\"og:description\" content=\"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-15T18:58:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T00:32:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1575\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\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: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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\"},\"author\":{\"name\":\"Binh Le\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63\"},\"headline\":\"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays\",\"datePublished\":\"2022-06-15T18:58:20+00:00\",\"dateModified\":\"2025-06-14T00:32:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\"},\"wordCount\":806,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg\",\"keywords\":[\"database TCO\",\"flatten_keys\"],\"articleSection\":[\"Couchbase Server\",\"SQL++ \/ N1QL Query\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\",\"name\":\"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg\",\"datePublished\":\"2022-06-15T18:58:20+00:00\",\"dateModified\":\"2025-06-14T00:32:04+00:00\",\"description\":\"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg\",\"width\":1575,\"height\":628,\"caption\":\"Why NoSQL JSON Databases Are So Useful for Developers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays\"}]},{\"@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":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays","description":"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.","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\/lower-tco-sql-flatten_keys-json-arrays\/","og_locale":"en_US","og_type":"article","og_title":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays","og_description":"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.","og_url":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/","og_site_name":"The Couchbase Blog","article_published_time":"2022-06-15T18:58:20+00:00","article_modified_time":"2025-06-14T00:32:04+00:00","og_image":[{"width":1575,"height":628,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg","type":"image\/jpeg"}],"author":"Binh Le","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Binh Le","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/"},"author":{"name":"Binh Le","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/f89064928e262c71eb43bee996c48c63"},"headline":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays","datePublished":"2022-06-15T18:58:20+00:00","dateModified":"2025-06-14T00:32:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/"},"wordCount":806,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg","keywords":["database TCO","flatten_keys"],"articleSection":["Couchbase Server","SQL++ \/ N1QL Query"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/","url":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/","name":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg","datePublished":"2022-06-15T18:58:20+00:00","dateModified":"2025-06-14T00:32:04+00:00","description":"Document database models are incredibly flexible vs. RDBMS. The Couchbase 7.1 FLATTEN_KEYS feature improves query performance and indexing.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/06\/Why-NoSQL-JSON-Databases-Are-So-Usefulblogbanner.jpg","width":1575,"height":628,"caption":"Why NoSQL JSON Databases Are So Useful for Developers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/lower-tco-sql-flatten_keys-json-arrays\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Lower TCO With SQL++ FLATTEN_KEYS For JSON Arrays"}]},{"@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\/13392","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=13392"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/13392\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/11334"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=13392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=13392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=13392"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=13392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}