Repository navigation
Description
Since #8524, SQL aggregates over a type scan in the parallel workers: SELECT sum(x) FROM V over 2,000,000 vertices takes 127-130 ms on about 10 of 12 cores. The same aggregate in openCypher, MATCH (n:V) RETURN sum(n.x), takes 680-752 ms on one core, so Cypher is now 4-8x behind SQL on every whole-label aggregate I tried.
Two parts, both visible in the source at 3d5e1a5:
- An unfiltered label scan never takes the parallel path:
NodeByLabelScansetsparallelCandidate = filter != null && ..., sosum,max, and a GROUP BY without a WHERE run at 1.0 cores. - With a WHERE, the scan is parallel since fix: Cypher OR index seeks, index order for more ORDER BY shapes, parallel filtered scan (#8723 #8724 #8725) #8771, but the aggregation stays on the consumer thread (
AggregationStep,GroupByAggregationStep); the PR says so: "Partial aggregation in workers not added: only filter survivors reach the consumer." The consumer is the bottleneck: the filtered count runs at 2.0-2.1 cores and is barely faster than the unfiltered sum.
Measured
Upstream main 3d5e1a5, embedded, outside a transaction, 2,000,000 vertices of one type created with BUCKETS 8, grp uniform in [0, 100), x uniform in [0, 1). 12 cores (cpuset 4-15), -Xmx6g. Each query: 5 untimed runs, then 9 timed; p50 wall time, and process CPU time over wall time during the timed runs ("cores"). Temurin 25 with compact object headers, Temurin 21 in brackets.
| query | SQL p50 | SQL cores | Cypher p50 | Cypher cores | Cypher / SQL |
|---|---|---|---|---|---|
count(*) where x > 0.5 |
82 ms (89) | 10.2 (10.0) | 657 ms (722) | 2.1 (2.1) | 8.0x (8.1x) |
sum(x) |
127 ms (130) | 10.6 (10.3) | 680 ms (752) | 1.0 (1.0) | 5.4x (5.8x) |
max(x) |
134 ms (141) | 10.8 (10.4) | 684 ms (706) | 1.1 (1.0) | 5.1x (5.0x) |
group by grp: count(*), sum(x) |
226 ms (239) | 10.4 (10.4) | 973 ms (1,146) | 1.0 (1.0) | 4.3x (4.8x) |
group by grp where x > 0.5: count(*) |
125 ms (135) | 10.3 (10.3) | 787 ms (870) | 2.0 (2.0) | 6.3x (6.5x) |
The queries, SQL then Cypher:
SELECT count(*) AS c FROM V WHERE x > 0.5 MATCH (n:V) WHERE n.x > 0.5 RETURN count(*) AS c
SELECT sum(x) AS c FROM V MATCH (n:V) RETURN sum(n.x) AS c
SELECT max(x) AS c FROM V MATCH (n:V) RETURN max(n.x) AS c
SELECT grp, count(*) AS c, sum(x) AS s FROM V GROUP BY grp MATCH (n:V) RETURN n.grp AS grp, count(*) AS c, sum(n.x) AS s
SELECT grp, count(*) AS c FROM V WHERE x > 0.5 GROUP BY grp MATCH (n:V) WHERE n.x > 0.5 RETURN n.grp AS grp, count(*) AS c
Every Cypher answer equals the SQL answer (the repro sums every numeric value of every row and prints it for both). An unfiltered count(*) is answered from the type's count in both languages (0.1-0.2 ms), so it is not in the table.
Per row the Cypher consumer is the cheaper of the two: about 340 ns of CPU per vertex for sum(x), against about 670 ns summed over SQL's workers. Partial aggregation in the workers, the split SQL uses since #8524 (per-worker counts, sums, extremes, and per-group maps, merged at the end), together with the parallel scan for an unfiltered label scan that feeds an aggregate, could bring Cypher level with SQL here.
Repro: AggParallel.java (standalone main, engine jar and its dependencies on the classpath)
java -Xmx6g -cp 'lib/*:.' AggParallel /tmp/agg-db 2000000 8
import com.arcadedb.database.*;
import com.arcadedb.query.sql.executor.*;
import java.lang.management.ManagementFactory;
import java.util.*;
/**
* Whole-type aggregates, SQL against openCypher, outside a transaction. N vertices of type V in BUCKETS buckets,
* grp uniform in [0, 100), x uniform in [0, 1). Each query: WARM untimed runs, then RUNS timed; prints the p50 wall
* time, the process CPU time over wall time during the timed runs (about 1 = one core), and the answer's checksum
* so SQL and Cypher can be compared.
*
* java AggParallel <dbDir> <n> <buckets>
*/
public class AggParallel {
static final int WARM = Integer.getInteger("warm", 5), RUNS = Integer.getInteger("runs", 9);
public static void main(String[] a) throws Exception {
final int n = Integer.parseInt(a[1]), buckets = Integer.parseInt(a[2]);
System.out.println("\nRESULT engine " + com.arcadedb.Constants.getRawVersion() + " " + com.arcadedb.Constants.getBuildNumber() + ", java "
+ System.getProperty("java.version") + ", cpus " + Runtime.getRuntime().availableProcessors() + ", n=" + n + ", buckets " + buckets);
final DatabaseFactory f = new DatabaseFactory(a[0]);
if (f.exists())
f.open().drop();
final Database db = f.create();
db.command("sql", "CREATE VERTEX TYPE V BUCKETS " + buckets);
db.command("sql", "CREATE PROPERTY V.grp INTEGER");
db.command("sql", "CREATE PROPERTY V.x DOUBLE");
final Random rnd = new Random(11);
db.begin();
for (int i = 0; i < n; i++) {
db.newVertex("V").set("grp", rnd.nextInt(100), "x", rnd.nextDouble()).save();
if ((i + 1) % 50_000 == 0) {
db.commit();
db.begin();
}
}
db.commit();
final String[][] qs = {
{ "count(*) all", "sql", "SELECT count(*) AS c FROM V", "MATCH (n:V) RETURN count(*) AS c" },
{ "count(*) WHERE x > 0.5", "sql", "SELECT count(*) AS c FROM V WHERE x > 0.5", "MATCH (n:V) WHERE n.x > 0.5 RETURN count(*) AS c" },
{ "sum(x)", "sql", "SELECT sum(x) AS c FROM V", "MATCH (n:V) RETURN sum(n.x) AS c" },
{ "GROUP BY grp: count, sum", "sql", "SELECT grp, count(*) AS c, sum(x) AS s FROM V GROUP BY grp", "MATCH (n:V) RETURN n.grp AS grp, count(*) AS c, sum(n.x) AS s" },
{ "GROUP BY grp WHERE x > 0.5", "sql", "SELECT grp, count(*) AS c FROM V WHERE x > 0.5 GROUP BY grp", "MATCH (n:V) WHERE n.x > 0.5 RETURN n.grp AS grp, count(*) AS c" },
{ "max(x)", "sql", "SELECT max(x) AS c FROM V", "MATCH (n:V) RETURN max(n.x) AS c" },
{ "distinct grp, counted", "sql", "SELECT count(*) AS c FROM (SELECT DISTINCT grp FROM V)", "MATCH (n:V) RETURN count(DISTINCT n.grp) AS c" } };
for (String[] q : qs) {
final String s = run(db, "sql", q[2]), c = run(db, "opencypher", q[3]);
System.out.printf("%nRESULT %-28s SQL %s | Cypher %s%n", q[0], s, c);
}
db.close();
}
static String run(Database db, String lang, String q) {
final var mx = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
double sum = 0;
for (int i = 0; i < WARM; i++)
sum = exec(db, lang, q);
final double[] ms = new double[RUNS];
final long cpu0 = mx.getProcessCpuTime(), w0 = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
final long t0 = System.nanoTime();
sum = exec(db, lang, q);
ms[i] = (System.nanoTime() - t0) / 1e6;
}
final double cores = (mx.getProcessCpuTime() - cpu0) / (double) (System.nanoTime() - w0);
Arrays.sort(ms);
return String.format("%8.1f ms %4.1f cores sum %.4f", ms[RUNS / 2], cores, sum);
}
/** A checksum of the answer: every numeric value of every row, summed. */
static double exec(Database db, String lang, String q) {
double s = 0;
try (ResultSet rs = db.query(lang, q)) {
while (rs.hasNext()) {
final Result r = rs.next();
for (String p : r.getPropertyNames())
if (r.getProperty(p) instanceof Number num)
s += num.doubleValue();
}
}
return s;
}
}
Activity
bindings docs: whole-type aggregates and distinct values, SQL GROUP B…
fix(#8799,#8797): address review - keep streaming DISTINCT outside pa…
test(#8799,#8797): array-valued DISTINCT equivalence and inline patte…
test(#8797,#8799): stronger DISTINCT value assertions, group-limit te…
perf(#8799): skip DISTINCT rewrite on small types; test(#8797): merge…
Merge pull request #8824 from ArcadeData/fix/8799-8797-8795-parallel-…
lvca commentedon Oct 1, 2026
Fixed in #8824 (26.10.1). OpenCypher
count,sum,avg,minandmax, with or without grouping, now run on all cores over a label scan, filtered or not.DISTINCTaggregates,collect()and aggregates over function calls still run on one thread. The sum or average of floating-point values can differ in the last digits between runs.❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.
tae898 commentedon Oct 1, 2026
Verified on the merge,
4be91b595c(engine built from it), against3d5e1a514c: 2,000,000 vertices in 8 buckets, 12 cores, p50 of 9 after 5, Temurin 25 with compact object headers and Temurin 21 in brackets. Every answer equals the SQL one (sums to the last digits you mentioned).3d5e1a514c4be91b595cMATCH (n:V) RETURN sum(n.x)MATCH (n:V) RETURN n.grp, count(*), sum(n.x)MATCH (n:V) WHERE n.x > 0.5 RETURN count(*)MATCH (n:V) RETURN max(n.x)MATCH (n:V) WHERE n.x > 0.5 RETURN n.grp, count(*)openCypher is now ahead of SQL on four of the five (SQL
sum(x)128 ms, the group-by 221 ms on Temurin 25).RETURN DISTINCT n.grpandcount(DISTINCT n.grp)stay on one thread (1,361 ms and 827 ms), as your note says. Glad the result surprised you in the right direction, and thank you.bindings docs: queries guide describes 26.10.1 as fixed, Cypher aggre…
perf(sql,opencypher): parallel DISTINCT, Cypher partial aggregation i…
perf(sql,opencypher): parallel DISTINCT, Cypher partial aggregation i…
fix(#8799,#8797): address review - keep streaming DISTINCT outside pa…
test(#8799,#8797): array-valued DISTINCT equivalence and inline patte…
test(#8797,#8799): stronger DISTINCT value assertions, group-limit te…
perf(#8799): skip DISTINCT rewrite on small types; test(#8797): merge…