mirror of
https://github.com/teslamate-org/teslamate.git
synced 2026-01-24 21:06:08 +08:00
Add GIST index based on ll_to_earth
This commit is contained in:
@@ -6,12 +6,20 @@ defmodule TeslaMate.Locations.Functions do
|
||||
defmacro within_geofence?(position, geofence, :right) do
|
||||
quote do
|
||||
fragment(
|
||||
"earth_box(ll_to_earth(?, ?), ?) @> ll_to_earth(?, ?)",
|
||||
"""
|
||||
earth_box(ll_to_earth(?, ?), ?) @> ll_to_earth(?, ?) AND
|
||||
earth_distance(ll_to_earth(?, ?), ll_to_earth(?, ?)) < ?
|
||||
""",
|
||||
^unquote(geofence).latitude,
|
||||
^unquote(geofence).longitude,
|
||||
^unquote(geofence).radius,
|
||||
unquote(position).latitude,
|
||||
unquote(position).longitude
|
||||
unquote(position).longitude,
|
||||
^unquote(geofence).latitude,
|
||||
^unquote(geofence).longitude,
|
||||
unquote(position).latitude,
|
||||
unquote(position).longitude,
|
||||
^unquote(geofence).radius
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -19,12 +27,20 @@ defmodule TeslaMate.Locations.Functions do
|
||||
defmacro within_geofence?(position, geofence, :left) do
|
||||
quote do
|
||||
fragment(
|
||||
"earth_box(ll_to_earth(?, ?), ?) @> ll_to_earth(?, ?)",
|
||||
"""
|
||||
earth_box(ll_to_earth(?, ?), ?) @> ll_to_earth(?, ?) AND
|
||||
earth_distance(ll_to_earth(?, ?), ll_to_earth(?, ?)) < ?
|
||||
""",
|
||||
unquote(geofence).latitude,
|
||||
unquote(geofence).longitude,
|
||||
unquote(geofence).radius,
|
||||
^unquote(position).latitude,
|
||||
^unquote(position).longitude
|
||||
^unquote(position).longitude,
|
||||
unquote(geofence).latitude,
|
||||
unquote(geofence).longitude,
|
||||
^unquote(position).latitude,
|
||||
^unquote(position).longitude,
|
||||
unquote(geofence).radius
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
defmodule TeslaMate.Repo.Migrations.AddNewFkeyIndexes do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create(index(:drives, [:start_position_id]))
|
||||
create(index(:drives, [:end_position_id]))
|
||||
create(index(:drives, [:start_geofence_id]))
|
||||
create(index(:drives, [:end_geofence_id]))
|
||||
|
||||
drop(index(:positions, ["ll_to_earth(latitude, longitude)"]))
|
||||
drop(index(:addresses, ["ll_to_earth(latitude, longitude)"]))
|
||||
drop(index(:geofences, ["(earth_box(ll_to_earth(latitude, longitude), radius))"]))
|
||||
|
||||
create(index(:positions, ["ll_to_earth(latitude, longitude)"], using: "gist"))
|
||||
create(index(:addresses, ["ll_to_earth(latitude, longitude)"], using: "gist"))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user