Experiment environment for course Topic 3, 智能索引推荐与设计 (intelligent index recommendation).
The project compares feedback, a recommender that builds each index it considers and keeps it only if the queries it changes run faster, with greedy, a baseline that relies on HypoPG's what-if estimates:
- On TPC-H SF10,
feedbackfinished the workload 10–23% faster thangreedyin all nine runs, three at each storage budget of 10%, 25%, and 50%. - On JOB, it was 38–49% faster at the 25% and 50% budgets, but about 7% slower at 10%.
- Every query returned the correct answer in every run.
- A complete repeat of the experiment on 2026-11-27 reproduced every one of these findings (comparison).
One command runs the report's final experiment again and compares the new results with the report:
./reproduceIt asks before it starts, then runs for about 7.5 hours in a background container, so closing the terminal does not stop it. Keep the computer awake and Docker running until it finishes. It needs:
- Docker with at least 12 CPUs and 30 GB of memory, because the report gave PostgreSQL 8 CPUs and 24 GB and the benchmark client 4 more CPUs and 5 GB. On a Mac, set this in Docker Desktop under Settings > Resources.
- About 100 GB of free Docker disk space.
- An internet connection, to download the 1.26 GB of IMDB data that JOB uses. DuckDB generates the TPC-H data.
It loads the data that are missing, rehearses the whole path on TPC-H SF1, and then makes every recommendation, times every configuration in the report's fixed order, and measures maintenance, exactly as the report's final session did. The comparison is written to results/experiments/reproduction/comparison.md. Absolute times depend on the machine, so it checks whether each of the report's findings holds again. ./reproduce status shows how far it has got, and running ./reproduce again after an interruption resumes from the last finished step.
Two shorter checks need no long run:
| Command | Time | Needs | What it shows |
|---|---|---|---|
./reproduce check |
About 10 seconds | Python 3.9 or later, or Docker | Every run summary agrees with the log of every query execution, every answer matched its reference, and every number in the report follows from the raw results in results/ |
./reproduce quick |
10 to 20 minutes | Docker with 2 CPUs and 8 GB | The whole pipeline runs on TPC-H SF1, with every answer checked against the official TPC-H answers |
A new machine loads the data again, and PostgreSQL then collects its planner statistics from a new random sample of each table. A few plans, and the indexes chosen for them, can therefore differ from the report's.
| Part | Setting |
|---|---|
| Database | PostgreSQL 17.12 with HypoPG 1.4.5, in Docker |
| Resource limits | 24 GB memory, no swap, 8 CPUs |
| Network | No published port, reachable only from the tools container |
| Databases | tpch_sf1, tpch_sf10, tpch_sf30, job |
| No-new-index baseline | Primary keys only |
The fixed database settings are in postgresql.conf. Keep them the same for every method you compare.
Start PostgreSQL and check the environment:
docker compose up -d --wait postgres./bench statusStop it when you are done, which frees its memory and keeps the data:
docker compose stopPut your recommended indexes in a file of CREATE INDEX statements, such as configs/my_method.sql. Then measure the baseline and your configuration:
./bench indexes reset --db tpch_sf10./bench run --db tpch_sf10 --label baseline./bench indexes apply --db tpch_sf10 --file configs/my_method.sql./bench run --db tpch_sf10 --label my_method./bench compare <baseline-result-folder> <my-method-result-folder>Measure insert, delete, and vacuum cost last, because this step changes the physical layout of the tables. Then remove the added indexes:
./bench refresh --db tpch_sf10 --label my_method./bench indexes reset --db tpch_sf10run defaults to one warm-up repeat, three measured repeats, and a 600-second timeout per query. Warm-up times are left out of the results, but warm-up answers are still checked. A query that times out is not retried in later repeats. If the measured repeat totals differ by more than 5% of their median, run prints a warning and sets stable to false in summary.json. Rerun that configuration before using its numbers.
Use HypoPG to estimate an index without building it:
./bench whatif --db tpch_sf10 --query q17 --index 'CREATE INDEX ON lineitem (l_partkey)'Both methods use the same candidate indexes, PostgreSQL cost estimates, and storage budget. The budget is a share of the database size with primary keys only.
greedyis the baseline. It uses HypoPG estimates only. At each step it takes the single-column index, or the widening of a chosen index, with the largest estimated saving per byte.feedbackis our method. It asks the same search for its next index, builds it, runs the queries whose plans change, and keeps it only if the real workload gets faster.sizedis an ablation. It runs the greedy search but builds each chosen index to learn its real size, and times no query, so comparing it withfeedbackshows what timing adds.
./bench recommend --db tpch_sf10 --method feedback --budget-percent 25This writes the chosen indexes to configs/generated/ and its costs to recommend.json. To run every method at every budget:
./bench experiment --name main --db tpch_sf10 job --budgets 10 25 50 --timeout 300Rerunning the same command resumes from the last finished step. Results go to results/experiments/<name>/, and summary.csv holds one row per method and budget.
To run the main experiment so that closing the app or a terminal does not stop it, use the launcher. It starts a separate Docker container, and running it again after an interruption resumes from the last finished step:
./run-experimentFollow its progress:
tail -f results/experiments/main/run.logThe Mac must stay awake and Docker Desktop must stay open. Run this in Terminal to prevent idle sleep until the experiment ends. A closed lid still puts the Mac to sleep:
caffeinate -i docker wait dbopt-experimentThe first pass left four results over the 5% stability limit, and the JOB baseline ended about 10% slower than it started. To time those comparisons again, close other applications and run:
./run-remeasureIt first repeats the JOB baseline and stops if that is more than 5% slower than the first pass, because the Mac is then too busy to measure. It then times both methods at JOB 10% and 50% and TPC-H 25% and 50%, repeats the JOB feedback recommendation at 10%, and repeats JOB maintenance. Each step is measured once, and its numbers replace the first pass whichever way they move. The first pass stays on disk, and the combined results are in results/experiments/main/remeasure/.
Safari was open for the later part of the second pass, and that pass's TPC-H baseline ended 8.43% slower than the first. To repeat only the work that overlapped another application, close other applications and run:
./run-third-passIt first repeats the full JOB and TPC-H baselines, the same way as the first pass, and stops if either differs from the first pass by more than 5% in either direction, because its timings could not then be combined with the earlier passes. It then repeats the feedback recommendation at JOB 50%, times both methods at JOB 50% and TPC-H 25% and 50%, and repeats JOB maintenance. The steps were chosen from their run times rather than their numbers, and the new numbers replace the earlier ones whichever way they move. Earlier passes stay on disk, and the combined results are in results/experiments/main/third-pass/.
On 2026-11-24 the third pass stopped at its first check, because JOB ran 9.4% faster than on 2026-11-23. Timings from different days therefore cannot be combined.
To time both baselines and every configuration again in one session, close other applications and run:
./run-one-sessionEach database gets a no-index baseline before and after its configurations, as in the first pass. Each method keeps the indexes it already chose, except that the feedback recommendation at JOB 50% runs again, because other applications were open during it. Recommendation times come from the run that made each choice. It takes about 2¼ hours, and the Mac must not restart while it runs. The results are in results/experiments/main/one-session/, and the earlier passes stay on disk.
It finished on 2026-11-24 with every timed result within the stability limit, and its results are the final ones. JOB's start and end baselines differed by 4.8%. TPC-H's differed by 8.5%, mostly from Q09, Q13, and Q18, while Q01 stayed within 6% across the session. Report TPC-H speedups over no indexes as a range between the two baselines.
The one-session run reused one recommendation per method. To recommend and time everything again in one overnight session, with feedback three times per budget and sized as an ablation, run:
./run-final-sessionIt rehearses the whole path on SF1 and continues only if that passes. For each database it then makes all 15 recommendations, times a no-index baseline, all 15 configurations in an order shuffled with a fixed seed, and a second baseline, and measures maintenance. Each step is measured once. It ran from 19:29 on 2026-11-24 to 02:43 on 2026-11-25. All 17 JOB runs were stable; 5 of the 17 TPC-H runs exceeded the spread limit, all between 21:47 and 22:38, when Q01 also slowed, so the Mac itself was briefly slower. Its results, in results/experiments/main/final-session/, are the ones the report uses.
To check that the final session's results repeat, it was run again with ./reproduce from 19:07 on 2026-11-27 to 02:14 on 2026-11-28, with the same code, settings, and seed, on the same Mac and loaded data. Every one of the report's findings held, the no-index baselines came within 0.3% of the final session's, and 26 of the 30 configurations chose exactly the same indexes. Section 5.4 of the report discusses it. Its results are in results/experiments/main/repeat-session/, with the comparison in comparison.md there, and its rehearsal is in results/setup_checks/repeat_session_rehearsal/.
The course report is private, so its text and the script that typesets it are not in this repository. Every number it prints is in report/data.json, which report/collect.py writes from results/experiments/main/final-session/ and, for its comparison with the repeat session, results/experiments/main/repeat-session/. report/analysis.py computes the bootstrap intervals and Wilcoxon tests, and ./reproduce check recomputes every value in report/data.json from the raw results. To rewrite it after new results, run:
python3 report/collect.pyEach run creates a folder in results/:
meta.jsonrecords versions, limits, settings, and the indexes present.queries.csvrecords every execution and marks it aswarmupormeasured.summary.csvrecords the median time and correctness status for each query.summary.jsonrecords workload totals, per-repeat totals and their spread, P50, P95, timeouts, errors, and mismatches.
apply writes index build time and size to build.json. refresh writes maintenance timings to refresh.json.
Environment checks are kept apart from experiment results in results/setup_checks/. To compare a run stored there, pass its full container path, such as /workspace/results/setup_checks/stability/<folder>.
- Correctness:
tpch_sf1is checked against the official TPC-H answers. The other databases are checked against saved reference results inbenchmarks/reference/. Those references came from a run with a helper index (configs/tpch_reference.sqlorbenchmarks/job/fkindexes.sql), and primary-key runs then matched them on all 113 JOB queries and on 20 of 22 TPC-H queries. Q17 and Q20 are too slow with primary keys only, so on SF10 and SF30 their references come from the indexed plan alone. - Timeouts: Timed-out queries are listed separately and left out of workload totals.
comparetotals only queries that finished in both runs. The experiment summary instead counts a timed-out query at the time limit, and also reports totals on only the queries the baseline finished. - Planner statistics: Statistics are collected once at load time. Do not run
ANALYZEbetween configurations, because resampling can change plans. - Refresh workload: The TPC-H version is TPC-H-style, not the official refresh files. Each round clones about 0.1% of orders and their line items, deletes them, and vacuums with forced index cleanup. JOB has no official update workload, so its version does the same with about 0.1% of the rows in
cast_info,movie_info,movie_keyword,movie_companies, andmovie_info_idx. The experiment measures this on a throwaway copy of the database. - Planner cost setting:
random_page_coststays at PostgreSQL's default of 4. On the queries whose plans differ, 1.1 and 4 each won three of six workload and index combinations by at least 5%, and the rule fixed before measuring keeps the default on a tie. The evidence is inresults/setup_checks/planner_setting/. - Timing stability: With primary keys only and one warm-up repeat, the four remaining workload totals differed by 3.75% on SF10, 2.82% on JOB, and 1.59% on SF30, so the experiments stay in Docker. The first repeat differed from the others by as much as 14%, which is why
rundiscards it. Treat differences under about 5% as noise. The evidence is inresults/setup_checks/stability/. - Candidates: Columns come from the filters, join keys, and sort and group keys in PostgreSQL's plans. Indexes have at most three columns, and columns too wide for a B-tree are left out.
- Feedback rule: An index is kept only if it saves at least 5% of the time of the queries whose plans it changes and at least 0.5% of the current workload time. These limits were set using TPC-H SF1 only, before any SF10 or JOB comparison.
- Held-out queries: On JOB, recommenders see 80 queries. The last variant of each of the 33 templates stays unseen, to test whether the chosen indexes generalize. TPC-H has no such split, so its results are tuned and scored on the same 22 queries.
- HypoPG limits: HypoPG does not model B-tree deduplication, so it can overestimate index size. On SF1, greedy's 400 MB estimate was 162 MB when built. Building or dropping a real index also invalidates hypothetical indexes on that table, so the search recreates them after every build.
- Timing noise: Close heavy applications during final runs. If this is a MacBook, keep it plugged in.
docker compose --profile tools build./bench load-tpch --sf 1 --replace./bench load-tpch --sf 10 --replace./bench load-tpch --sf 30 --replace./bench fetch-job./bench load-job --replaceTo load only the databases that are missing, as ./reproduce does, run:
./bench prepare --db tpch_sf1 tpch_sf10 job