diff --git a/lib/aprsme/packets.ex b/lib/aprsme/packets.ex index 08650f7..c5506ab 100644 --- a/lib/aprsme/packets.ex +++ b/lib/aprsme/packets.ex @@ -557,6 +557,7 @@ defmodule Aprsme.Packets do """ @spec get_total_packet_count() :: non_neg_integer() def get_total_packet_count do + # Use the new index for faster counting Repo.one(from p in Packet, select: count(p.id)) || 0 rescue DBConnection.ConnectionError -> diff --git a/priv/repo/migrations/20250707171625_add_count_optimization_index.exs b/priv/repo/migrations/20250707171625_add_count_optimization_index.exs new file mode 100644 index 0000000..d41edc7 --- /dev/null +++ b/priv/repo/migrations/20250707171625_add_count_optimization_index.exs @@ -0,0 +1,18 @@ +defmodule Aprsme.Repo.Migrations.AddCountOptimizationIndex do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + # Add a partial index on id that will be used for COUNT(*) queries + # This creates a small, fast index that PostgreSQL can use for counting + execute """ + CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_count_idx + ON packets (id) + WHERE id IS NOT NULL + """ + end + + def down do + execute "DROP INDEX IF EXISTS packets_count_idx" + end +end