@query-doctor/core 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1122,17 +1122,17 @@ var _IndexOptimizer = class _IndexOptimizer {
1122
1122
  }
1123
1123
  const baseIndexes = this.findUsedIndexes(baseExplain.Plan);
1124
1124
  const finalIndexes = this.findUsedIndexes(finalExplain.Plan);
1125
+ const triedIndexes = new Map(
1126
+ toCreate.map((index) => [index.name.toString(), index])
1127
+ );
1128
+ this.replaceUsedIndexesWithDefinition(finalExplain.Plan, triedIndexes);
1125
1129
  return {
1126
1130
  kind: "ok",
1127
1131
  baseCost,
1128
1132
  finalCost,
1129
1133
  newIndexes: finalIndexes.newIndexes,
1130
1134
  existingIndexes: baseIndexes.existingIndexes,
1131
- triedIndexes: new Map(
1132
- // TODO: don't prematurely stringify PgIdentifier here
1133
- // (requires more work)
1134
- toCreate.map((index) => [index.name.toString(), index])
1135
- ),
1135
+ triedIndexes,
1136
1136
  baseExplainPlan: baseExplain.Plan,
1137
1137
  explainPlan: finalExplain.Plan
1138
1138
  };
@@ -1351,8 +1351,8 @@ var _IndexOptimizer = class _IndexOptimizer {
1351
1351
  const newIndexes = /* @__PURE__ */ new Set();
1352
1352
  const existingIndexes = /* @__PURE__ */ new Set();
1353
1353
  const prefix = _IndexOptimizer.prefix;
1354
- function go(plan) {
1355
- const indexName = plan["Index Name"];
1354
+ walkExplain(explain, (stage) => {
1355
+ const indexName = stage["Index Name"];
1356
1356
  if (indexName) {
1357
1357
  if (indexName.startsWith(prefix)) {
1358
1358
  newIndexes.add(indexName);
@@ -1363,21 +1363,36 @@ var _IndexOptimizer = class _IndexOptimizer {
1363
1363
  existingIndexes.add(indexName);
1364
1364
  }
1365
1365
  }
1366
- if (plan.Plans) {
1367
- for (const p of plan.Plans) {
1368
- go(p);
1369
- }
1370
- }
1371
- }
1372
- go(explain);
1366
+ });
1373
1367
  return {
1374
1368
  newIndexes,
1375
1369
  existingIndexes
1376
1370
  };
1377
1371
  }
1372
+ replaceUsedIndexesWithDefinition(explain, triedIndexes) {
1373
+ walkExplain(explain, (stage) => {
1374
+ const indexName = stage["Index Name"];
1375
+ if (indexName) {
1376
+ const recommendation = triedIndexes.get(indexName);
1377
+ if (recommendation) {
1378
+ stage["Index Name"] = recommendation.definition;
1379
+ }
1380
+ }
1381
+ });
1382
+ }
1378
1383
  };
1379
1384
  __publicField(_IndexOptimizer, "prefix", "__qd_");
1380
1385
  var IndexOptimizer = _IndexOptimizer;
1386
+ function walkExplain(explain, f) {
1387
+ function go(plan) {
1388
+ f(plan);
1389
+ if (plan.Plans) {
1390
+ for (const p of plan.Plans) {
1391
+ go(p);
1392
+ }
1393
+ }
1394
+ }
1395
+ }
1381
1396
  var RollbackError = class {
1382
1397
  constructor(value) {
1383
1398
  this.value = value;