Hi,
I have a fairly small database: 5,773 nodes, 14,756 relationships. I have a number of queries I am running at once, combined with UNION ALL. I noticed that the individual queries (i.e. without any union statements) run fast, but when I combine them they run much slower - i.e. if I ran them all individually and added the times, I would get a cumulative time much lower than the time it takes to run them all as a single query using UNION ALL statements.
I am warming the cache with apoc.warmup.run(true, true, true) but it has no effect on the time it takes to run the queries. I have indexed node IDs and dates, which are the primary properties used in the queries.
The queries I am running are all pretty similar to the one below, which takes 223 ms to run the first time I run it (after warming the cache), and 107 ms to run the second time I run it. I am combining 250 of these short queries into a single query using UNION ALL - which takes about 10 minutes to run. If I group them into 22 separate queries, it takes them 4 minutes to run (cumulatively).
Example Query:
MATCH (di3:apple)-[:R1]->(:pear {Action: "Start"})
OPTIONAL MATCH (:banana)-[:R2]->(di3)-[:R3]->(l3:cherry)
WITH COLLECT(l3.ID) AS lids
MATCH (g:banana)-[:R2]->(di:apple)-[:R3]->(l:cherry)-[:R4]->(c:grape), (d:peach)<-[:R5]-(di)-[:R1]->(dt:pear)
WHERE NOT l.ID in lids
RETURN DISTINCT c.ID AS aa, dt.ID AS bb, dt.prop AS cc, g.ID AS dd, dt.Name AS ee, etc.
Is there anything I can do to speed up the combined query? My understanding is that UNION ALL just combines the results of each query - it's not trying to eliminate duplicates or anything. Why then does it take so much longer to run than it should?
Thank you