Question Builder: Generate SQL with naive dates instead of localized timestamps #20333
Labels
Database/Postgres
.Performance
Priority:P2
Average run of the mill bug
Querying/Processor
.Team/QueryProcessor
:hammer_and_wrench:
Type:Bug
Product defects
I'm using Metabase 0.42.0 with PostgreSQL and I noticed that one particular question built with the question builder took very long to complete compared to a native query.
The question is basically "count all rows where column = x", where column has the PostgreSQL type
date
. In the question builder, everything looks like Metabase correctly detects that this column is a date column. It allows me to specify the time in addition to the date, but this is optional. However, the Metabase logs showed the following:which already indicates that Metabase is using a localized timestamp instead of a naive date type. The slow query log revealed the following:
The slow query log unfortunately does not tell us which type is used for
'2021-01-01 00:00:00+01'
and'2021-01-22 00:00:00+01'
, but given the output from the Metabase log and the fact that the string includes timezone information, I would assume it'stimestamptz
. So based on this assumption, I try to reproduce what Metabase is doing by running a raw SQL query, like this:which takes about 56 seconds to complete, which is about the same time it took metabase to complete this query from the question builder. The explain plan shows that no indexes are being used. Next, I try to run a raw SQL query like the one above, but with
date
types instead oftimestamptz
types:This time, the query takes 140ms to complete, and the index is being used.
Proposal
Could you provide the possibility to treat columns of type
date
as simple, naive dates without any timestamp conversions in the background to make it easier for PostgreSQL (and perhaps other databases) to use indexes?Motivation
We have tables with more than 100 million rows, and the date column is usually that one column that allows us to run queries that don't take forever to complete. So, it would be a great improvement for us if Metabase generates SQL queries where indexes based on
date
columns are used whenever possible.The text was updated successfully, but these errors were encountered: