Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composite index to position : drive_id + date instead of drive_id #3186

Merged
merged 1 commit into from
Jun 12, 2023

Conversation

IlyaDiallo
Copy link
Contributor

This is a proposed fix to the #2278 issue. On my setup with 5 million records in the position table, the close_drive function executes in under 7 seconds from 55 seconds before the index change.
An (untested) alternative fix would be to change the queries in the close_drive function like this:

    filtered_positions =
      from p in Position,
        where:
          p.drive_id == ^id and
            not is_nil(p.ideal_battery_range_km) and
            not is_nil(p.odometer)

    non_streamed_drive_data =
      from p in subquery(filtered_positions),
        select: %{
          start_ideal_range_km: first_value(p.ideal_battery_range_km) |> over(:w),
          end_ideal_range_km: last_value(p.ideal_battery_range_km) |> over(:w),
          start_rated_range_km: first_value(p.rated_battery_range_km) |> over(:w),
          end_rated_range_km: last_value(p.rated_battery_range_km) |> over(:w)
        },
        windows: [
          w: [
            order_by:
              fragment("? RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", p.date)
          ]
        ],
        limit: 1

The idea here is to pre-filter to work with a subset of the positions table (drive_id = id) instead of the whole table.

Copy link
Collaborator

@adriankumpf adriankumpf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks.

@adriankumpf adriankumpf merged commit 21cb083 into teslamate-org:master Jun 12, 2023
adriankumpf pushed a commit that referenced this pull request Jun 12, 2023
Co-authored-by: Ilya Diallo <idiallo@waplanner.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants