cancel
Showing results for 
Search instead for 
Did you mean: 

Weekly Challenge #2 Query Tuning

TrevorS
Community Team
Community Team

For this week's challenge, let's create a recommendations sandbox at sandbox.neo4j.com.

This query returns the movie titles by a person who acted in the movies. We use the actorName parameter with the value "Clint Eastwood".

Your starting Cypher is 

 

 

PROFILE
MATCH (a:Actor)-[:ACTED_IN]->(m:Movie)
WHERE a.name = $actor
RETURN m.title AS Movies

 

 

How can this query be improved?

Please post your solution below!

TrevorS
Community Specialist
1 ACCEPTED SOLUTION

This query should be better:

MATCH (a:Person {name: $actor})-[:ACTED_IN]->(m)
RETURN m.title AS Movies;

Cobra_0-1659976307177.png

Regards,
Cobra

View solution in original post

5 REPLIES 5

Cobra
Ninja
Ninja

Hello @TrevorS 😊

Here is my query:

:param actor => 'Clint Eastwood';

MATCH (a:Actor {name: $actor}) 
WITH a LIMIT 1 
MATCH (a)-[:ACTED_IN]->(m) 
RETURN m.title AS Movies;

Here is the screenshot:

Weekly Challenge #2.png

The query performance can be improved by adding a UNIQUE CONSTRAINT or an INDEX without changing the query:

CREATE CONSTRAINT constraint_Actor_name FOR (n:Actor) REQUIRE n.name IS UNIQUE;
CREATE INDEX index_Actor_name FOR (n:Actor) ON (n.name);

Regards,
Cobra

@Cobra ,
You cannot create the constraint on this dataset. And we want a query that uses the database as is without adding an index.
Your query had an elapsed time of 7 ms. 
There is a query with an elapsed time of 5 ms.

Elaine

This query should be better:

MATCH (a:Person {name: $actor})-[:ACTED_IN]->(m)
RETURN m.title AS Movies;

Cobra_0-1659976307177.png

Regards,
Cobra

gaga
Node

@TrevorS My solution is the best I think: 30 ms: 

PROFILE MATCH (a:Actor)-->(m:Movie)
WHERE exists ((a)-[:ACTED_IN]-()) AND a.name = "Clint Eastwood"
SET a:Actor
RETURN m.title

@gaga 

This query is not faster (even when I remove the update of the label).

A couple of things to note:
1. You do not want literals in your queries (except for testing a query). Use parameters.

2. Why are you setting the label? It is already an Actor?

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.