SlideShare una empresa de Scribd logo
1 de 50
Descargar para leer sin conexión
Tuning Cypher
Mark Needham @markhneedham
Petra Selmer@Aethelraed
Why do we need to tune?
‣ No query planner is ever perfect
‣ You know your domain better than the
database
2
The planners - the Rule planner
‣ This is the original planner
‣ It consists of rules that use the indexes to
produce the query execution plan
‣ All write queries only use the Rule planner
3
The planners - the Cost planner
‣ This is our new, cost-based planner
‣ Introduced in 2.2.0
‣ It uses the statistics service in Neo4j to
assign costs to various query execution
plans, picking the cheapest one
‣ All read-only queries use this by default
4
Configuring a planner
‣ Read-only queries can still be run with Rule
• Prepend query with CYPHER planner=rule
• Set dbms.cypher.planner to RULE
‣ http://neo4j.com/docs/stable/how-are-queries-executed.html 5
Cypher query execution
‣ http://neo4j.com/docs/snapshot/execution-plans.html
‣ http://neo4j.com/blog/introducing-new-cypher-query-optimizer 6
How do I view a query plan?
‣ EXPLAIN
• shows the execution plan without actually
executing it or returning any results.
‣ PROFILE
• executes the statement and returns the results
along with profiling information.
7
Neo4j’s longest plan (so far…)
8
Neo4j’s longest plan (so far…)
9
Neo4j’s longest plan (so far…)
10
What is our goal?
At a high level, the goal is
simple: get the number of
db hits down.
11
an abstract unit of storage
engine work.
What is a database hit?
“
”
12
‣ Operators to look out for
• All nodes scan expensive
• Label scan cheaper
• Node index seek cheapest
• Node index scan used for range queries
‣ http://neo4j.com/docs/2.3.0-M03/execution-plans.html
Execution plan operators
13
Our data set
14
Finding The Matrix
MATCH (movie {title: "The Matrix"})
RETURN movie
15
Finding The Matrix
MATCH (movie
{title: "The Matrix"})
RETURN movie
16
Tip: Use labels
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
17
Tip: Use labels
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
18
Finding The Matrix
MATCH (movie
{title: "The Matrix"})
RETURN movie
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
19
Tip: Use indexes and constraints
‣ Indexes for non unique values
‣ Constraints for unique values
CREATE INDEX ON :Movie(title)
CREATE INDEX ON :Person(name)
CREATE CONSTRAINT ON (g:Genre)
ASSERT g.name IS UNIQUE
20
How does Neo4j use indexes?
‣ Indexes are only used to find the starting
point for queries.
21
How does Neo4j use indexes?
‣ Indexes are only used to find the starting
point for queries.
Use index scans to look up
rows in tables and join them
with rows from other tables
Use indexes to find the starting
points for a query.
Relational
Graph
22
Tip: Use indexes and constraints
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
23
Finding The Matrix
(no index)
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
(index)
MATCH (movie:Movie
{title: "The Matrix"})
RETURN movie
24
Actors who appeared together
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
RETURN COUNT(*)
25
Actors who appeared together
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
RETURN COUNT(*)
26
Tip: Enforce index usage
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
USING INDEX a:Person(name)
USING INDEX b:Person(name)
RETURN COUNT(*)
27
Tip: Enforce index usage
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
USING INDEX a:Person(name)
USING INDEX b:Person(name)
RETURN COUNT(*)
28
Actors who appeared together
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
RETURN COUNT(*)
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()<-[:ACTS_IN]-
(b:Person {name:"Meg Ryan"})
USING INDEX a:Person(name)
USING INDEX b:Person(name)
RETURN COUNT(*)
29
Tom Hanks’ colleagues’ movies
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-
(coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title
30
Tom Hanks’ colleagues’ movies
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-
(coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title
31
Tip: Reduce cardinality of WIP
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-
(coActor)
WITH DISTINCT coActor
MATCH (coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title 32
Tip: Reduce cardinality of WIP
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-
(coActor)
WITH DISTINCT coActor
MATCH (coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title 33
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-(coActor)
WITH DISTINCT coActor
MATCH (coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title
Tom Hanks’ colleagues’ movies
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m1)<-[:ACTS_IN]-
(coActor)-[:ACTS_IN]->(m2)
RETURN distinct m2.title;
34
Counting number of movies
MATCH (n:Actor)-[:ACTS_IN]->()
RETURN n, COUNT(*) AS count
ORDER BY count DESC
35
Counting number of movies
MATCH (n:Actor)-[:ACTS_IN]->()
RETURN n, COUNT(*) AS count
ORDER BY count DESC
36
Tip: Use `SIZE` for fast counting
MATCH (n:Actor)
RETURN n,
SIZE((n)-[:ACTS_IN]->())
AS count
ORDER BY count DESC
37
Counting number of movies
MATCH (n:Actor)-[:ACTS_IN]->()
RETURN n, COUNT(*) AS count
ORDER BY count DESC
MATCH (n:Actor)
RETURN n,
SIZE((n)-[:ACTS_IN]->()) AS count
ORDER BY count DESC
38
Hints
USING INDEX
• Force the use of a specific index
MATCH (a:Person {name:"Tom Hanks"})
-[:ACTS_IN]->()
USING INDEX a:Person(name)
RETURN count(*)
‣ http://neo4j.com/docs/2.3.0-M03/query-using.html 39
Hints
USING SCAN
• Forces a label scan on lower cardinality labels
MATCH (a:Actor)-->(m:Movie:Comedy)
USING SCAN m:Comedy
RETURN count(distinct a)
40
Even more tips...
Use parameters
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m)
RETURN m.title
42
Use parameters
MATCH (p:Person {name: {name}})
-[:ACTS_IN]->(m)
RETURN m.title
MATCH (p:Person {name:"Tom Hanks"})
-[:ACTS_IN]->(m)
RETURN m.title
43
Avoid Cartesian products
‣ Easy to do this inadvertently:
MATCH (a:Actor), (m:Movie)
RETURN count(a), count(m)
‣ This is correct, and performs better
MATCH (a:Actor)
WITH count(a) as a_count
MATCH (m:Movie)
RETURN a_count, count(m) 44
Watch out for those warnings!
45
Cardinalities
Watch those rows!
46
Only RETURN what you need
‣ This is not recommended:
MATCH (a:Actor)
RETURN a
‣ Use this instead:
MATCH (a:Actor)
RETURN a.name, a.birthdate, a.height
47
Keep it short ‘n sweet
‣ Keep queries as short as possible
• Better to have many, smaller queries than one
larger one
‣ Keep read and write queries separate
• If not, only the RULE planner will be used
48
tl;dr
‣ View query plans with EXPLAIN and PROFILE
‣ Use labels
‣ Index your starting points
‣ Reduce work in progress
‣ Use SIZE for fast relationship counting
‣ Remember the hints
49
Thanks for coming
‣ And don’t forget, if the tips aren’t working
ask us for help on Stack Overflow
Mark Needham @markhneedham
Petra Selmer @Aethelraed
50

Más contenido relacionado

Similar a Graph Connect: Tuning Cypher

GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark Needham
GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark NeedhamGraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark Needham
GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark NeedhamNeo4j
 
SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?Brent Ozar
 
Big data analytics using a custom SQL engine
Big data analytics using a custom SQL engineBig data analytics using a custom SQL engine
Big data analytics using a custom SQL engineAndrew Tsvelodub
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftAmazon Web Services
 
List intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and OptimizationsList intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and OptimizationsSunghwan Kim
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftAmazon Web Services
 
Linear Model Selection and Regularization (Article 6 - Practical exercises)
Linear Model Selection and Regularization (Article 6 - Practical exercises)Linear Model Selection and Regularization (Article 6 - Practical exercises)
Linear Model Selection and Regularization (Article 6 - Practical exercises)Theodore Grammatikopoulos
 
Processing Large Graphs
Processing Large GraphsProcessing Large Graphs
Processing Large GraphsNishant Gandhi
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftAmazon Web Services
 
Design portfolio
Design portfolioDesign portfolio
Design portfolioSam Gupta
 
Hands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4jHands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4jSerendio Inc.
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftAmazon Web Services
 
Tactical data engineering
Tactical data engineeringTactical data engineering
Tactical data engineeringJulian Hyde
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF Loft
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF LoftData Warehousing with Amazon Redshift: Data Analytics Week at the SF Loft
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF LoftAmazon Web Services
 
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxData
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxDataOptimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxData
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxDataInfluxData
 
Lazy Join Optimizations Without Upfront Statistics with Matteo Interlandi
Lazy Join Optimizations Without Upfront Statistics with Matteo InterlandiLazy Join Optimizations Without Upfront Statistics with Matteo Interlandi
Lazy Join Optimizations Without Upfront Statistics with Matteo InterlandiDatabricks
 
Nested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypherNested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypheropenCypher
 

Similar a Graph Connect: Tuning Cypher (20)

GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark Needham
GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark NeedhamGraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark Needham
GraphConnect Europe 2016 - Tuning Your Cypher - Petra Selmer, Mark Needham
 
SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?SQL Query Optimization: Why Is It So Hard to Get Right?
SQL Query Optimization: Why Is It So Hard to Get Right?
 
Big data analytics using a custom SQL engine
Big data analytics using a custom SQL engineBig data analytics using a custom SQL engine
Big data analytics using a custom SQL engine
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon Redshift
 
List intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and OptimizationsList intersection for web search: Algorithms, Cost Models, and Optimizations
List intersection for web search: Algorithms, Cost Models, and Optimizations
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon Redshift
 
Linear Model Selection and Regularization (Article 6 - Practical exercises)
Linear Model Selection and Regularization (Article 6 - Practical exercises)Linear Model Selection and Regularization (Article 6 - Practical exercises)
Linear Model Selection and Regularization (Article 6 - Practical exercises)
 
Processing Large Graphs
Processing Large GraphsProcessing Large Graphs
Processing Large Graphs
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon Redshift
 
Design portfolio
Design portfolioDesign portfolio
Design portfolio
 
Hands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4jHands on Training – Graph Database with Neo4j
Hands on Training – Graph Database with Neo4j
 
Data Warehousing with Amazon Redshift
Data Warehousing with Amazon RedshiftData Warehousing with Amazon Redshift
Data Warehousing with Amazon Redshift
 
Tactical data engineering
Tactical data engineeringTactical data engineering
Tactical data engineering
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF Loft
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF LoftData Warehousing with Amazon Redshift: Data Analytics Week at the SF Loft
Data Warehousing with Amazon Redshift: Data Analytics Week at the SF Loft
 
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxData
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxDataOptimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxData
Optimizing InfluxDB Performance in the Real World | Sam Dillard | InfluxData
 
Lazy Join Optimizations Without Upfront Statistics with Matteo Interlandi
Lazy Join Optimizations Without Upfront Statistics with Matteo InterlandiLazy Join Optimizations Without Upfront Statistics with Matteo Interlandi
Lazy Join Optimizations Without Upfront Statistics with Matteo Interlandi
 
Nested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypherNested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypher
 

Más de Mark Needham

Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and OperationsNeo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and OperationsMark Needham
 
This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018Mark Needham
 
Building a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4jBuilding a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4jMark Needham
 
Graph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyGraph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyMark Needham
 
Graph Connect Europe: From Zero To Import
Graph Connect Europe: From Zero To ImportGraph Connect Europe: From Zero To Import
Graph Connect Europe: From Zero To ImportMark Needham
 
Optimizing cypher queries in neo4j
Optimizing cypher queries in neo4jOptimizing cypher queries in neo4j
Optimizing cypher queries in neo4jMark Needham
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueMark Needham
 
The Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier LeagueThe Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier LeagueMark Needham
 
Scala: An experience report
Scala: An experience reportScala: An experience report
Scala: An experience reportMark Needham
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMark Needham
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mark Needham
 
F#: What I've learnt so far
F#: What I've learnt so farF#: What I've learnt so far
F#: What I've learnt so farMark Needham
 

Más de Mark Needham (14)

Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and OperationsNeo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
Neo4j GraphTour: Utilizing Powerful Extensions for Analytics and Operations
 
This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018This week in Neo4j - 3rd February 2018
This week in Neo4j - 3rd February 2018
 
Building a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4jBuilding a recommendation engine with python and neo4j
Building a recommendation engine with python and neo4j
 
Graph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easilyGraph Connect: Importing data quickly and easily
Graph Connect: Importing data quickly and easily
 
Graph Connect Europe: From Zero To Import
Graph Connect Europe: From Zero To ImportGraph Connect Europe: From Zero To Import
Graph Connect Europe: From Zero To Import
 
Optimizing cypher queries in neo4j
Optimizing cypher queries in neo4jOptimizing cypher queries in neo4j
Optimizing cypher queries in neo4j
 
Football graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier LeagueFootball graph - Neo4j and the Premier League
Football graph - Neo4j and the Premier League
 
The Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier LeagueThe Football Graph - Neo4j and the Premier League
The Football Graph - Neo4j and the Premier League
 
Scala: An experience report
Scala: An experience reportScala: An experience report
Scala: An experience report
 
Visualisations
VisualisationsVisualisations
Visualisations
 
Mixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented languageMixing functional programming approaches in an object oriented language
Mixing functional programming approaches in an object oriented language
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
F#: What I've learnt so far
F#: What I've learnt so farF#: What I've learnt so far
F#: What I've learnt so far
 

Último

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 

Último (20)

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 

Graph Connect: Tuning Cypher

  • 1. Tuning Cypher Mark Needham @markhneedham Petra Selmer@Aethelraed
  • 2. Why do we need to tune? ‣ No query planner is ever perfect ‣ You know your domain better than the database 2
  • 3. The planners - the Rule planner ‣ This is the original planner ‣ It consists of rules that use the indexes to produce the query execution plan ‣ All write queries only use the Rule planner 3
  • 4. The planners - the Cost planner ‣ This is our new, cost-based planner ‣ Introduced in 2.2.0 ‣ It uses the statistics service in Neo4j to assign costs to various query execution plans, picking the cheapest one ‣ All read-only queries use this by default 4
  • 5. Configuring a planner ‣ Read-only queries can still be run with Rule • Prepend query with CYPHER planner=rule • Set dbms.cypher.planner to RULE ‣ http://neo4j.com/docs/stable/how-are-queries-executed.html 5
  • 6. Cypher query execution ‣ http://neo4j.com/docs/snapshot/execution-plans.html ‣ http://neo4j.com/blog/introducing-new-cypher-query-optimizer 6
  • 7. How do I view a query plan? ‣ EXPLAIN • shows the execution plan without actually executing it or returning any results. ‣ PROFILE • executes the statement and returns the results along with profiling information. 7
  • 8. Neo4j’s longest plan (so far…) 8
  • 9. Neo4j’s longest plan (so far…) 9
  • 10. Neo4j’s longest plan (so far…) 10
  • 11. What is our goal? At a high level, the goal is simple: get the number of db hits down. 11
  • 12. an abstract unit of storage engine work. What is a database hit? “ ” 12
  • 13. ‣ Operators to look out for • All nodes scan expensive • Label scan cheaper • Node index seek cheapest • Node index scan used for range queries ‣ http://neo4j.com/docs/2.3.0-M03/execution-plans.html Execution plan operators 13
  • 15. Finding The Matrix MATCH (movie {title: "The Matrix"}) RETURN movie 15
  • 16. Finding The Matrix MATCH (movie {title: "The Matrix"}) RETURN movie 16
  • 17. Tip: Use labels MATCH (movie:Movie {title: "The Matrix"}) RETURN movie 17
  • 18. Tip: Use labels MATCH (movie:Movie {title: "The Matrix"}) RETURN movie 18
  • 19. Finding The Matrix MATCH (movie {title: "The Matrix"}) RETURN movie MATCH (movie:Movie {title: "The Matrix"}) RETURN movie 19
  • 20. Tip: Use indexes and constraints ‣ Indexes for non unique values ‣ Constraints for unique values CREATE INDEX ON :Movie(title) CREATE INDEX ON :Person(name) CREATE CONSTRAINT ON (g:Genre) ASSERT g.name IS UNIQUE 20
  • 21. How does Neo4j use indexes? ‣ Indexes are only used to find the starting point for queries. 21
  • 22. How does Neo4j use indexes? ‣ Indexes are only used to find the starting point for queries. Use index scans to look up rows in tables and join them with rows from other tables Use indexes to find the starting points for a query. Relational Graph 22
  • 23. Tip: Use indexes and constraints MATCH (movie:Movie {title: "The Matrix"}) RETURN movie 23
  • 24. Finding The Matrix (no index) MATCH (movie:Movie {title: "The Matrix"}) RETURN movie (index) MATCH (movie:Movie {title: "The Matrix"}) RETURN movie 24
  • 25. Actors who appeared together MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) RETURN COUNT(*) 25
  • 26. Actors who appeared together MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) RETURN COUNT(*) 26
  • 27. Tip: Enforce index usage MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) USING INDEX a:Person(name) USING INDEX b:Person(name) RETURN COUNT(*) 27
  • 28. Tip: Enforce index usage MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) USING INDEX a:Person(name) USING INDEX b:Person(name) RETURN COUNT(*) 28
  • 29. Actors who appeared together MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) RETURN COUNT(*) MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->()<-[:ACTS_IN]- (b:Person {name:"Meg Ryan"}) USING INDEX a:Person(name) USING INDEX b:Person(name) RETURN COUNT(*) 29
  • 30. Tom Hanks’ colleagues’ movies MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]- (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title 30
  • 31. Tom Hanks’ colleagues’ movies MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]- (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title 31
  • 32. Tip: Reduce cardinality of WIP MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]- (coActor) WITH DISTINCT coActor MATCH (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title 32
  • 33. Tip: Reduce cardinality of WIP MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]- (coActor) WITH DISTINCT coActor MATCH (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title 33
  • 34. MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]-(coActor) WITH DISTINCT coActor MATCH (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title Tom Hanks’ colleagues’ movies MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m1)<-[:ACTS_IN]- (coActor)-[:ACTS_IN]->(m2) RETURN distinct m2.title; 34
  • 35. Counting number of movies MATCH (n:Actor)-[:ACTS_IN]->() RETURN n, COUNT(*) AS count ORDER BY count DESC 35
  • 36. Counting number of movies MATCH (n:Actor)-[:ACTS_IN]->() RETURN n, COUNT(*) AS count ORDER BY count DESC 36
  • 37. Tip: Use `SIZE` for fast counting MATCH (n:Actor) RETURN n, SIZE((n)-[:ACTS_IN]->()) AS count ORDER BY count DESC 37
  • 38. Counting number of movies MATCH (n:Actor)-[:ACTS_IN]->() RETURN n, COUNT(*) AS count ORDER BY count DESC MATCH (n:Actor) RETURN n, SIZE((n)-[:ACTS_IN]->()) AS count ORDER BY count DESC 38
  • 39. Hints USING INDEX • Force the use of a specific index MATCH (a:Person {name:"Tom Hanks"}) -[:ACTS_IN]->() USING INDEX a:Person(name) RETURN count(*) ‣ http://neo4j.com/docs/2.3.0-M03/query-using.html 39
  • 40. Hints USING SCAN • Forces a label scan on lower cardinality labels MATCH (a:Actor)-->(m:Movie:Comedy) USING SCAN m:Comedy RETURN count(distinct a) 40
  • 42. Use parameters MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m) RETURN m.title 42
  • 43. Use parameters MATCH (p:Person {name: {name}}) -[:ACTS_IN]->(m) RETURN m.title MATCH (p:Person {name:"Tom Hanks"}) -[:ACTS_IN]->(m) RETURN m.title 43
  • 44. Avoid Cartesian products ‣ Easy to do this inadvertently: MATCH (a:Actor), (m:Movie) RETURN count(a), count(m) ‣ This is correct, and performs better MATCH (a:Actor) WITH count(a) as a_count MATCH (m:Movie) RETURN a_count, count(m) 44
  • 45. Watch out for those warnings! 45
  • 47. Only RETURN what you need ‣ This is not recommended: MATCH (a:Actor) RETURN a ‣ Use this instead: MATCH (a:Actor) RETURN a.name, a.birthdate, a.height 47
  • 48. Keep it short ‘n sweet ‣ Keep queries as short as possible • Better to have many, smaller queries than one larger one ‣ Keep read and write queries separate • If not, only the RULE planner will be used 48
  • 49. tl;dr ‣ View query plans with EXPLAIN and PROFILE ‣ Use labels ‣ Index your starting points ‣ Reduce work in progress ‣ Use SIZE for fast relationship counting ‣ Remember the hints 49
  • 50. Thanks for coming ‣ And don’t forget, if the tips aren’t working ask us for help on Stack Overflow Mark Needham @markhneedham Petra Selmer @Aethelraed 50