SlideShare a Scribd company logo
© 2022 Neo4j, Inc. All rights reserved.
Top 10 Cypher
(Tuning) Tips & Tricks
Michael Hunger,
Senior Director, User
Innovation
© 2022 Neo4j, Inc. All rights reserved.
3
Why should I care?
1 2
Efficiency Readability
3 4
Resources Network
© 2022 Neo4j, Inc. All rights reserved.
5
Dataset - (All of) StackOverflow
https://demo.neo4jlabs.com:7473
username/password/database: stackoverflow
51M nodes
124M relationships
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
6
1. PROFILE
© 2022 Neo4j, Inc. All rights reserved.
7
1. Profile
• PROFILE and EXPLAIN show you the query plan
• All Operators – Index lookups, Expand, Projection, …
• Cardinality (rows) - multiplicative
• DB-Hits (measure of cost)
• Avoid Eager for Updates
• Shows Memory Usage
• Best if you can stream through and shortcut (e.g. LIMIT)
• avoid aggregation / sorting / collect if you can
© 2022 Neo4j, Inc. All rights reserved.
8
PROFILE
PROFILE
MATCH (t:Tag)<-[:TAGGED]-(q:Question)
RETURN t.name, count(*) AS c
ORDER BY c DESC LIMIT 10
// Cypher version: CYPHER 4.4,
// planner: COST,
// runtime: PIPELINED.
// 97726446 total db hits
// in 29387 ms
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
9
2. Properties
© 2022 Neo4j, Inc. All rights reserved.
10
2. Properties
• Property reads are more expensive than they should be
• Avoid them if possible, or defer to the latest moment
• When you have narrowed down the data to what you want to return
• Elevate properties to labels or rel-types can speed up a lot
• E.g. boolean, status properties as labels
• Sub-Structure of rel-properties to Rel-Types (e.g. year of a datetime)
• Use indexes, properties can be read from the index (except for points)
which is much faster (need indicate type)
© 2022 Neo4j, Inc. All rights reserved.
11
PROFILE
PROFILE
MATCH (t:Tag)<-[:TAGGED]-(q:Question)
WITH t, count(*) AS c
ORDER BY c DESC LIMIT 10
RETURN t.name, c
// 97526566 total db hits
// in 29831 ms.
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
12
3. Relationships
© 2022 Neo4j, Inc. All rights reserved.
13
Relationships
• Avoid traversing relationships if you can, each is O(1) but all are O(n)
• Always provide rel-type and direction
• Don’t traverse across supernodes, check “against” them
• Use get-degree (size(pattern)) without end label to get fast reads
• Elevate properties to rel-type for speedups
• Use relationship-indexes for global lookups or
• Add source/target key/property to index for node-centric indexes
© 2022 Neo4j, Inc. All rights reserved.
14
PROFILE
PROFILE
MATCH (t:Tag)
WITH t, size([p=(t)<-[:TAGGED]-()|p]) AS c
ORDER BY c DESC LIMIT 10
RETURN t.name, c
// 104911 total db hits in 195 ms
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
15
4. Indexes & Constraints
© 2022 Neo4j, Inc. All rights reserved.
16
PROFILE
PROFILE
MATCH (t:Tag)
RETURN t.name, t.count AS c
ORDER BY c DESC LIMIT 10
// 157356 total db hits in 66 ms
© 2022 Neo4j, Inc. All rights reserved.
17
PROFILE
PROFILE
MATCH (t:Tag)
// type triggers index usage
WHERE t.count > 0
RETURN t.name, t.count AS c
ORDER BY c DESC LIMIT 10
// 32 total db hits in 1 ms.
© 2022 Neo4j, Inc. All rights reserved.
18
Types & Uses of indexes and constraints
1 2
Lookup
Ranges
Type Scans
3 4
Sorting
Aggregation
Uniqueness
Existence
© 2022 Neo4j, Inc. All rights reserved.
19
Indexes & Constraints
• show indexes
• show constraints
• create <type> index/constraint <name> IF NOT EXISTS ON (x) FOR
(prop)
• todo docs
© 2022 Neo4j, Inc. All rights reserved.
20
Indexes & Constraints
• lookup
• lookup in list
• lookup range
• don't read from property store
• sorting / aggregation
• compound indexes (one range, rest equality)
◦ NOM type
• relationship(type) indexes
◦ global lookup by attribute / range
◦ simulate node-centric indexes index start/end-node id or property
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
21
neo4j.com/docs/cypher-manual/current/query-tuning/using/
5. Query Hints
© 2022 Neo4j, Inc. All rights reserved.
22
Query Hints
• shouldn't be needed
• but planner is not smart enough (yet)
• use rarely, first try without
• USING INDEX [SEEK] - force index usage (also for relationships)
• USING JOIN ON - force hash join for two high-cardinality sides
◦ example with LDBC
• USING SCAN ON - force label scan if very selective label
© 2022 Neo4j, Inc. All rights reserved.
23
INDEX Hint
PROFILE
MATCH (t:Tag)
WHERE t.count > 0
USING INDEX :Tag(count)
RETURN t.name, t.count AS c
ORDER BY c DESC LIMIT 10
// 32 total db hits in 1 ms.
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
24
4. Var Length Queries
© 2022 Neo4j, Inc. All rights reserved.
25
4. Var Length Queries
• Traverse arbitrary length paths / trees
• Limit length if possible
• Pruning Var-Length Expand ( on distinct end nodes )
• Shortest Path ( can also be used as condition )
© 2022 Neo4j, Inc. All rights reserved.
26
Var Length Example
profile
MATCH (u:User {name:'meistermeier'})-
[:POSTED|ACCEPTED|ANSWERED*..5]-(u2:User)
RETURN count(u)
// 569.130 total db hits
© 2022 Neo4j, Inc. All rights reserved.
27
Var Length Example
profile
MATCH (u:User {name:'meistermeier'})-
[:POSTED|ACCEPTED|ANSWERED*..5]-(u2:User)
RETURN count(distinct u)
// 217.347 total db hits
© 2022 Neo4j, Inc. All rights reserved.
28
Triadic Closure Example
profile
MATCH (u:User {name:'meistermeier‘})
--()--(u2:User)
WHERE NOT (u)--(u2)
RETURN count(distinct u)
// 810 total db total db hits
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
29
5. Fun with Lists
© 2022 Neo4j, Inc. All rights reserved.
30
5. Fun with Lists
• Like in Python
• [ elem IN list WHERE pred(elem) | expr(elem) ]
• Result is a list again
• Quantors
• all/single/none/any(x IN list WHERE pred(element))
• List into rows: UNWIND list AS elem
• Rows into list: collect(elem) AS list
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
31
6. Map Projections
© 2022 Neo4j, Inc. All rights reserved.
32
5. Map Projections
• Idea borrowed from GraphQL
• Map expression - node/relationship/map as expression
• var { .* } // all properties
• var {.property1, .property2}
• var {value} // key = „value“
• var {name: expression} // any expression, incl. map/list
• collect map expression to get list of documents
• You can use map { .*, foo : null } to remove properties from a map
(e.g. for LOAD CSV)
© 2022 Neo4j, Inc. All rights reserved.
33
5. Map Projections
MATCH (u:User {name:'meistermeier‘})
-[:POSTED]->()-[:ANSWERED]->(q)-[:TAGGED]->(tag)
WITH q, collect(tag.name) as tags
RETURN q { .title, .answers, .views, tags, } as question
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
34
7. Pattern
Comprehensions
© 2022 Neo4j, Inc. All rights reserved.
35
6. Pattern Comprehensions
• Idea borrowed from GraphQL
• Pattern comprehension = like list comprehension just for patterns
• Pattern (can introduce new local variables)
• Filter
• Expression
• [ (pattern)-[of:SOME]->(nodes) WHERE filter | expression ]
• Result is list of the expression
• Expressions can be pattern comprehensions again or map expressions
• Not well planned (indexes etc)
• No sorting / pagination / pushdown
© 2022 Neo4j, Inc. All rights reserved.
36
6. Pattern Comprehensions
MATCH (u:User {name:'meistermeier‘})
RETURN [(u)-[:POSTED]->()-[:ANSWERED]->(q) |
q { .title, .answers, .views }] as questions
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
37
8. Subqueries
© 2022 Neo4j, Inc. All rights reserved.
38
6. Subqueries
• Added in 4.0/4.1
• CALL { WITH var MATCH … RETURN var2 } …
• Existential subqueries EXISTS {}
• Can not shadow variables in RETURN
• Dependent or independent
• Still participate in cardinality but can change it
• Properly planned, unlike pattern comprehensions -> can rewrite
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
39
9. Batched Updates
© 2022 Neo4j, Inc. All rights reserved.
40
7. Batched Updates
• Avoid transaction sizes exceed available memory
• Update in smaller chunks e.g. 50k updates per tx
• (2/3G heap per 1M updates in a tx)
• 3 options
◦ Send chunks (list of dicts) from client: UNWIND $data AS row …
◦ CALL {} IN TRANSACTIONS [OF 50000 ROWS]
◦ APOC apoc.periodic.iterate
• USING PERIODIC COMMIT is deprecated
• Avoid the Eager
© 2022 Neo4j, Inc. All rights reserved.
41
7. Avoid the Eager
• To isolate dependent reads from writes
• Cypher injects an Eager operator that
• pulls ALL rows through that happen before the operator
• Which can use a lot of memory,
• and can disable transaction batching
• Avoided through:
• Multi-pass
© 2022 Neo4j, Inc. All rights reserved.
42
9. Might help to
• To isolate dependent reads from writes
• Cypher injects an Eager operator that
• pulls ALL rows through that happen before the operator
• Which can use a lot of memory, and can disable batching
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
43
10. User Defined
Procedures & Functions
© 2022 Neo4j, Inc. All rights reserved.
44
10. User Defined Procedures & Functions
• Lego blocks to combine inside a Cypher query
• Procedure libraries like APOC cover a wide range of utilities (have a look)
• Refactoring
• Data integration
• Import
• Map/collection/datetime functions
• SHOW PROCEDURES / FUNCTIONS / call apoc.help(„text“)
• CALL procedure(arg1, arg2) YIELD col1, col2
© 2022 Neo4j, Inc. All rights reserved.
45
10. User Defined Procedures & Functions
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
46
11. Upcoming – Path
Filters
© 2022 Neo4j, Inc. All rights reserved.
47
11. Upcoming – Path Filters
• Working towards GQL support
• Inline WHERE predicates for nodes, relationships (5.0)
• Future: predicates for pattern
• Quantifier for patterns / subpatterns
© 2022 Neo4j, Inc. All rights reserved.
48
Learn more
1 2
Docs
neo4j.com/docs/cypher-
manual/current/query-tuning/
Course
neo4j.com/graphacademy/training-cqt-40/
3 4
Refcard
neo4j.com/docs/cypher-refcard
Community
community.neo4j.com
© 2022 Neo4j, Inc. All rights reserved.
49
Q&A
© 2022 Neo4j, Inc. All rights reserved.
50
Thank you!
Contact us at
sales@neo4j.com
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
51
Split 3rds - bullets
When using bullets on the left ,
select all the bullets and use:
Add space after list item
to visually separate them
Example of bullets:
• Short point
here not more
than 3 lines
• Another point
which is not too
long
• Short and
sweet
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
52
Neo4j Aura Enterprise is allowing us to realize our
vision to build a knowledge graph that unifies all
product knowledge to improve our retail
recommendations, search and merchandising.”
“
- Name and position, Company
© 2022 Neo4j, Inc. All rights reserved.
53
Special slides examples
1 2
Arial
Extra Bold 86pt
for big numbers
Arial
Normal 14pt for
small text
3 4
Arial
Extra Bold 86pt
for big numbers
Arial
Normal 14pt for
small text
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
54
Thing to compare 2
Thing to compare 1
• Comparison 1
• Comparison 2
◦ Details
• Comparison 1
• Comparison 2
◦ Details
© 2022 Neo4j, Inc. All rights reserved.
55
1. Use Media Slide from Master
2. Image is cropped at width
4.62in, so it takes up half of
the slide (and aligned with
blue rectangle on the left)
Media
© 2022 Neo4j, Inc. All rights reserved.
56
Image Placeholder
© 2022 Neo4j, Inc. All rights reserved.
© 2022 Neo4j, Inc. All rights reserved.
57
© 2022 Neo4j, Inc. All rights reserved.
58
Your Logo Here
© 2022 Neo4j, Inc. All rights reserved.
59
59
Your Logo Here

More Related Content

PDF
Oracle 21c: New Features and Enhancements of Data Pump & TTS
PPTX
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
PDF
IMS DC Self Study Complete Tutorial
PDF
The Neo4j Data Platform for Today & Tomorrow.pdf
PPTX
Azure storage
PPTX
Oracle Database in-Memory Overivew
PDF
Oracle RAC One Node 12c Overview
PDF
Graph databases and OrientDB
Oracle 21c: New Features and Enhancements of Data Pump & TTS
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
IMS DC Self Study Complete Tutorial
The Neo4j Data Platform for Today & Tomorrow.pdf
Azure storage
Oracle Database in-Memory Overivew
Oracle RAC One Node 12c Overview
Graph databases and OrientDB

What's hot (20)

PPT
A complete guide to azure storage
PPT
Ms sql server architecture
PPTX
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
PDF
MariaDB ColumnStore
PDF
Oracle Database Appliance Workshop
PPTX
Basic oracle-database-administration
PDF
Oracle Database – Mission Critical
PDF
Oracle 12c Multitenant architecture
PPT
PPTX
The Art of the Possible with Graph - Sudhir Hasbe - GraphSummit London 14 Nov...
PPS
Oracle Database Overview
PPT
DB2 and storage management
PDF
SQL Outer Joins for Fun and Profit
PDF
Cisco UCS (Unified Computing System)
PDF
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
PDF
Oracle db architecture
PDF
Mongo DB
PDF
NOSQL- Presentation on NoSQL
PDF
Spark Summit East 2015 Advanced Devops Student Slides
PDF
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
A complete guide to azure storage
Ms sql server architecture
re:Invent 2022 DAT326 Deep dive into Amazon Aurora and its innovations
MariaDB ColumnStore
Oracle Database Appliance Workshop
Basic oracle-database-administration
Oracle Database – Mission Critical
Oracle 12c Multitenant architecture
The Art of the Possible with Graph - Sudhir Hasbe - GraphSummit London 14 Nov...
Oracle Database Overview
DB2 and storage management
SQL Outer Joins for Fun and Profit
Cisco UCS (Unified Computing System)
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Oracle db architecture
Mongo DB
NOSQL- Presentation on NoSQL
Spark Summit East 2015 Advanced Devops Student Slides
Oracle Real Application Clusters (RAC) 12c Rel. 2 - Operational Best Practices
Ad

Similar to GraphConnect 2022 - Top 10 Cypher Tuning Tips & Tricks.pptx (20)

PPTX
The Inside Scoop on Neo4j: Meet the Builders
PDF
RadioBOSS Advanced 7.0.8 Free Download
PDF
Evernote 10.132.4.49891 With Crack free
PDF
Roadmap y Novedades de producto
PDF
Adobe Photoshop 2025 Free crack Download
PDF
Office Tool Plus Free Download (Latest 2025)
PDF
managing big data
PDF
Neo4j: The path to success with Graph Database and Graph Data Science
PDF
Graph Connect: Tuning Cypher
PDF
Training Series - Intro to Neo4j
PDF
Pazu Netflix Video Downloader Download
PDF
Atlantis Word Processor 4.4.5.1 Free Download
PDF
Pazu Netflix Video Downloader 1.7.3 Crack Free
PPTX
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
PDF
Adobe Substance 3D Designer 14.1.2.8986
PDF
Neo4j Introduction (Basics, Cypher, RDBMS to GRAPH)
PPTX
From Relational to Graph: How Going Graph Revealed the Unknown(Jason_Schatz)....
PPTX
Neo4j Training Introduction
PPTX
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
PDF
Markdown Monster 3.6.9 Free crack Download
The Inside Scoop on Neo4j: Meet the Builders
RadioBOSS Advanced 7.0.8 Free Download
Evernote 10.132.4.49891 With Crack free
Roadmap y Novedades de producto
Adobe Photoshop 2025 Free crack Download
Office Tool Plus Free Download (Latest 2025)
managing big data
Neo4j: The path to success with Graph Database and Graph Data Science
Graph Connect: Tuning Cypher
Training Series - Intro to Neo4j
Pazu Netflix Video Downloader Download
Atlantis Word Processor 4.4.5.1 Free Download
Pazu Netflix Video Downloader 1.7.3 Crack Free
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Adobe Substance 3D Designer 14.1.2.8986
Neo4j Introduction (Basics, Cypher, RDBMS to GRAPH)
From Relational to Graph: How Going Graph Revealed the Unknown(Jason_Schatz)....
Neo4j Training Introduction
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
Markdown Monster 3.6.9 Free crack Download
Ad

More from jexp (20)

PDF
Looming Marvelous - Virtual Threads in Java Javaland.pdf
PDF
Easing the daily grind with the awesome JDK command line tools
PDF
Looming Marvelous - Virtual Threads in Java
PPTX
Neo4j Connector Apache Spark FiNCENFiles
PPTX
How Graphs Help Investigative Journalists to Connect the Dots
PPTX
The Home Office. Does it really work?
PDF
Polyglot Applications with GraalVM
PPTX
Neo4j Graph Streaming Services with Apache Kafka
PDF
How Graph Databases efficiently store, manage and query connected data at s...
PPTX
APOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures Library
PPTX
Refactoring, 2nd Edition
PPTX
New Features in Neo4j 3.4 / 3.3 - Graph Algorithms, Spatial, Date-Time & Visu...
PPTX
GraphQL - The new "Lingua Franca" for API-Development
PPTX
A whirlwind tour of graph databases
PDF
Practical Graph Algorithms with Neo4j
PPTX
A Game of Data and GraphQL
PPTX
Querying Graphs with GraphQL
PDF
Graphs & Neo4j - Past Present Future
PDF
Intro to Graphs and Neo4j
PDF
Class graph neo4j and software metrics
Looming Marvelous - Virtual Threads in Java Javaland.pdf
Easing the daily grind with the awesome JDK command line tools
Looming Marvelous - Virtual Threads in Java
Neo4j Connector Apache Spark FiNCENFiles
How Graphs Help Investigative Journalists to Connect the Dots
The Home Office. Does it really work?
Polyglot Applications with GraalVM
Neo4j Graph Streaming Services with Apache Kafka
How Graph Databases efficiently store, manage and query connected data at s...
APOC Pearls - Whirlwind Tour Through the Neo4j APOC Procedures Library
Refactoring, 2nd Edition
New Features in Neo4j 3.4 / 3.3 - Graph Algorithms, Spatial, Date-Time & Visu...
GraphQL - The new "Lingua Franca" for API-Development
A whirlwind tour of graph databases
Practical Graph Algorithms with Neo4j
A Game of Data and GraphQL
Querying Graphs with GraphQL
Graphs & Neo4j - Past Present Future
Intro to Graphs and Neo4j
Class graph neo4j and software metrics

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPT
Teaching material agriculture food technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Cloud computing and distributed systems.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Modernizing your data center with Dell and AMD
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Teaching material agriculture food technology
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Cloud computing and distributed systems.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Machine learning based COVID-19 study performance prediction
Modernizing your data center with Dell and AMD
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Empathic Computing: Creating Shared Understanding
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Monthly Chronicles - July 2025
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology

GraphConnect 2022 - Top 10 Cypher Tuning Tips & Tricks.pptx

  • 1. © 2022 Neo4j, Inc. All rights reserved. Top 10 Cypher (Tuning) Tips & Tricks Michael Hunger, Senior Director, User Innovation
  • 2. © 2022 Neo4j, Inc. All rights reserved. 3 Why should I care? 1 2 Efficiency Readability 3 4 Resources Network
  • 3. © 2022 Neo4j, Inc. All rights reserved. 5 Dataset - (All of) StackOverflow https://demo.neo4jlabs.com:7473 username/password/database: stackoverflow 51M nodes 124M relationships
  • 4. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 6 1. PROFILE
  • 5. © 2022 Neo4j, Inc. All rights reserved. 7 1. Profile • PROFILE and EXPLAIN show you the query plan • All Operators – Index lookups, Expand, Projection, … • Cardinality (rows) - multiplicative • DB-Hits (measure of cost) • Avoid Eager for Updates • Shows Memory Usage • Best if you can stream through and shortcut (e.g. LIMIT) • avoid aggregation / sorting / collect if you can
  • 6. © 2022 Neo4j, Inc. All rights reserved. 8 PROFILE PROFILE MATCH (t:Tag)<-[:TAGGED]-(q:Question) RETURN t.name, count(*) AS c ORDER BY c DESC LIMIT 10 // Cypher version: CYPHER 4.4, // planner: COST, // runtime: PIPELINED. // 97726446 total db hits // in 29387 ms
  • 7. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 9 2. Properties
  • 8. © 2022 Neo4j, Inc. All rights reserved. 10 2. Properties • Property reads are more expensive than they should be • Avoid them if possible, or defer to the latest moment • When you have narrowed down the data to what you want to return • Elevate properties to labels or rel-types can speed up a lot • E.g. boolean, status properties as labels • Sub-Structure of rel-properties to Rel-Types (e.g. year of a datetime) • Use indexes, properties can be read from the index (except for points) which is much faster (need indicate type)
  • 9. © 2022 Neo4j, Inc. All rights reserved. 11 PROFILE PROFILE MATCH (t:Tag)<-[:TAGGED]-(q:Question) WITH t, count(*) AS c ORDER BY c DESC LIMIT 10 RETURN t.name, c // 97526566 total db hits // in 29831 ms.
  • 10. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 12 3. Relationships
  • 11. © 2022 Neo4j, Inc. All rights reserved. 13 Relationships • Avoid traversing relationships if you can, each is O(1) but all are O(n) • Always provide rel-type and direction • Don’t traverse across supernodes, check “against” them • Use get-degree (size(pattern)) without end label to get fast reads • Elevate properties to rel-type for speedups • Use relationship-indexes for global lookups or • Add source/target key/property to index for node-centric indexes
  • 12. © 2022 Neo4j, Inc. All rights reserved. 14 PROFILE PROFILE MATCH (t:Tag) WITH t, size([p=(t)<-[:TAGGED]-()|p]) AS c ORDER BY c DESC LIMIT 10 RETURN t.name, c // 104911 total db hits in 195 ms
  • 13. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 15 4. Indexes & Constraints
  • 14. © 2022 Neo4j, Inc. All rights reserved. 16 PROFILE PROFILE MATCH (t:Tag) RETURN t.name, t.count AS c ORDER BY c DESC LIMIT 10 // 157356 total db hits in 66 ms
  • 15. © 2022 Neo4j, Inc. All rights reserved. 17 PROFILE PROFILE MATCH (t:Tag) // type triggers index usage WHERE t.count > 0 RETURN t.name, t.count AS c ORDER BY c DESC LIMIT 10 // 32 total db hits in 1 ms.
  • 16. © 2022 Neo4j, Inc. All rights reserved. 18 Types & Uses of indexes and constraints 1 2 Lookup Ranges Type Scans 3 4 Sorting Aggregation Uniqueness Existence
  • 17. © 2022 Neo4j, Inc. All rights reserved. 19 Indexes & Constraints • show indexes • show constraints • create <type> index/constraint <name> IF NOT EXISTS ON (x) FOR (prop) • todo docs
  • 18. © 2022 Neo4j, Inc. All rights reserved. 20 Indexes & Constraints • lookup • lookup in list • lookup range • don't read from property store • sorting / aggregation • compound indexes (one range, rest equality) ◦ NOM type • relationship(type) indexes ◦ global lookup by attribute / range ◦ simulate node-centric indexes index start/end-node id or property
  • 19. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 21 neo4j.com/docs/cypher-manual/current/query-tuning/using/ 5. Query Hints
  • 20. © 2022 Neo4j, Inc. All rights reserved. 22 Query Hints • shouldn't be needed • but planner is not smart enough (yet) • use rarely, first try without • USING INDEX [SEEK] - force index usage (also for relationships) • USING JOIN ON - force hash join for two high-cardinality sides ◦ example with LDBC • USING SCAN ON - force label scan if very selective label
  • 21. © 2022 Neo4j, Inc. All rights reserved. 23 INDEX Hint PROFILE MATCH (t:Tag) WHERE t.count > 0 USING INDEX :Tag(count) RETURN t.name, t.count AS c ORDER BY c DESC LIMIT 10 // 32 total db hits in 1 ms.
  • 22. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 24 4. Var Length Queries
  • 23. © 2022 Neo4j, Inc. All rights reserved. 25 4. Var Length Queries • Traverse arbitrary length paths / trees • Limit length if possible • Pruning Var-Length Expand ( on distinct end nodes ) • Shortest Path ( can also be used as condition )
  • 24. © 2022 Neo4j, Inc. All rights reserved. 26 Var Length Example profile MATCH (u:User {name:'meistermeier'})- [:POSTED|ACCEPTED|ANSWERED*..5]-(u2:User) RETURN count(u) // 569.130 total db hits
  • 25. © 2022 Neo4j, Inc. All rights reserved. 27 Var Length Example profile MATCH (u:User {name:'meistermeier'})- [:POSTED|ACCEPTED|ANSWERED*..5]-(u2:User) RETURN count(distinct u) // 217.347 total db hits
  • 26. © 2022 Neo4j, Inc. All rights reserved. 28 Triadic Closure Example profile MATCH (u:User {name:'meistermeier‘}) --()--(u2:User) WHERE NOT (u)--(u2) RETURN count(distinct u) // 810 total db total db hits
  • 27. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 29 5. Fun with Lists
  • 28. © 2022 Neo4j, Inc. All rights reserved. 30 5. Fun with Lists • Like in Python • [ elem IN list WHERE pred(elem) | expr(elem) ] • Result is a list again • Quantors • all/single/none/any(x IN list WHERE pred(element)) • List into rows: UNWIND list AS elem • Rows into list: collect(elem) AS list
  • 29. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 31 6. Map Projections
  • 30. © 2022 Neo4j, Inc. All rights reserved. 32 5. Map Projections • Idea borrowed from GraphQL • Map expression - node/relationship/map as expression • var { .* } // all properties • var {.property1, .property2} • var {value} // key = „value“ • var {name: expression} // any expression, incl. map/list • collect map expression to get list of documents • You can use map { .*, foo : null } to remove properties from a map (e.g. for LOAD CSV)
  • 31. © 2022 Neo4j, Inc. All rights reserved. 33 5. Map Projections MATCH (u:User {name:'meistermeier‘}) -[:POSTED]->()-[:ANSWERED]->(q)-[:TAGGED]->(tag) WITH q, collect(tag.name) as tags RETURN q { .title, .answers, .views, tags, } as question
  • 32. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 34 7. Pattern Comprehensions
  • 33. © 2022 Neo4j, Inc. All rights reserved. 35 6. Pattern Comprehensions • Idea borrowed from GraphQL • Pattern comprehension = like list comprehension just for patterns • Pattern (can introduce new local variables) • Filter • Expression • [ (pattern)-[of:SOME]->(nodes) WHERE filter | expression ] • Result is list of the expression • Expressions can be pattern comprehensions again or map expressions • Not well planned (indexes etc) • No sorting / pagination / pushdown
  • 34. © 2022 Neo4j, Inc. All rights reserved. 36 6. Pattern Comprehensions MATCH (u:User {name:'meistermeier‘}) RETURN [(u)-[:POSTED]->()-[:ANSWERED]->(q) | q { .title, .answers, .views }] as questions
  • 35. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 37 8. Subqueries
  • 36. © 2022 Neo4j, Inc. All rights reserved. 38 6. Subqueries • Added in 4.0/4.1 • CALL { WITH var MATCH … RETURN var2 } … • Existential subqueries EXISTS {} • Can not shadow variables in RETURN • Dependent or independent • Still participate in cardinality but can change it • Properly planned, unlike pattern comprehensions -> can rewrite
  • 37. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 39 9. Batched Updates
  • 38. © 2022 Neo4j, Inc. All rights reserved. 40 7. Batched Updates • Avoid transaction sizes exceed available memory • Update in smaller chunks e.g. 50k updates per tx • (2/3G heap per 1M updates in a tx) • 3 options ◦ Send chunks (list of dicts) from client: UNWIND $data AS row … ◦ CALL {} IN TRANSACTIONS [OF 50000 ROWS] ◦ APOC apoc.periodic.iterate • USING PERIODIC COMMIT is deprecated • Avoid the Eager
  • 39. © 2022 Neo4j, Inc. All rights reserved. 41 7. Avoid the Eager • To isolate dependent reads from writes • Cypher injects an Eager operator that • pulls ALL rows through that happen before the operator • Which can use a lot of memory, • and can disable transaction batching • Avoided through: • Multi-pass
  • 40. © 2022 Neo4j, Inc. All rights reserved. 42 9. Might help to • To isolate dependent reads from writes • Cypher injects an Eager operator that • pulls ALL rows through that happen before the operator • Which can use a lot of memory, and can disable batching
  • 41. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 43 10. User Defined Procedures & Functions
  • 42. © 2022 Neo4j, Inc. All rights reserved. 44 10. User Defined Procedures & Functions • Lego blocks to combine inside a Cypher query • Procedure libraries like APOC cover a wide range of utilities (have a look) • Refactoring • Data integration • Import • Map/collection/datetime functions • SHOW PROCEDURES / FUNCTIONS / call apoc.help(„text“) • CALL procedure(arg1, arg2) YIELD col1, col2
  • 43. © 2022 Neo4j, Inc. All rights reserved. 45 10. User Defined Procedures & Functions
  • 44. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 46 11. Upcoming – Path Filters
  • 45. © 2022 Neo4j, Inc. All rights reserved. 47 11. Upcoming – Path Filters • Working towards GQL support • Inline WHERE predicates for nodes, relationships (5.0) • Future: predicates for pattern • Quantifier for patterns / subpatterns
  • 46. © 2022 Neo4j, Inc. All rights reserved. 48 Learn more 1 2 Docs neo4j.com/docs/cypher- manual/current/query-tuning/ Course neo4j.com/graphacademy/training-cqt-40/ 3 4 Refcard neo4j.com/docs/cypher-refcard Community community.neo4j.com
  • 47. © 2022 Neo4j, Inc. All rights reserved. 49 Q&A
  • 48. © 2022 Neo4j, Inc. All rights reserved. 50 Thank you! Contact us at sales@neo4j.com
  • 49. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 51 Split 3rds - bullets When using bullets on the left , select all the bullets and use: Add space after list item to visually separate them Example of bullets: • Short point here not more than 3 lines • Another point which is not too long • Short and sweet
  • 50. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 52 Neo4j Aura Enterprise is allowing us to realize our vision to build a knowledge graph that unifies all product knowledge to improve our retail recommendations, search and merchandising.” “ - Name and position, Company
  • 51. © 2022 Neo4j, Inc. All rights reserved. 53 Special slides examples 1 2 Arial Extra Bold 86pt for big numbers Arial Normal 14pt for small text 3 4 Arial Extra Bold 86pt for big numbers Arial Normal 14pt for small text
  • 52. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 54 Thing to compare 2 Thing to compare 1 • Comparison 1 • Comparison 2 ◦ Details • Comparison 1 • Comparison 2 ◦ Details
  • 53. © 2022 Neo4j, Inc. All rights reserved. 55 1. Use Media Slide from Master 2. Image is cropped at width 4.62in, so it takes up half of the slide (and aligned with blue rectangle on the left) Media
  • 54. © 2022 Neo4j, Inc. All rights reserved. 56 Image Placeholder
  • 55. © 2022 Neo4j, Inc. All rights reserved. © 2022 Neo4j, Inc. All rights reserved. 57
  • 56. © 2022 Neo4j, Inc. All rights reserved. 58 Your Logo Here
  • 57. © 2022 Neo4j, Inc. All rights reserved. 59 59 Your Logo Here

Editor's Notes

  • #49: Video (TBD) youtube.com/watch?v=QnozzFP_fPo