Skip to main content

Calibrating the Lucene query optimizer

Info

A query with a condition on a child case (for example address.county:xyz) can be executed in two ways: root first or child first. The optimizer of Mercury DB (HgDB) 3.0 chooses the way with a threshold, lucene.query.cost.childFirstRatio. The best value depends on the data and on the version of the engine, so it should be measured, not guessed. The command -lc of mercury-cmd measures it on the index of a node and prints the result. It never changes the configuration itself.

How the optimizer chooses​

For a query with a child condition the optimizer counts the hits of the root part (nRoot) and of the child part (nChild) with a cardinality probe (the count stops at lucene.query.cost.probeLimit). Then:

  • root first when nChild >= childFirstRatio × nRoot (the child condition is not selective enough to start from it),
  • child first otherwise.

Both plans return the same result. They differ in time: sometimes by an order of magnitude in either direction. The query plan (see Lucene queries monitor) shows the decision, for example:

DECISION: CHILD_FIRST by PROBE (nRoot=200; nChild=2; childFirstRatio=0.5; probeLimit=3000000; probeMs=1; hints: useCostOptimization=true, ruleRootFirst=false)
ACTUAL: 3 ms, hits=2

The command​

The command runs on the host of the node, in its own process, and reads the index of the node read only (or a copy given by --index-copy).

mercury-cmd.sh lc --generate 40 --out /tmp/calibration
mercury-cmd.sh lc --queries sample.txt --index-copy /data/index-copy --max-seconds 1800 --pause-ms 50
OptionMeaning
--queries <file>file with queries (see below); can be combined with --generate
--generate <N>generate N queries from the index (model 3.0); --seed <n> fixes the choice
--max-child-hits <n>the generator skips queries with more child hits (default 300000, see Safety)
--save-queries <file>save the queries that were used in the same file format
--out <directory>directory for the results (default lucene-calibration-<time>)
--ratios a,b,ctested thresholds (default 0.05 to 1024, doubling)
--limits a,b,ctested probe limits (default 1000000, 3000000, 10000000)
--runs <n>runs of each fixed plan, the best time counts (default 2)
--max-queries <n>, --max-seconds <n>, --pause-ms <n>limits that protect the node
--noise-ms <n>queries faster than this are noise (default 100)
--index-copy <directory>directory with the subdirectory Case: use a copy of the index instead of the node's index
--dry-runonly prepare and save the queries, no measurement
--yesdo not ask for confirmation

Safety​

The measurement executes every query several times, so it loads the CPU and the disk of the host:

  • it runs in one thread, with --max-queries, --max-seconds and --pause-ms to limit the load,
  • it asks for confirmation (--yes skips the question; without a console it refuses),
  • use a quiet period, or better a copy of the index (--index-copy),
  • the plan child first costs about 0.26 ms per child hit (measured on one index), so a query with millions of child hits can take minutes; the generator skips such queries (--max-child-hits). Queries that you supply yourself are not skipped.

Where the queries come from​

  • A file of the client (--queries): UTF-8, one query per line, optionally name<TAB>query; empty lines and lines starting with # are skipped; duplicates and queries cut by the monitor (ending with …) are skipped with a warning.
  • The generator (--generate): reads the statistics of the index (model 3.0: fields <field>_mrc_Case_id, mrc_parentFields, mrc_parentTypes), samples the documents and builds queries of the form mrc_typeCodeValue:<root> [AND <attribute>:<value>] AND <field>.mrc_typeCodeValue:<child> AND <field>.<attribute>:<value> with a spread of ratios nChild/nRoot (from 0.001 to 1000).
  • The monitor (Lucene queries monitor): the button Save sample for calibration writes real queries of the node in the same file format.
Personal data

The generator takes the values from the index and the monitor sample contains real queries of the users, so the files contain personal data (names, addresses, logins). Treat them as personal data and do not publish them.

What the command does​

  1. For each query and each tested probe limit it prepares the plan without executing it and reads the counts and the time of the probe from the decision of the optimizer.
  2. It executes the query in the two fixed plans (root first and child first), --runs times each, and compares the results (hits and content). A query with different results is reported and skipped.
  3. It simulates which plan every tested threshold and limit would choose and what time it would give (time of the chosen plan plus time of the probe). No further executions are needed.
  4. It rates every pair (threshold, limit) with the criterion: a query is satisfied when its regret (time of the chosen plan divided by the time of the best fixed plan) is at most 1.25 or the difference is at most 100 ms. The target is 90% of the queries.
  5. It writes the report and a fragment of lucene.properties.

Queries with no subquery or with more than one subquery are reported with a status and do not take part in the rating.

Results​

In the directory given by --out: report.txt (tables, recommendation, warnings), measurements.csv (one line per query, saved after each query, so an interrupted run keeps its results), queries.txt (the queries used) and recommended.properties. The command does not change lucene.properties: review the recommendation and apply it yourself.

The report warns when: the target of 90% is not reached, the best thresholds reach the edge of the tested range (widen it with --ratios), the probe limit was not exercised (no count reached it, so the limit does not matter on this data and no value is recommended), or only few queries are above the noise level (the result is weak).

Interpreting the result​

  • The optimum depends on the version of the engine and on the data. After a change that moves the cost of one of the plans, calibrate again.
  • A sharp optimum (the number of satisfied queries changes a lot between neighbouring thresholds) means that a few queries decide: use a larger and more representative sample.
  • The measurement needs lucene.query.maxHits above the number of root hits. With a smaller limit the plan root first truncates the set of roots before it applies the child condition and returns fewer results than child first, so the comparison is not valid (the command reports it as different results).
  • Example (index of 22 million documents, 40 generated queries, repeated twice with the same result): child first was faster for most queries and the best threshold was far above the default 2.0 (about 256), but the sample favours queries with large child sets. The default value was not changed on this basis; one index is not enough. Calibrate on the data of the environment and on real queries from the monitor before you change the parameters.