Skip to content

Commit 6f8ff1c

Browse files
committed
plugins/sql: add payment_hash index to channelmoves table.
This significantly speeds up the query which bookkeeper often does: "SELECT created_index" " FROM channelmoves" " WHERE payment_hash = X'%s'" " AND credit_msat = %"PRIu64 " AND created_index <= %"PRIu64, On large databases this scan is expensive, and a payment_hash index cuts it down a great deal. It does take longer to load the channelmoves in the first place though (about 3x). Before: $ while sleep 10; do wc -l /tmp/bkpr-progress; done 169505 /tmp/bkpr-progress 196010 /tmp/bkpr-progress 219370 /tmp/bkpr-progress 235671 /tmp/bkpr-progress 244242 /tmp/bkpr-progress 255362 /tmp/bkpr-progress 265636 /tmp/bkpr-progress 276966 /tmp/bkpr-progress 284451 /tmp/bkpr-progress 288836 /tmp/bkpr-progress 296578 /tmp/bkpr-progress 304571 /tmp/bkpr-progress After: $ while sleep 10; do wc -l /tmp/bkpr-progress; done 161421 /tmp/bkpr-progress 238273 /tmp/bkpr-progress 281185 /tmp/bkpr-progress 305787 /tmp/bkpr-progress Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent d84850c commit 6f8ff1c

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

plugins/sql.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ static const struct index indices[] = {
197197
"channelmoves",
198198
{ "account_id", NULL },
199199
},
200+
{
201+
"channelmoves",
202+
{ "payment_hash", NULL },
203+
},
200204
};
201205

202206
static enum fieldtype find_fieldtype(const jsmntok_t *name)
@@ -1757,20 +1761,22 @@ static const char *fmt_indexes(const tal_t *ctx, const char *table)
17571761
for (size_t i = 0; i < ARRAY_SIZE(indices); i++) {
17581762
if (!streq(indices[i].tablename, table))
17591763
continue;
1760-
/* FIXME: Handle multiple indices! */
1761-
assert(!ret);
1764+
if (!ret)
1765+
ret = tal_fmt(ctx, " indexed by ");
1766+
else
1767+
tal_append_fmt(&ret, ", also indexed by ");
17621768
BUILD_ASSERT(ARRAY_SIZE(indices[i].fields) == 2);
17631769
if (indices[i].fields[1])
1764-
ret = tal_fmt(tmpctx, "`%s` and `%s`",
1765-
indices[i].fields[0],
1766-
indices[i].fields[1]);
1770+
tal_append_fmt(&ret, "`%s` and `%s`",
1771+
indices[i].fields[0],
1772+
indices[i].fields[1]);
17671773
else
1768-
ret = tal_fmt(tmpctx, "`%s`",
1769-
indices[i].fields[0]);
1774+
tal_append_fmt(&ret, "`%s`",
1775+
indices[i].fields[0]);
17701776
}
17711777
if (!ret)
17721778
return "";
1773-
return tal_fmt(ctx, " indexed by %s", ret);
1779+
return ret;
17741780
}
17751781

17761782
static const char *json_prefix(const tal_t *ctx,

0 commit comments

Comments
 (0)