Add GIST index based on ll_to_earth

This commit is contained in:
Adrian Kumpf
2019-10-08 18:15:25 +02:00
parent 08dcba1804
commit d5691e2cea
2 changed files with 37 additions and 4 deletions
+20 -4
View File
@@ -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