SlideShare uma empresa Scribd logo
1 de 50
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante 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
 

Semelhante 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
 

Mais 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
 

Mais 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

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 

Último (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 

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