I'm trying to understand why my queries are having huge performance difference in my case.
I have this table has columns: timestamp, ticker, open, high, low, close, volume, exchange. The database is Postgres 9.6.
The table is around 300 million rows.
I've built the following index:
- unique index on (timestamp, ticker, exchange),
- index on (ticker),
- index on (exchange).
My query is as follows
SELECT MAX(timestamp) FROM table WHERE ticker='ticker1' AND exchange='exchange1';
But for different values for tickers, I'm having huge query time difference, ranging from 300ms to 7mins.
I'm trying to understand what is causing this and if I can improve it in any ways.
More information:
create table ohlcv (
timestamp bigint,
ticker varchar(20),
open double precision,
high double precision,
low double precision,
close double precision,
volume double precision,
exchange varchar(20),
constraint ohlcv_timestamp_ticker_exchange_key
unique (timestamp, ticker, exchange)
);
create index ohlcv_exchange_index on ohlcv (exchange);
create index ohlcv_ticker_index on ohlcv (ticker);
explain (analyze, buffers, format text)(not just a "simple" explain) as formatted text and make sure you prevent the indention of the plan. Paste the text, then put```on the line before the plan and on a line after the plan. Please also include completecreate indexstatements for all indexes as well.