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

What's hot (20)

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

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

PPTX
Understanding N1QL Optimizer to Tune Queries
Keshav Murthy
 
PPTX
N1QL workshop: Indexing & Query turning.
Keshav Murthy
 
PPTX
Tuning N1QL Query Performance with Couchbase Server 4.0
Cihan Biyikoglu
 
PPTX
Tuning for Performance: indexes & Queries
Keshav Murthy
 
PDF
N1QL New Features in couchbase 7.0
Keshav Murthy
 
PPTX
Deep dive into N1QL: SQL for JSON: Internals and power features.
Keshav Murthy
 
PPTX
Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...
Data Con LA
 
PDF
NoSQL's biggest lie: SQL never went away - Martin Esmann
distributed matters
 
PPTX
Querying NoSQL with SQL - MIGANG - July 2017
Matthew Groves
 
PPTX
Couchbase Tutorial: Big data Open Source Systems: VLDB2018
Keshav Murthy
 
PPTX
NoSQL’s biggest secret: NoSQL never went away
Codemotion
 
PPTX
Putting the SQL Back in NoSQL - October 2022 - All Things Open
Matthew Groves
 
PPTX
Global Secondary Indexes in Couchbase Server 4.0 - JUNE 2015
Cihan Biyikoglu
 
PPTX
Query in Couchbase. N1QL: SQL for JSON
Keshav Murthy
 
PPTX
No sq ls-biggest-lie_sql-never-went-away_martin-esmann
Martin Esmann
 
PPTX
Querying NoSQL with SQL - KCDC - August 2017
Matthew Groves
 
PDF
Postgres Performance for Humans
Citus Data
 
PPTX
Querying NoSQL with SQL: HAVING Your JSON Cake and SELECTing it too
All Things Open
 
KEY
Advanced querying
strmpnk
 
PPTX
Couchbase Data Platform | Big Data Demystified
Omid Vahdaty
 
Understanding N1QL Optimizer to Tune Queries
Keshav Murthy
 
N1QL workshop: Indexing & Query turning.
Keshav Murthy
 
Tuning N1QL Query Performance with Couchbase Server 4.0
Cihan Biyikoglu
 
Tuning for Performance: indexes & Queries
Keshav Murthy
 
N1QL New Features in couchbase 7.0
Keshav Murthy
 
Deep dive into N1QL: SQL for JSON: Internals and power features.
Keshav Murthy
 
Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...
Data Con LA
 
NoSQL's biggest lie: SQL never went away - Martin Esmann
distributed matters
 
Querying NoSQL with SQL - MIGANG - July 2017
Matthew Groves
 
Couchbase Tutorial: Big data Open Source Systems: VLDB2018
Keshav Murthy
 
NoSQL’s biggest secret: NoSQL never went away
Codemotion
 
Putting the SQL Back in NoSQL - October 2022 - All Things Open
Matthew Groves
 
Global Secondary Indexes in Couchbase Server 4.0 - JUNE 2015
Cihan Biyikoglu
 
Query in Couchbase. N1QL: SQL for JSON
Keshav Murthy
 
No sq ls-biggest-lie_sql-never-went-away_martin-esmann
Martin Esmann
 
Querying NoSQL with SQL - KCDC - August 2017
Matthew Groves
 
Postgres Performance for Humans
Citus Data
 
Querying NoSQL with SQL: HAVING Your JSON Cake and SELECTing it too
All Things Open
 
Advanced querying
strmpnk
 
Couchbase Data Platform | Big Data Demystified
Omid Vahdaty
 
Ad

More from Keshav Murthy (16)

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

Recently uploaded (20)

PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 

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