Optimize SQL queries used in handling account_tx:

Profiling and research indicates that the SQLite query planner executed
our existing SQL queries sub-optimally by not using the index efficiently.

Restructuring the SQL query works around this issue and allows queries
to be executed efficiently and without unnecessary delay.
This commit is contained in:
Mark Travis
2017-12-31 20:39:54 -08:00
committed by Nikolaos D. Bougalis
parent d2fc4e3569
commit 5e4dac41a7

View File

@@ -142,19 +142,27 @@ accountTxPage (
} }
else if (forward && (findLedger != 0)) else if (forward && (findLedger != 0))
{ {
auto b58acct = idCache.toBase58(account);
sql = boost::str (boost::format( sql = boost::str (boost::format(
prefix + (R"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
(R"( Status,RawTxn,TxnMeta
AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u' OR FROM AccountTransactions, Transactions WHERE
( AccountTransactions.LedgerSeq = '%u' AND (AccountTransactions.TransID = Transactions.TransID AND
AccountTransactions.TxnSeq >= '%u' ) AccountTransactions.Account = '%s' AND
AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u')
OR
(AccountTransactions.TransID = Transactions.TransID AND
AccountTransactions.Account = '%s' AND
AccountTransactions.LedgerSeq = '%u' AND
AccountTransactions.TxnSeq >= '%u')
ORDER BY AccountTransactions.LedgerSeq ASC, ORDER BY AccountTransactions.LedgerSeq ASC,
AccountTransactions.TxnSeq ASC AccountTransactions.TxnSeq ASC
LIMIT %u; LIMIT %u;
)")) )"))
% idCache.toBase58(account) % b58acct
% (findLedger + 1) % (findLedger + 1)
% maxLedger % maxLedger
% b58acct
% findLedger % findLedger
% findSeq % findSeq
% queryLimit); % queryLimit);
@@ -174,17 +182,27 @@ accountTxPage (
} }
else if (!forward && (findLedger != 0)) else if (!forward && (findLedger != 0))
{ {
auto b58acct = idCache.toBase58(account);
sql = boost::str (boost::format( sql = boost::str (boost::format(
prefix + (R"(SELECT AccountTransactions.LedgerSeq,AccountTransactions.TxnSeq,
(R"(AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u' OR Status,RawTxn,TxnMeta
(AccountTransactions.LedgerSeq = '%u' AND FROM AccountTransactions, Transactions WHERE
AccountTransactions.TxnSeq <= '%u') (AccountTransactions.TransID = Transactions.TransID AND
ORDER BY AccountTransactions.LedgerSeq DESC, AccountTransactions.Account = '%s' AND
AccountTransactions.TxnSeq DESC AccountTransactions.LedgerSeq BETWEEN '%u' AND '%u')
LIMIT %u;)")) OR
% idCache.toBase58(account) (AccountTransactions.TransID = Transactions.TransID AND
AccountTransactions.Account = '%s' AND
AccountTransactions.LedgerSeq = '%u' AND
AccountTransactions.TxnSeq <= '%u')
ORDER BY AccountTransactions.LedgerSeq DESC,
AccountTransactions.TxnSeq DESC
LIMIT %u;
)"))
% b58acct
% minLedger % minLedger
% (findLedger - 1) % (findLedger - 1)
% b58acct
% findLedger % findLedger
% findSeq % findSeq
% queryLimit); % queryLimit);