Abstract
Query optimization in RDF systems is critical for performance but often hampered by inaccurate cardinality estimations. This paper introduces Shape Statistics, a lightweight approach that extends the SHACL standard with statistical metadata. By capturing class-property correlations, Shape Statistics enables more accurate join ordering with significantly lower preprocessing and storage overhead than existing state-of-the-art methods.
Introduction
In the world of Resource Description Framework (RDF) and SPARQL, query optimization is notoriously difficult. To find the most efficient execution plan, an optimizer must estimate the "cardinality" (the number of results) of intermediate steps. Most current systems rely on global statistics, which look at the dataset as a whole. However, RDF data is often highly correlated; for instance, a "Professor" is much more likely to have a "publications" property than a "Student." Global statistics miss these nuances, leading to poor execution plans.
As illustrated in the figure above, complex graph patterns involve multiple joins. Inaccurate cardinality estimates for these patterns can lead to suboptimal join ordering and significantly degraded query performance. While existing "fine-grained" methods like Characteristic Sets or Graph Summaries address this, they are extremely expensive to compute and can require gigabytes of storage for a single dataset.
Methodology: Shape Statistics
The authors propose repurposing SHACL (Shapes Constraint Language), a W3C standard usually used for data validation, as a source for query optimization.
Extending SHACL with Statistics
The core innovation involves augmenting SHACL node and property shapes with specific statistical attributes:
- sh:count: Total number of triples or instances.
- sh:minCount / sh:maxCount: Bounds on property occurrences per instance.
- sh:distinctCount: Number of unique objects for a property.
Preprocessing and Optimization
These statistics are gathered by running analytical SPARQL queries over the dataset during a one-time preprocessing phase. When a query arrives, the system maps triple patterns to their corresponding SHACL shapes. It then uses a set of formulas—combining global and shape-specific stats—to estimate the size of results and the cost of joins (Subject-Subject, Subject-Object, etc.). A greedy algorithm then uses these precise estimates to refine the join order.
Evaluation
The approach was evaluated on three major benchmarks: LUBM-500 (~91M triples), WatDiv (up to 1B triples), and the real-world YAGO-4 dataset (~210M triples).
Key Results
- Preprocessing Speed: Shape Statistics were generated 2x to 4x faster than Characteristic Sets or SumRDF. For YAGO-4, it took ~1 hour compared to over 8 hours for Characteristic Sets.
- Storage Footprint: The extended SHACL graphs are tiny—often just a few dozen kilobytes—representing a 100x reduction in space compared to other summary methods.
- Query Performance: The Shape Statistics approach found the best query plan in 75% of cases. It significantly outperformed default optimizers in Apache Jena and was competitive with specialized estimators like SumRDF.
- Accuracy: The q-error (the ratio between estimated and true cardinality) was consistently lower, particularly for complex "snowflake" and "star" queries.
Conclusion
Shape Statistics leverages existing standards to provide a powerful optimization tool for "free," without inventing new proprietary formats. By associating properties with specific classes, the optimizer avoids the independence assumption trap. Future work includes direct integration into SPARQL engines and expanding support for complex operators beyond basic graph patterns.
Summary updated