From c1e958d2b9dbdbb9dafae3b7bce95ecd6da6c639 Mon Sep 17 00:00:00 2001 From: Karol Hosiawa Date: Sat, 23 Jan 2021 18:19:47 +0100 Subject: [PATCH] Added index to forwarded_payments.state to speedup lookups using this column --- wallet/db.c | 3 ++- wallet/db_postgres_sqlgen.c | 10 ++++++-- wallet/db_sqlite3_sqlgen.c | 10 ++++++-- wallet/statements_gettextgen.po | 42 ++++++++++++++++++--------------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index 6748b6d8e..fa513eb1b 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -669,12 +669,13 @@ static struct migration dbmigrations[] = { /* A reference into our own offers table, if it was made from one */ {SQL("ALTER TABLE payments ADD COLUMN local_offer_id BLOB DEFAULT NULL REFERENCES offers(offer_id);"), NULL}, {SQL("ALTER TABLE channels ADD funding_tx_remote_sigs_received INTEGER DEFAULT 0;"), NULL}, - /* Speeds up deletion of one peer from the database, measurements suggest * it cuts down the time by 80%. */ {SQL("CREATE INDEX forwarded_payments_out_htlc_id" " ON forwarded_payments (out_htlc_id);"), NULL}, {SQL("UPDATE channel_htlcs SET malformed_onion = 0 WHERE malformed_onion IS NULL"), NULL}, + /* Speed up forwarded_payments lookup based on state */ + {SQL("CREATE INDEX forwarded_payments_state ON forwarded_payments (state)"), NULL}, }; /* Leak tracking. */ diff --git a/wallet/db_postgres_sqlgen.c b/wallet/db_postgres_sqlgen.c index 27c67ee62..00ec570d9 100644 --- a/wallet/db_postgres_sqlgen.c +++ b/wallet/db_postgres_sqlgen.c @@ -884,6 +884,12 @@ struct db_query db_postgres_queries[] = { .placeholders = 0, .readonly = false, }, + { + .name = "CREATE INDEX forwarded_payments_state ON forwarded_payments (state)", + .query = "CREATE INDEX forwarded_payments_state ON forwarded_payments (state)", + .placeholders = 0, + .readonly = false, + }, { .name = "UPDATE vars SET intval = intval + 1 WHERE name = 'data_version' AND intval = ?", .query = "UPDATE vars SET intval = intval + 1 WHERE name = 'data_version' AND intval = $1", @@ -1786,10 +1792,10 @@ struct db_query db_postgres_queries[] = { }, }; -#define DB_POSTGRES_QUERY_COUNT 296 +#define DB_POSTGRES_QUERY_COUNT 297 #endif /* HAVE_POSTGRES */ #endif /* LIGHTNINGD_WALLET_GEN_DB_POSTGRES */ -// SHA256STAMP:4a878278ad7a1d427b80ff3a2abe34ae82cd73cf635ecf25e5cb20532c0604be +// SHA256STAMP:bbe38ba26543917c2c8be0eeba93c2d0345b51f7f29803e30cc3f03aaf077798 diff --git a/wallet/db_sqlite3_sqlgen.c b/wallet/db_sqlite3_sqlgen.c index ae8715eff..64dcd03d0 100644 --- a/wallet/db_sqlite3_sqlgen.c +++ b/wallet/db_sqlite3_sqlgen.c @@ -884,6 +884,12 @@ struct db_query db_sqlite3_queries[] = { .placeholders = 0, .readonly = false, }, + { + .name = "CREATE INDEX forwarded_payments_state ON forwarded_payments (state)", + .query = "CREATE INDEX forwarded_payments_state ON forwarded_payments (state)", + .placeholders = 0, + .readonly = false, + }, { .name = "UPDATE vars SET intval = intval + 1 WHERE name = 'data_version' AND intval = ?", .query = "UPDATE vars SET intval = intval + 1 WHERE name = 'data_version' AND intval = ?", @@ -1786,10 +1792,10 @@ struct db_query db_sqlite3_queries[] = { }, }; -#define DB_SQLITE3_QUERY_COUNT 296 +#define DB_SQLITE3_QUERY_COUNT 297 #endif /* HAVE_SQLITE3 */ #endif /* LIGHTNINGD_WALLET_GEN_DB_SQLITE3 */ -// SHA256STAMP:4a878278ad7a1d427b80ff3a2abe34ae82cd73cf635ecf25e5cb20532c0604be +// SHA256STAMP:bbe38ba26543917c2c8be0eeba93c2d0345b51f7f29803e30cc3f03aaf077798 diff --git a/wallet/statements_gettextgen.po b/wallet/statements_gettextgen.po index 36342d2f9..d8f70759f 100644 --- a/wallet/statements_gettextgen.po +++ b/wallet/statements_gettextgen.po @@ -574,75 +574,79 @@ msgstr "" msgid "ALTER TABLE channels ADD funding_tx_remote_sigs_received INTEGER DEFAULT 0;" msgstr "" -#: wallet/db.c:675 +#: wallet/db.c:674 msgid "CREATE INDEX forwarded_payments_out_htlc_id ON forwarded_payments (out_htlc_id);" msgstr "" -#: wallet/db.c:677 +#: wallet/db.c:676 msgid "UPDATE channel_htlcs SET malformed_onion = 0 WHERE malformed_onion IS NULL" msgstr "" -#: wallet/db.c:904 +#: wallet/db.c:678 +msgid "CREATE INDEX forwarded_payments_state ON forwarded_payments (state)" +msgstr "" + +#: wallet/db.c:905 msgid "UPDATE vars SET intval = intval + 1 WHERE name = 'data_version' AND intval = ?" msgstr "" -#: wallet/db.c:1004 +#: wallet/db.c:1005 msgid "SELECT version FROM version LIMIT 1" msgstr "" -#: wallet/db.c:1062 +#: wallet/db.c:1063 msgid "UPDATE version SET version=?;" msgstr "" -#: wallet/db.c:1070 +#: wallet/db.c:1071 msgid "INSERT INTO db_upgrades VALUES (?, ?);" msgstr "" -#: wallet/db.c:1082 +#: wallet/db.c:1083 msgid "SELECT intval FROM vars WHERE name = 'data_version'" msgstr "" -#: wallet/db.c:1109 +#: wallet/db.c:1110 msgid "SELECT intval FROM vars WHERE name= ? LIMIT 1" msgstr "" -#: wallet/db.c:1125 +#: wallet/db.c:1126 msgid "UPDATE vars SET intval=? WHERE name=?;" msgstr "" -#: wallet/db.c:1134 +#: wallet/db.c:1135 msgid "INSERT INTO vars (name, intval) VALUES (?, ?);" msgstr "" -#: wallet/db.c:1148 +#: wallet/db.c:1149 msgid "UPDATE channels SET feerate_base = ?, feerate_ppm = ?;" msgstr "" -#: wallet/db.c:1169 +#: wallet/db.c:1170 msgid "UPDATE channels SET our_funding_satoshi = funding_satoshi WHERE funder = 0;" msgstr "" -#: wallet/db.c:1185 +#: wallet/db.c:1186 msgid "SELECT type, keyindex, prev_out_tx, prev_out_index, channel_id, peer_id, commitment_point FROM outputs WHERE scriptpubkey IS NULL;" msgstr "" -#: wallet/db.c:1247 +#: wallet/db.c:1248 msgid "UPDATE outputs SET scriptpubkey = ? WHERE prev_out_tx = ? AND prev_out_index = ?" msgstr "" -#: wallet/db.c:1272 +#: wallet/db.c:1273 msgid "SELECT id, funding_tx_id, funding_tx_outnum FROM channels;" msgstr "" -#: wallet/db.c:1291 +#: wallet/db.c:1292 msgid "UPDATE channels SET full_channel_id = ? WHERE id = ?;" msgstr "" -#: wallet/db.c:1314 +#: wallet/db.c:1315 msgid "SELECT c.id, p.node_id, c.last_tx, c.funding_satoshi, c.fundingkey_remote, c.last_sig FROM channels c LEFT OUTER JOIN peers p ON p.id = c.peer_id;" msgstr "" -#: wallet/db.c:1381 +#: wallet/db.c:1382 msgid "UPDATE channels SET last_tx = ? WHERE id = ?;" msgstr "" @@ -1181,4 +1185,4 @@ msgstr "" #: wallet/test/run-wallet.c:1399 msgid "INSERT INTO channels (id) VALUES (1);" msgstr "" -# SHA256STAMP:e50580171fbee6db0aa824ddc75693d353453c74f4d65c8ca4daf023d7858dc1 +# SHA256STAMP:9cbdc346b00eec65089e37237361242a6c3afa2c50d2e4ae0210060a08f1a738