I have the following query in django:
end_date = timezone.now()
start_date = end_date + timedelta(-5*365)
queryset = (
DailyPriceHistory
.objects
.filter(symbol=symbol,datetime_utc__range=(start_date, end_date))
.order_by('datetime')
.order_by('creation_time')
.values_list('high','low','open','datetime','close','creation_time')
)
the Sql it generates is
SELECT `daily_price_history`.`high`,
`daily_price_history`.`low`,
`daily_price_history`.`open`,
`daily_price_history`.`datetime`,
`daily_price_history`.`close`,
`daily_price_history`.`creation_time`
FROM `daily_price_history`
WHERE (`daily_price_history`.`datetime_utc`
BETWEEN '2015-12-04 18:43:28.710229'
AND '2020-12-02 18:43:28.710229'
AND `daily_price_history`.`symbol` = 'A')
ORDER BY `daily_price_history`.`creation_time` ASC
Currently i have symbol
and datatime
as seperately indexed columns
I found why the filter sequence is not followed. i.e .filter(symbol=symbol,datetime_utc__range=(start_date, end_date))
I wanted
WHERE (`daily_price_history`.`symbol` = 'A'
AND `daily_price_history`.`datetime_utc`
BETWEEN '2015-12-04 18:43:28.710229'
AND '2020-12-02 18:43:28.710229')
but it uses
WHERE (`daily_price_history`.`datetime_utc`
BETWEEN '2015-12-04 18:43:28.710229'
AND '2020-12-02 18:43:28.710229'
AND `daily_price_history`.`symbol` = 'A')
Also i dont see order_by('datetime')
in the sql, is this because datetime
is indexed