-
I am using psygopg2 to connect to a TimescaleDB instance. I want to query the latest entries from a big table (35 million rows) containing price information of assets with the columns create table prices
(
datetime timestamptz not null,
asset_id integer references assets (asset_id),
price real not null,
created_at timestamptz not null default now(),
unique (datetime, asset_id)
); This table was turned into a TimescaleDB hypertable with select create_hypertable('"prices"', 'datetime'); and from this article's suggestion created the following index: create index latest_prices_idx on prices (asset_id, datetime desc); I use "Option 3" from this article to query the most recent price of each asset: select distinct on (asset_id) datetime, asset_id, price
from prices
order by asset_id, datetime desc; which takes ~300ms via my IDE's console. However, when doing the same query via psycopg2 it takes about 10 seconds: cursor.execute("""
select distinct on (asset_id) datetime, asset_id, price
from prices
order by asset_id, datetime desc;
""")
res = cursor.fetchall() Why might this be the case? Edit: Prepending |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The issue was with the IDE I was using (PyCharm). It implicitly limited the query results to 500, but was not showing this in the query logs... |
Beta Was this translation helpful? Give feedback.
The issue was with the IDE I was using (PyCharm). It implicitly limited the query results to 500, but was not showing this in the query logs...