Reader small image

You're reading from  Learning Neo4j 3.x - Second Edition

Product typeBook
Published inOct 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786466143
Edition2nd Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Jerome Baton
Jerome Baton
author image
Jerome Baton

Jérôme Baton started hacking computers at the age of skin problems, gaming first then continued his trip by self-learning Basic on Amstrad CPC, peaking on coding a full screen horizontal starfield, and messing the interlace of the video controller so that sprites appeared twice as high in horizontal beat'em up games. Disks were three inches for 178 Kb then. Then, for gaming reasons, he switched to Commodore Amiga and its fantastic AMOS Basic. Later caught by seriousness and studies, he wrote Turbo Pascal, C, COBOL, Visual C++, and Java on PCs and mainframes at university, and even Logo in high school. Then, Java happened and he became a consultant, mostly on backend code of websites in many different businesses. Jérôme authored several articles in French on Neo4j, JBoss Forge, an Arduino workshop for Devoxx4Kids, and reviewed kilos of books on Android. He has a weakness for wordplay, puns, spoonerisms, and Neo4j that relieves him from join(t) pains. Jérôme also has the joy to teach in French universities, currently at I.U.T de Paris, Université Paris V - René Descartes (Neo4j, Android), and Université de Troyes (Neo4j), where he does his best to enterTRain the students. When not programming, Jérôme enjoys photography, doing electronics, everything DIY, understanding how things work, trying to be clever or funny on Twitter, and spends a lot of time trying to understand his kids and life in general.
Read more about Jerome Baton

Right arrow

Chapter 7. Query Performance Tuning

In this chapter, you will learn how to estimate if our queries are under performing. It may well happen, and if this is the case, what are the rules to apply to improve them? The rule of thumb is to reduce the number of db hits.

A DB hit is a unit of work for the database. So, how do we do this?

We will cover the following topics in the chapter:

  • Explain and profile instructions
    • Query plan
    • Operators
  • Indexes
  • Rule of thumb for improving performance

Explain and profile instructions


Explain and profile are the two Cypher instructions that will help us get facts where we see possible performance issues.

Explain will give you the execution plan of your query without executing it, while profile will execute the query and return results.

The execution plan is not determined by a hitman but out of the text defining the query by a component of Cypher named the planner; there are two planners: cost planner and rule planner. Cost planner is used by default; it appeared in version 2.2. Its action is to smoothly turn a text into a list of operators (remember this name).

A query plan

To illustrate, here is an example of a query plan, which reads from top to bottom. You can see the related query in the image; it is based on the Movies dataset that is available if you follow the slides tied to the :play movies command:

A short execution plan

The first operator is NodeByLabelScan because I mentioned the labels. If I drop them, the execution plan becomes...

Indexes


In Neo4j, indexes are used to find the starting points of the queries. You can count on them. Indexes are automatically created on properties that have a constraint. Otherwise, you can create an index with a query like the following:

CREATE INDEX ON :LabelName(propertyName)

However, refrain from creating an index for every property of each label as they need to be maintained by the server when data is inserted. It is taken care of  here, but there is no magic.

The command to get the list of indexes in use in your graph is as follows:

CALL db.indexes

Force index usage

You may force the use of an index by specifying it in your query, as follows:

MATCH (t:Tower {name: ""})
 USING INDEX t:Tower(name)
 RETURN ...

Force label usage

You may also force the planner to start by evaluating nodes for a label instead of an index. This is particularly useful if you are querying for nodes having two labels and you know that one of the labels is more restrictive than the other. In this example, we could...

Rules of thumb


Here is a list of rules to help you make your queries the most performant possible.

Explain all the queries

Once written and their results verified, explain all your queries on a decent dataset to look for possible bottlenecks, as explained earlier in this chapter.

Rows

In your query plans, the row counts should decrease rapidly from top to bottom. If not, did you use enough labels? Enough indexes? Are the properties you use indexed?

Do not overconsume

Virtual resources are resources too. Do not waste CPU cycles, memory, and energy by returning more data than you need. Do not get the full nodes if you only want to use a few properties of each.

Cartesian or not?

Unless authorized, you should not do a cartesian product in your queries. You may already know this, but sometimes it happens. For example, here is one, the most obvious:

MATCH (n),(m)
RETURN n,m

Mind that the number of results is the square of the number of nodes. Recognize cartesian products by the lack of relation expressed...

Summary


We have seen how to check whether our queries are wasting resources and how to correct them, because CPU cycles are time and electric power, and your time is precious! Notwithstanding the fact that it generates heat and the climate is already a mess.

We saw Neo4j's execution plans, how to read them, and spot what may be an issue. We saw what the parameters are and how they might remind you of JDBC's PreparedStatements. We saw how to force the use of indexes or labels (you may have to do a comparison of the performances of both, depending on your graph and queries). Remember this old saying that elders transmit to younger disciples of our profession: Make it work, then, and only then, optimize!

Now, let's switch to something totally different: Chapter 8, Importing data into Neo4j.

The import queries can be profiled too!

 

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Neo4j 3.x - Second Edition
Published in: Oct 2017Publisher: PacktISBN-13: 9781786466143
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jerome Baton

Jérôme Baton started hacking computers at the age of skin problems, gaming first then continued his trip by self-learning Basic on Amstrad CPC, peaking on coding a full screen horizontal starfield, and messing the interlace of the video controller so that sprites appeared twice as high in horizontal beat'em up games. Disks were three inches for 178 Kb then. Then, for gaming reasons, he switched to Commodore Amiga and its fantastic AMOS Basic. Later caught by seriousness and studies, he wrote Turbo Pascal, C, COBOL, Visual C++, and Java on PCs and mainframes at university, and even Logo in high school. Then, Java happened and he became a consultant, mostly on backend code of websites in many different businesses. Jérôme authored several articles in French on Neo4j, JBoss Forge, an Arduino workshop for Devoxx4Kids, and reviewed kilos of books on Android. He has a weakness for wordplay, puns, spoonerisms, and Neo4j that relieves him from join(t) pains. Jérôme also has the joy to teach in French universities, currently at I.U.T de Paris, Université Paris V - René Descartes (Neo4j, Android), and Université de Troyes (Neo4j), where he does his best to enterTRain the students. When not programming, Jérôme enjoys photography, doing electronics, everything DIY, understanding how things work, trying to be clever or funny on Twitter, and spends a lot of time trying to understand his kids and life in general.
Read more about Jerome Baton