defmodule Microwaveprop.Repo.Migrations.AddContactsInsertedAtDescIndex do
  use Ecto.Migration

  # Matches the default sort for `Radio.list_contacts/1` which is
  # `ORDER BY inserted_at DESC, id DESC`. Without this index Postgres does a
  # Seq Scan + top-N heapsort over every row on each `/contacts` page load
  # (measured ~77ms on 58k rows).
  def change do
    create index(:contacts, ["inserted_at DESC", "id DESC"],
             name: :contacts_inserted_at_desc_id_desc_idx
           )
  end
end
