Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

VersionManager.as_of(): avoid using OR in query #150

Open
tbazadaykin opened this issue Mar 20, 2018 · 1 comment
Open

VersionManager.as_of(): avoid using OR in query #150

tbazadaykin opened this issue Mar 20, 2018 · 1 comment

Comments

@tbazadaykin
Copy link

When using as_of() it using OR operator into query to add current record into result.

>>>print MyMode.objects.as_of(timezone.now()).query
SELECT `mymodel`.`id`, `mymodel`.`identity`, `mymodel`.`version_start_date`, `mymodel`.`version_end_date`, `mymodel`.`version_birth_date` FROM `mymodel` WHERE ((`mymodel`.`version_end_date` > 2018-03-20 14:30:18 OR `mymodel`.`version_end_date` IS NULL) AND `mymodel`.`version_start_date` <= 2018-03-20 14:30:18)

If set version_end_date default value to datetime.max (or other hard-to-reach value) it is possible to avoid using OR. This will lead to a decrease of query cost.

SELECT `mymodel`.`id`, `mymodel`.`identity`, `mymodel`.`version_start_date`, `mymodel`.`version_end_date`, `mymodel`.`version_birth_date` FROM `mymodel` WHERE (`mymodel`.`version_end_date` > 2018-03-20 14:30:18 AND `mymodel`.`version_start_date` <= 2018-03-20 14:30:18)
@maennel
Copy link
Contributor

maennel commented Mar 26, 2018

Hey @tbazadaykin, thanks for coming up with this. I see a bit of a trade-off in identifying current versions - but this may be less of a problem than it seems, if the column is properly indexed.
I will not have time to go deeper into this during the upcoming couple of weeks, but will try to get back to this point sometime later.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants