Benjamin Habegger created OAK-12348:
---------------------------------------

             Summary: Property and node type indexes should support 
costPerEntry / costPerExecution overrides
                 Key: OAK-12348
                 URL: https://issues.apache.org/jira/browse/OAK-12348
             Project: Jackrabbit Oak
          Issue Type: Improvement
          Components: query
            Reporter: Benjamin Habegger


h3. Summary
Property and node type indexes ({{PropertyIndex}}, {{NodeTypeIndex}} in 
{{oak-core}}) compute their query cost purely from indexed entry counts, with a 
fixed cost overhead of 2 ({{PropertyIndexPlan.COST_OVERHEAD}} / 
{{PropertyIndexLookup.COST_OVERHEAD}}). Unlike Lucene/Elastic indexes, where 
{{oak-search}}'s {{IndexDefinition.getCostPerEntry()}} / 
{{getCostPerExecution()}} let an index definition tune its own cost estimate, 
there is no equivalent knob for property/node type indexes.

In production this is a real problem: the entry-count estimate for a 
property/node type index can be significantly wrong (for example, based on an 
approximate count that still includes since-deleted entries), causing the query 
planner to prefer the property/node type index over a more selective, 
purpose-built index for the same query — large scans, slow sorts, and timeouts 
follow. The workaround today is to add {{OPTION(INDEX TAG ...)}} to every 
affected query and tag the intended index, which requires touching the query 
itself rather than correcting the misestimate at its source, and doesn't help 
queries whose source can't be changed quickly (framework-generated queries, 
third-party code, etc).

h3. Proposal
Add optional {{costPerEntry}} (double, default {{1.0}}) and 
{{costPerExecution}} (double, default {{2.0}}, matching the current 
{{COST_OVERHEAD}}) properties to the property index definition 
({{oak:QueryIndexDefinition}} with {{type=property}}), using the same property 
names {{oak-search}} already uses for Lucene/Elastic. Read them in 
{{PropertyIndexPlan}} and {{PropertyIndexLookup.getCost()}} and combine as:

{{cost = costPerExecution + costPerEntry * estimatedEntryCount}}

Defaults reproduce today's behaviour exactly (no change for existing 
repositories/index definitions).

{{NodeTypeIndex}} gets this for free: its cost is the sum of two 
{{PropertyIndexLookup.getCost()}} calls, on the property indexes for 
{{jcr:primaryType}} and {{jcr:mixinTypes}}. Setting 
{{costPerEntry}}/{{costPerExecution}} on those two property index definitions 
therefore also tunes node type index selection, without any change to 
{{NodeTypeIndex}} itself.

h3. Out of scope
{{resultCacheSize}} is a Lucene/Elastic query-result caching concept with no 
equivalent in the property/node type index's 
{{ContentMirrorStoreStrategy}}-based lookup — there is no result cache to size 
in the same sense, so it is not part of this change.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to