On postgresql@12 I have a table with 300 millions rows and first query executes < 1s, but the second is 2 minutes. Why? I created all possible indexes.
First query (< 1s):
<code>SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC
LIMIT 1000;
</code>
<code>SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC
LIMIT 1000;
</code>
SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC
LIMIT 1000;
Second query (2 mins):
<code>SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC, transactionIndex ASC
LIMIT 1000;
</code>
<code>SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC, transactionIndex ASC
LIMIT 1000;
</code>
SELECT hash FROM _transfers
WHERE (from_=‘abcd’ or to_=‘a’bcd) and blockNumber<=12345
ORDER BY blockNumber DESC, transactionIndex ASC
LIMIT 1000;
Indexes:
<code>create index on _transfers(from_);
create index on _transfers(to_);
create index on _transfers(blockNumber);
create index on _transfers(from_, to_, blockNumber);
create index on _transfers(transactionIndex);
create index on _transfers(blockNumber, transactionIndex);
create index on _transfers(from_, to_, blockNumber, transactionIndex);
create index on _transfers(from_, to_) include(blockNumber, transactionIndex);
</code>
<code>create index on _transfers(from_);
create index on _transfers(to_);
create index on _transfers(blockNumber);
create index on _transfers(from_, to_, blockNumber);
create index on _transfers(transactionIndex);
create index on _transfers(blockNumber, transactionIndex);
create index on _transfers(from_, to_, blockNumber, transactionIndex);
create index on _transfers(from_, to_) include(blockNumber, transactionIndex);
</code>
create index on _transfers(from_);
create index on _transfers(to_);
create index on _transfers(blockNumber);
create index on _transfers(from_, to_, blockNumber);
create index on _transfers(transactionIndex);
create index on _transfers(blockNumber, transactionIndex);
create index on _transfers(from_, to_, blockNumber, transactionIndex);
create index on _transfers(from_, to_) include(blockNumber, transactionIndex);