SlideShare a Scribd company logo
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
N1QL QUERY
OPTIMIZER AND
IMPROVMENTS IN 5.0
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
AGENDA
01/
02
03
Optimizer Overview
Improvements in 5.0
Q&A
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
1 OPTIMIZER
OVERVIEW
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 4
Query Execution Flow
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 5
Query Service
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 6
Query Execution Phases
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 7
Optimizer
• Query Rewrite
• N1QL does very limited rewrite.
• Access Path Selection
• KeyScan Access
• IndexScan Access
• PrimaryScan Access
• JOIN ORDER, Types and Methods
• The keyspaces specified in the FROM clause are joined in the exact order given in the query.
• Nested Loop Join
• LOOK UP JOIN
• Index JOIN
• Execution Plan
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 8
Optimizer
• Optimizer considers all possible ways to execute query and decides best query plan.
• Query plan generated based on rule based optimization
• If index can’t satisfy the query that index will not be chosen.
• If an index scan can be performed, will not perform a full / primary scan.
• Each query block (i.e. SELECT… ) has its own query plan
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 9
Index Selection
• Online indexes
• Only online indexes are considered
• Preferred indexes
• USE INDEX hint is provided the indexes in that list are considered
• Satisfying Index condition
• Partial / filtered indexes that index condition is super set of query predicate are considered
• Satisfying Index keys
• Indexes whose leading keys satisfy query predicate are considered
• Longest satisfying index keys
• Redundancy is eliminated by keeping longest satisfying index keys in same order.
• Index with satisfying keys (a,b,c) is retained over index with satisfying (a,b)
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 10
Access Path Selection
• Key Scan
• If the query contains a USE KEYS clause, no index scan or primary scan is performed. The input document keys are taken directly
from the USE KEYS clause.
• Index Count Scan
• Covering Secondary Scan
• Regular secondary scan -- longest satisfying keys, intersect scan;
• To avoid IntersectScan, provide a hint with USE INDEX.
• UNNEST scan;
• Only array indexes with an index key matching the predicates are used for UNNEST scan.
• Regular primary scan
• If a primary scan is selected, and there is no primary index available, the query errors out.
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 11
Scan Methods
• Covering Primary Scan
• A covering primary scan is a primary scan that does not perform a subsequent document fetch. It is used for queries
that need a full / primary scan and only reference META().id.
SELECT META(t).id FROM `travel-sample` t;
• Regular Primary Scan
• A regular primary scan also performs a subsequent document fetch. It is used for queries that need a full / primary
scan and reference some document data other than META().id.
SELECT META(t).cas FROM `travel-sample` t;
SELECT * FROM `travel-sample` t;
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 12
©2016 Couchbase Inc.
Scan Methods
Covering Secondary Scan
• Each satisfied index with most number of index keys is examined for query coverage
• Shortest covering index will be used.
CREATE INDEX ts_name ON `travel-sample`(country, name) WHERE type = "hotel";
SELECT country, name, type, META().id
FROM `travel-sample`
WHERE type = "hotel" AND country = "United States";
Regular Secondary Scan
• Indexes in with most number of matching index keys are used
• When more than one index are qualified, IntersectScan is used.
• To avoid IntersectScan provide hint with USE INDEX.
SELECT country, name, type, META().id, phone
FROM `travel-sample`
WHERE type = "hotel" AND country = "United States";
12
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 13
©2016 Couchbase Inc.
Scan Methods
UNNEST Scan
• Only array indexes are considered. And only queries with UNNEST clauses are considered
Index Count Scan
• Queries with single projection of COUNT aggregate, NO JOIN’s, GROUP BY is considered
• Chosen Index needs to be covered with single range, exact range will be able to push to indexer and
argument to COUNT needs to be constant or leading key
SELECT COUNT(1)
FROM `travel-sample`
WHERE type = "hotel" AND country = "United States";
13
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
2 IMPROVMENTS IN
5.0
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 15
UnionScan
• OR predicate can use multiple indexes.
• Each Index perform IndexScan and results are merged using
UnionScan.
• Each IndexScan can push variable length of index keys.
• All IndexScan under UnionScan are covered the UnionScan
is covered.
• CREATE INDEX ts_cc ON `travel-sample` (country, city)
WHERE type = "hotel";
• CREATE INDEX ts_n ON `travel-sample` (name) WHERE
type = "hotel";
EXPLAIN SELECT name, country, city
FROM `travel-sample`
WHERE type = "hotel" AND
((country = "United States" AND city = "San Francisco")
OR (name = "White Wolf"));
{ "#operator": "UnionScan",
"scans": [{ "index": "ts_cc",
"spans": [ { "range": [
{ "high": ""United States"",
"inclusion": 3, "low": ""United States"" },
{ "high": ""San Francisco"",
"inclusion": 3, "low": ""San Francisco"" } ]
} ],
},
{ "index": "ts_n",
"spans": [ { "range": [ { "high": ""White
Wolf"", "inclusion": 3, "low": ""White Wolf"" } ] }],
} ]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 16
IntersectScan
• IntersectScan is improved by terminating scans early when
one of the scan completed or limit is reached. Also only
completed scan results are considered as possible
candidates.
• If query has ORDER BY and predicate on the order by
clausesand when possible it uses OrderedIntersectScan.
EXPLAIN
SELECT name, country, city
FROM `travel-sample`
WHERE type = "hotel" AND
country = "United States" AND
city = "San Francisco" AND
name >= "White Wolf"
ORDER BY name;
{ "#operator": "OrderedIntersectScan",
"scans": [ { "index": "ts_n",
"spans": [ {
"range": [ { "inclusion": 1,
"low": ""White Wolf"" } ] } ],
},
{ "index": "ts_cc",
"spans": [ {
"range": [ { "high": ""United States"",
"inclusion": 3, "low": ""United States"" },
{ "high": ""San Francisco"",
"inclusion": 3, "low": ""San Francisco"" } ]
} ],
} ]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 17
Implicit Covering Array Index
• N1QL supports simplified Implicit Covering Array
Index syntax in certain cases where the mandatory
array index-key requirement is relaxed to create a
covering array-index.
• The predicates that can be exactly and completely
pushed to the indexer during the array index scan.
• No false positives
CREATE INDEX ts_r_simple ON `travel-sample` ( DISTINCT
ARRAY v.flight FOR v IN schedule END) WHERE type = "route";
EXPLAIN SELECT meta().id
FROM `travel-sample`
WHERE type = "route" AND
ANY v IN schedule SATISFIES v.flight LIKE 'UA%'
END;
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 18
Stable Scans
Earlier versions IndexScan use to do single range scan
(i.e single Span)
If the query has multiple ranges (i.e. OR, IN, NOT
clauses) N1QL use to do separate IndexScan for each
range.
• This causes Indexer can use different snapshot
for each scan (make it unstable scan)
• Number of IndexScans are higher, result in
increase in index connections.
In 5.0.0 multiple ranges are passed into indexer and
indexer uses same snapshot for all the ranges.
If Explain shows operator IndexScan2, It uses stables
Scans.
EXPLAIN SELECT name, country, city
FROM `travel-sample`
WHERE type = "hotel" AND
country IN ["United States" , "France"];
{ "#operator": "IndexScan2",
"index": "ts_cc",
"spans": [
{ "range": [ { "high": ""France"",
"inclusion": 3,
"low": ""France""
}]
},
{ "range": [ { "high": ""United States"",
"inclusion": 3,
"low": ""United States""
}]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 19
Efficiently Pushdown Composite Filters
• Earlier versions composite Index the spans that
pushed to indexer contains single range for all
composite keys together.
• Indexer will not applying range for each part of the
key separately. This may result in lot of false
positives.
• In 5.0.0 with IndexScan2 each index key range
separately pushed and indexer will apply keys
separately.
• This results in no/less false positives and aides push
more information to indexer.
EXPLAIN SELECT name, country, city
FROM `travel-sample`
WHERE type = "hotel" AND
country >= "United States" AND
city = "San Francisco";
{ "#operator": "IndexScan2",
"index": "ts_cc",
"spans": [
{ "range": [ {"inclusion": 1,
"low": ""United States""
},
{ "high": ""San Francisco"",
"inclusion": 3,
"low": ""San Francisco""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 20
Pagination (ORDER, OFFSET, LIMIT)
• Pagination queries can contain any combination of
ORDER, LIMIT, OFFSET clauses.
• Predicates are completely and exactly pushed to
indexer, by pushing offset, limit to indexer can
improve query performance significantly. If that
happened IndexScan2 section of EXPLAIN will have
limit, offset.
• If query ORDER BY matches index key order query
can exploit index order and avoid sort. If that
happened order operator is not present in the
EXPLAIN.
EXPLAIN SELECT country, city
FROM `travel-sample`
WHERE type = "hotel" AND
country >= "United States"
ORDER BY country, city
OFFSET 1 LIMIT 10;
{ "#operator": "IndexScan2",
"index": "ts_cc",
"limit": "10",
"offset": "1",
"spans": [
{ "range": [ {"inclusion": 1,
"low": ""United States""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 21
DESC Index Collation
• Index can be created with ASC/DESC collation on
each index key
• Query can utilize index collation
CREATE INDEX ts_acc ON `travel-sample` (country DESC,
city ASC) WHERE type = "airline";
EXPLAIN SELECT country, city
FROM `travel-sample`
WHERE type = "airline" AND
country >= "United States"
ORDER BY country DESC , city
OFFSET 1 LIMIT 10;
{ "#operator": "IndexScan2",
"index": "ts_acc",
"limit": "10",
"offset": "1",
"spans": [
{ "range": [ {"inclusion": 1,
"low": ""United States""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 22
MAX pushdown
• If the MAX arguments matched with Index leading
key exploit index order for MAX.
• MAX can only be use DESC on leading index key.
• MIN can only be use ASC on leading index key.
• If pushdown happens "limit: 1 will appear in
IndexScan2 section of the EXPLAIN.
CREATE INDEX ts_acc ON `travel-sample` (country DESC,
city ASC) WHERE type = "airline";
EXPLAIN SELECT MAX(country)
FROM `travel-sample`
WHERE type = "airline" AND
country >= "United States";
{ "#operator": "IndexScan2",
"index": "ts_acc",
"limit": "1",
"spans": [
{ "range": [ {"inclusion": 1,
"low": ""United States""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 23
COUNT (DISTINCT expr)
• If the expr matched with Index leading key, COUNT
DISTINCT can be pushed to indexer
• Complete predicates needs to pushed to indexer exactly
• No false positives are possible
• No group or JOIN
• Only single projection
• When pushdown IndexCountDistinctScan2 will
appear in EXPLAIN
EXPLAIN SELECT COUNT( DISTINCT country)
FROM `travel-sample`
WHERE type = "hotel" AND
country >= "United States";
{
"#operator": "IndexCountDistinctScan2"
"index": "ts_cc",
"spans": [
{ "range": [ {"inclusion": 1,
"low": ""United States""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 24
Index Projection
• The index can have many keys but query might be
interested only subset of keys.
• By only requesting required information from indexer
can save lot of network transportation, memory, cpu,
backfill etc. All this can help in performance and
scaling the cluster.
• The requested information can be found in
"IndexScan2" Section of EXPLAIN as
"index_projection"
"index_projection": {
"entry_keys": [ xxx,....... ]
"primary_key": true
}
EXPLAIN SELECT country FROM `travel-sample`
WHERE type = "hotel" AND country >= "United
States";
"index_projection": { "entry_keys": [ 0 ] }
EXPLAIN SELECT country,city FROM `travel-sample`
WHERE type = "hotel" AND country >= "United
States" ;
"index_projection": { "entry_keys": [ 0 ,1] }
EXPLAIN SELECT country,city, META().id FROM `travel-
sample`
WHERE type = "hotel" AND country >= "United
States" ;
"index_projection": { "entry_keys": [ 0 ,1], "primary_key":true }
EXPLAIN SELECT country,city, META().id, name
FROM `travel-sample`
WHERE type = "hotel" AND country >= "United
States" ;
non covered query
"index_projection": {"primary_key":true }
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 25
Index Cas and Expiration
• META().cas, META().expiration can be indexed and
used in queries.
• Note: META().expiration will work in covered queries.
For non covered queries it gives 0
CREATE INDEX ts_cas ON `travel-sample` (country,
META().cas, META().expiration) WHERE type = "airport";
EXPLAIN SELECT country, META().cas, META().expiration
FROM `travel-sample`
WHERE type = "airport" AND country = "United
States";
{
"#operator": "IndexScan2"
"index": "ts_cas",
"spans": [
{ "range": [ { "high": ""United States""
"inclusion": 3,
"low": ""United States""
}
]
}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
3 Q&A
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved.
THANK
YOU

More Related Content

PDF
Couchbase 5.5: N1QL and Indexing features
PPTX
Couchbase Query Workbench Enhancements By Eben Haber
PPTX
Couchbase N1QL: Language & Architecture Overview.
PPTX
Mindmap: Oracle to Couchbase for developers
PPTX
N1QL+GSI: Language and Performance Improvements in Couchbase 5.0 and 5.5
PPTX
Couchbase N1QL: Index Advisor
PPTX
N1QL: What's new in Couchbase 5.0
PDF
Real-Time Analytics with Solr: Presented by Yonik Seeley, Cloudera
Couchbase 5.5: N1QL and Indexing features
Couchbase Query Workbench Enhancements By Eben Haber
Couchbase N1QL: Language & Architecture Overview.
Mindmap: Oracle to Couchbase for developers
N1QL+GSI: Language and Performance Improvements in Couchbase 5.0 and 5.5
Couchbase N1QL: Index Advisor
N1QL: What's new in Couchbase 5.0
Real-Time Analytics with Solr: Presented by Yonik Seeley, Cloudera

What's hot (20)

PDF
Webinar: What's New in Solr 6
PDF
Parallel SQL and Analytics with Solr: Presented by Yonik Seeley, Cloudera
PPTX
Native Code, Off-Heap Data & JSON Facet API for Solr (Heliosearch)
PDF
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
PPTX
ElasticSearch AJUG 2013
PDF
Simple search with elastic search
PDF
Simple Fuzzy Name Matching in Solr: Presented by Chris Mack, Basis Technology
PDF
Data Science with Solr and Spark
PDF
Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...
PPT
How to integrate Splunk with any data solution
PDF
Introduction to Elasticsearch
PDF
Elasticsearch in 15 minutes
PDF
Cassandra 3 new features 2016
PPTX
Solr 6 Feature Preview
PDF
Creating New Streams: Presented by Dennis Gove, Bloomberg LP
ODP
Query DSL In Elasticsearch
PDF
Solr vs. Elasticsearch, Case by Case: Presented by Alexandre Rafalovitch, UN
PDF
Data Exploration with Apache Drill: Day 1
PDF
Retrieving Information From Solr
PDF
Introduction to Elasticsearch
Webinar: What's New in Solr 6
Parallel SQL and Analytics with Solr: Presented by Yonik Seeley, Cloudera
Native Code, Off-Heap Data & JSON Facet API for Solr (Heliosearch)
Streaming Aggregation in Solr - New Horizons for Search: Presented by Erick E...
ElasticSearch AJUG 2013
Simple search with elastic search
Simple Fuzzy Name Matching in Solr: Presented by Chris Mack, Basis Technology
Data Science with Solr and Spark
Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...
How to integrate Splunk with any data solution
Introduction to Elasticsearch
Elasticsearch in 15 minutes
Cassandra 3 new features 2016
Solr 6 Feature Preview
Creating New Streams: Presented by Dennis Gove, Bloomberg LP
Query DSL In Elasticsearch
Solr vs. Elasticsearch, Case by Case: Presented by Alexandre Rafalovitch, UN
Data Exploration with Apache Drill: Day 1
Retrieving Information From Solr
Introduction to Elasticsearch
Ad

Similar to N1QL: Query Optimizer Improvements in Couchbase 5.0. By, Sitaram Vemulapalli (20)

PPTX
N1QL workshop: Indexing & Query turning.
PPTX
Understanding N1QL Optimizer to Tune Queries
PDF
SplunkSummit 2015 - A Quick Guide to Search Optimization
PPTX
Tuning for Performance: indexes & Queries
PDF
OQL querying and indexes with Apache Geode (incubating)
PPTX
State of Florida Neo4j Graph Briefing - Cyber IAM
PPTX
Cassandra Tools and Distributed Administration (Jeffrey Berger, Knewton) | C*...
PDF
MySQL 8.0 Optimizer Guide
PPTX
Using Compass to Diagnose Performance Problems
PPTX
Using Compass to Diagnose Performance Problems in Your Cluster
PDF
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
PDF
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
PPTX
Test strategy utilising mc useful tools
PDF
Introducing MongoDB Stitch, Backend-as-a-Service from MongoDB
PPTX
Data saturday malta - ADX Azure Data Explorer overview
PPTX
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
PPTX
Saltconf16 william-cannon b
PPT
TechTalk #13 Grokking: Marrying Elasticsearch with NLP to solve real-world se...
PDF
Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...
N1QL workshop: Indexing & Query turning.
Understanding N1QL Optimizer to Tune Queries
SplunkSummit 2015 - A Quick Guide to Search Optimization
Tuning for Performance: indexes & Queries
OQL querying and indexes with Apache Geode (incubating)
State of Florida Neo4j Graph Briefing - Cyber IAM
Cassandra Tools and Distributed Administration (Jeffrey Berger, Knewton) | C*...
MySQL 8.0 Optimizer Guide
Using Compass to Diagnose Performance Problems
Using Compass to Diagnose Performance Problems in Your Cluster
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Test strategy utilising mc useful tools
Introducing MongoDB Stitch, Backend-as-a-Service from MongoDB
Data saturday malta - ADX Azure Data Explorer overview
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
Saltconf16 william-cannon b
TechTalk #13 Grokking: Marrying Elasticsearch with NLP to solve real-world se...
Scaling-MongoDB-with-Horizontal-and-Vertical-Sharding Mydbops Opensource Data...
Ad

More from Keshav Murthy (20)

PDF
N1QL New Features in couchbase 7.0
PPTX
Couchbase Tutorial: Big data Open Source Systems: VLDB2018
PPTX
XLDB Lightning Talk: Databases for an Engaged World: Requirements and Design...
PPTX
From SQL to NoSQL: Structured Querying for JSON
PPTX
Utilizing Arrays: Modeling, Querying and Indexing
PPTX
Extended JOIN in Couchbase Server 4.5
PPTX
Bringing SQL to NoSQL: Rich, Declarative Query for NoSQL
PPTX
Query in Couchbase. N1QL: SQL for JSON
PPTX
SQL for JSON: Rich, Declarative Querying for NoSQL Databases and Applications 
PPTX
Introducing N1QL: New SQL Based Query Language for JSON
PPTX
Enterprise Architect's view of Couchbase 4.0 with N1QL
PPTX
Deep dive into N1QL: SQL for JSON: Internals and power features.
PPTX
Drilling on JSON
PPTX
Accelerating analytics on the Sensor and IoT Data.
PPTX
You know what iMEAN? Using MEAN stack for application dev on Informix
PPT
Informix SQL & NoSQL: Putting it all together
PPT
Informix SQL & NoSQL -- for Chat with the labs on 4/22
PDF
NoSQL Deepdive - with Informix NoSQL. IOD 2013
PDF
Informix NoSQL & Hybrid SQL detailed deep dive
PPT
Table for two? Hybrid approach to developing combined SQL, NoSQL applications...
N1QL New Features in couchbase 7.0
Couchbase Tutorial: Big data Open Source Systems: VLDB2018
XLDB Lightning Talk: Databases for an Engaged World: Requirements and Design...
From SQL to NoSQL: Structured Querying for JSON
Utilizing Arrays: Modeling, Querying and Indexing
Extended JOIN in Couchbase Server 4.5
Bringing SQL to NoSQL: Rich, Declarative Query for NoSQL
Query in Couchbase. N1QL: SQL for JSON
SQL for JSON: Rich, Declarative Querying for NoSQL Databases and Applications 
Introducing N1QL: New SQL Based Query Language for JSON
Enterprise Architect's view of Couchbase 4.0 with N1QL
Deep dive into N1QL: SQL for JSON: Internals and power features.
Drilling on JSON
Accelerating analytics on the Sensor and IoT Data.
You know what iMEAN? Using MEAN stack for application dev on Informix
Informix SQL & NoSQL: Putting it all together
Informix SQL & NoSQL -- for Chat with the labs on 4/22
NoSQL Deepdive - with Informix NoSQL. IOD 2013
Informix NoSQL & Hybrid SQL detailed deep dive
Table for two? Hybrid approach to developing combined SQL, NoSQL applications...

Recently uploaded (20)

PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administraation Chapter 3
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
L1 - Introduction to python Backend.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Introduction to Artificial Intelligence
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
System and Network Administration Chapter 2
PDF
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administraation Chapter 3
Reimagine Home Health with the Power of Agentic AI​
L1 - Introduction to python Backend.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Choose the Right IT Partner for Your Business in Malaysia
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Operating system designcfffgfgggggggvggggggggg
Design an Analysis of Algorithms II-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
How to Migrate SBCGlobal Email to Yahoo Easily
How Creative Agencies Leverage Project Management Software.pdf
PTS Company Brochure 2025 (1).pdf.......
Introduction to Artificial Intelligence
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
VVF-Customer-Presentation2025-Ver1.9.pptx
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
System and Network Administration Chapter 2
Nekopoi APK 2025 free lastest update

N1QL: Query Optimizer Improvements in Couchbase 5.0. By, Sitaram Vemulapalli

  • 1. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. N1QL QUERY OPTIMIZER AND IMPROVMENTS IN 5.0
  • 2. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. AGENDA 01/ 02 03 Optimizer Overview Improvements in 5.0 Q&A
  • 3. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 1 OPTIMIZER OVERVIEW
  • 4. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 4 Query Execution Flow
  • 5. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 5 Query Service
  • 6. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 6 Query Execution Phases
  • 7. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 7 Optimizer • Query Rewrite • N1QL does very limited rewrite. • Access Path Selection • KeyScan Access • IndexScan Access • PrimaryScan Access • JOIN ORDER, Types and Methods • The keyspaces specified in the FROM clause are joined in the exact order given in the query. • Nested Loop Join • LOOK UP JOIN • Index JOIN • Execution Plan
  • 8. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 8 Optimizer • Optimizer considers all possible ways to execute query and decides best query plan. • Query plan generated based on rule based optimization • If index can’t satisfy the query that index will not be chosen. • If an index scan can be performed, will not perform a full / primary scan. • Each query block (i.e. SELECT… ) has its own query plan
  • 9. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 9 Index Selection • Online indexes • Only online indexes are considered • Preferred indexes • USE INDEX hint is provided the indexes in that list are considered • Satisfying Index condition • Partial / filtered indexes that index condition is super set of query predicate are considered • Satisfying Index keys • Indexes whose leading keys satisfy query predicate are considered • Longest satisfying index keys • Redundancy is eliminated by keeping longest satisfying index keys in same order. • Index with satisfying keys (a,b,c) is retained over index with satisfying (a,b)
  • 10. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 10 Access Path Selection • Key Scan • If the query contains a USE KEYS clause, no index scan or primary scan is performed. The input document keys are taken directly from the USE KEYS clause. • Index Count Scan • Covering Secondary Scan • Regular secondary scan -- longest satisfying keys, intersect scan; • To avoid IntersectScan, provide a hint with USE INDEX. • UNNEST scan; • Only array indexes with an index key matching the predicates are used for UNNEST scan. • Regular primary scan • If a primary scan is selected, and there is no primary index available, the query errors out.
  • 11. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 11 Scan Methods • Covering Primary Scan • A covering primary scan is a primary scan that does not perform a subsequent document fetch. It is used for queries that need a full / primary scan and only reference META().id. SELECT META(t).id FROM `travel-sample` t; • Regular Primary Scan • A regular primary scan also performs a subsequent document fetch. It is used for queries that need a full / primary scan and reference some document data other than META().id. SELECT META(t).cas FROM `travel-sample` t; SELECT * FROM `travel-sample` t;
  • 12. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 12 ©2016 Couchbase Inc. Scan Methods Covering Secondary Scan • Each satisfied index with most number of index keys is examined for query coverage • Shortest covering index will be used. CREATE INDEX ts_name ON `travel-sample`(country, name) WHERE type = "hotel"; SELECT country, name, type, META().id FROM `travel-sample` WHERE type = "hotel" AND country = "United States"; Regular Secondary Scan • Indexes in with most number of matching index keys are used • When more than one index are qualified, IntersectScan is used. • To avoid IntersectScan provide hint with USE INDEX. SELECT country, name, type, META().id, phone FROM `travel-sample` WHERE type = "hotel" AND country = "United States"; 12
  • 13. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 13 ©2016 Couchbase Inc. Scan Methods UNNEST Scan • Only array indexes are considered. And only queries with UNNEST clauses are considered Index Count Scan • Queries with single projection of COUNT aggregate, NO JOIN’s, GROUP BY is considered • Chosen Index needs to be covered with single range, exact range will be able to push to indexer and argument to COUNT needs to be constant or leading key SELECT COUNT(1) FROM `travel-sample` WHERE type = "hotel" AND country = "United States"; 13
  • 14. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 2 IMPROVMENTS IN 5.0
  • 15. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 15 UnionScan • OR predicate can use multiple indexes. • Each Index perform IndexScan and results are merged using UnionScan. • Each IndexScan can push variable length of index keys. • All IndexScan under UnionScan are covered the UnionScan is covered. • CREATE INDEX ts_cc ON `travel-sample` (country, city) WHERE type = "hotel"; • CREATE INDEX ts_n ON `travel-sample` (name) WHERE type = "hotel"; EXPLAIN SELECT name, country, city FROM `travel-sample` WHERE type = "hotel" AND ((country = "United States" AND city = "San Francisco") OR (name = "White Wolf")); { "#operator": "UnionScan", "scans": [{ "index": "ts_cc", "spans": [ { "range": [ { "high": ""United States"", "inclusion": 3, "low": ""United States"" }, { "high": ""San Francisco"", "inclusion": 3, "low": ""San Francisco"" } ] } ], }, { "index": "ts_n", "spans": [ { "range": [ { "high": ""White Wolf"", "inclusion": 3, "low": ""White Wolf"" } ] }], } ] }
  • 16. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 16 IntersectScan • IntersectScan is improved by terminating scans early when one of the scan completed or limit is reached. Also only completed scan results are considered as possible candidates. • If query has ORDER BY and predicate on the order by clausesand when possible it uses OrderedIntersectScan. EXPLAIN SELECT name, country, city FROM `travel-sample` WHERE type = "hotel" AND country = "United States" AND city = "San Francisco" AND name >= "White Wolf" ORDER BY name; { "#operator": "OrderedIntersectScan", "scans": [ { "index": "ts_n", "spans": [ { "range": [ { "inclusion": 1, "low": ""White Wolf"" } ] } ], }, { "index": "ts_cc", "spans": [ { "range": [ { "high": ""United States"", "inclusion": 3, "low": ""United States"" }, { "high": ""San Francisco"", "inclusion": 3, "low": ""San Francisco"" } ] } ], } ] }
  • 17. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 17 Implicit Covering Array Index • N1QL supports simplified Implicit Covering Array Index syntax in certain cases where the mandatory array index-key requirement is relaxed to create a covering array-index. • The predicates that can be exactly and completely pushed to the indexer during the array index scan. • No false positives CREATE INDEX ts_r_simple ON `travel-sample` ( DISTINCT ARRAY v.flight FOR v IN schedule END) WHERE type = "route"; EXPLAIN SELECT meta().id FROM `travel-sample` WHERE type = "route" AND ANY v IN schedule SATISFIES v.flight LIKE 'UA%' END;
  • 18. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 18 Stable Scans Earlier versions IndexScan use to do single range scan (i.e single Span) If the query has multiple ranges (i.e. OR, IN, NOT clauses) N1QL use to do separate IndexScan for each range. • This causes Indexer can use different snapshot for each scan (make it unstable scan) • Number of IndexScans are higher, result in increase in index connections. In 5.0.0 multiple ranges are passed into indexer and indexer uses same snapshot for all the ranges. If Explain shows operator IndexScan2, It uses stables Scans. EXPLAIN SELECT name, country, city FROM `travel-sample` WHERE type = "hotel" AND country IN ["United States" , "France"]; { "#operator": "IndexScan2", "index": "ts_cc", "spans": [ { "range": [ { "high": ""France"", "inclusion": 3, "low": ""France"" }] }, { "range": [ { "high": ""United States"", "inclusion": 3, "low": ""United States"" }] } ] }
  • 19. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 19 Efficiently Pushdown Composite Filters • Earlier versions composite Index the spans that pushed to indexer contains single range for all composite keys together. • Indexer will not applying range for each part of the key separately. This may result in lot of false positives. • In 5.0.0 with IndexScan2 each index key range separately pushed and indexer will apply keys separately. • This results in no/less false positives and aides push more information to indexer. EXPLAIN SELECT name, country, city FROM `travel-sample` WHERE type = "hotel" AND country >= "United States" AND city = "San Francisco"; { "#operator": "IndexScan2", "index": "ts_cc", "spans": [ { "range": [ {"inclusion": 1, "low": ""United States"" }, { "high": ""San Francisco"", "inclusion": 3, "low": ""San Francisco"" } ] } ] }
  • 20. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 20 Pagination (ORDER, OFFSET, LIMIT) • Pagination queries can contain any combination of ORDER, LIMIT, OFFSET clauses. • Predicates are completely and exactly pushed to indexer, by pushing offset, limit to indexer can improve query performance significantly. If that happened IndexScan2 section of EXPLAIN will have limit, offset. • If query ORDER BY matches index key order query can exploit index order and avoid sort. If that happened order operator is not present in the EXPLAIN. EXPLAIN SELECT country, city FROM `travel-sample` WHERE type = "hotel" AND country >= "United States" ORDER BY country, city OFFSET 1 LIMIT 10; { "#operator": "IndexScan2", "index": "ts_cc", "limit": "10", "offset": "1", "spans": [ { "range": [ {"inclusion": 1, "low": ""United States"" } ] } ] }
  • 21. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 21 DESC Index Collation • Index can be created with ASC/DESC collation on each index key • Query can utilize index collation CREATE INDEX ts_acc ON `travel-sample` (country DESC, city ASC) WHERE type = "airline"; EXPLAIN SELECT country, city FROM `travel-sample` WHERE type = "airline" AND country >= "United States" ORDER BY country DESC , city OFFSET 1 LIMIT 10; { "#operator": "IndexScan2", "index": "ts_acc", "limit": "10", "offset": "1", "spans": [ { "range": [ {"inclusion": 1, "low": ""United States"" } ] } ] }
  • 22. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 22 MAX pushdown • If the MAX arguments matched with Index leading key exploit index order for MAX. • MAX can only be use DESC on leading index key. • MIN can only be use ASC on leading index key. • If pushdown happens "limit: 1 will appear in IndexScan2 section of the EXPLAIN. CREATE INDEX ts_acc ON `travel-sample` (country DESC, city ASC) WHERE type = "airline"; EXPLAIN SELECT MAX(country) FROM `travel-sample` WHERE type = "airline" AND country >= "United States"; { "#operator": "IndexScan2", "index": "ts_acc", "limit": "1", "spans": [ { "range": [ {"inclusion": 1, "low": ""United States"" } ] } ] }
  • 23. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 23 COUNT (DISTINCT expr) • If the expr matched with Index leading key, COUNT DISTINCT can be pushed to indexer • Complete predicates needs to pushed to indexer exactly • No false positives are possible • No group or JOIN • Only single projection • When pushdown IndexCountDistinctScan2 will appear in EXPLAIN EXPLAIN SELECT COUNT( DISTINCT country) FROM `travel-sample` WHERE type = "hotel" AND country >= "United States"; { "#operator": "IndexCountDistinctScan2" "index": "ts_cc", "spans": [ { "range": [ {"inclusion": 1, "low": ""United States"" } ] } ] }
  • 24. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 24 Index Projection • The index can have many keys but query might be interested only subset of keys. • By only requesting required information from indexer can save lot of network transportation, memory, cpu, backfill etc. All this can help in performance and scaling the cluster. • The requested information can be found in "IndexScan2" Section of EXPLAIN as "index_projection" "index_projection": { "entry_keys": [ xxx,....... ] "primary_key": true } EXPLAIN SELECT country FROM `travel-sample` WHERE type = "hotel" AND country >= "United States"; "index_projection": { "entry_keys": [ 0 ] } EXPLAIN SELECT country,city FROM `travel-sample` WHERE type = "hotel" AND country >= "United States" ; "index_projection": { "entry_keys": [ 0 ,1] } EXPLAIN SELECT country,city, META().id FROM `travel- sample` WHERE type = "hotel" AND country >= "United States" ; "index_projection": { "entry_keys": [ 0 ,1], "primary_key":true } EXPLAIN SELECT country,city, META().id, name FROM `travel-sample` WHERE type = "hotel" AND country >= "United States" ; non covered query "index_projection": {"primary_key":true }
  • 25. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 25 Index Cas and Expiration • META().cas, META().expiration can be indexed and used in queries. • Note: META().expiration will work in covered queries. For non covered queries it gives 0 CREATE INDEX ts_cas ON `travel-sample` (country, META().cas, META().expiration) WHERE type = "airport"; EXPLAIN SELECT country, META().cas, META().expiration FROM `travel-sample` WHERE type = "airport" AND country = "United States"; { "#operator": "IndexScan2" "index": "ts_cas", "spans": [ { "range": [ { "high": ""United States"" "inclusion": 3, "low": ""United States"" } ] } ] }
  • 26. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 3 Q&A
  • 27. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. THANK YOU