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

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

Description

@tbazadaykin

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions