Estou executando uma consulta como
select id from students where school_id='67153fb1-8f79-441d-a747-ca3778cf6d3d';
na mesa que parece
Table "public.students"
Column | Type | Modifiers
-------------------+-----------------------------+------------------------------------
id | uuid | not null default gen_random_uuid()
school_id | uuid |
Indexes:
"students_pkey" PRIMARY KEY, btree (id)
"students_school_id_idx" btree (school_id)
O plano de consulta para a instrução select com exatamente onde se parece abaixo-
explain select id from students where school_id='67153fb1-8f79-441d-a747-ca3778cf6d3d';
QUERY PLAN
--------------------------------------------------------------------------------------------------
Bitmap Heap Scan on students (cost=581.83..83357.10 rows=24954 width=16)
Recheck Cond: (school_id = '67153fb1-8f79-441d-a747-ca3778cf6d3d'::uuid)
-> Bitmap Index Scan on students_school_id_idx (cost=0.00..575.59 rows=24954 width=0)
Index Cond: (school_id = '67153fb1-8f79-441d-a747-ca3778cf6d3d'::uuid)
Isso é bastante rápido.
Agora adicionamos order by à query com id que degrada a query.(Tal query é gerada pelo Rails como student.first com alguma condição)
explain select id from students where school_id='67153fb1-8f79-441d-a747-ca3778cf6d3d' order by id asc limit 1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Limit (cost=0.43..488.51 rows=1 width=16)
-> Index Scan using students_pkey on students (cost=0.43..12179370.22 rows=24954 width=16)
Filter: (school_id = '67153fb1-8f79-441d-a747-ca3778cf6d3d'::uuid)
Como posso melhorar a velocidade para retornar os resultados desta consulta? Atualmente existem cerca de 4990731 registros na tabela e está demorando mais de 2 minutos! Sua execução no RDS com instância db.t2.medium.
ATUALIZAÇÃO
Depois de executarAnalyze students;
explain select id from students where school_id='67153fb1-8f79-441d-a747-ca3778cf6d3d' order by id asc limit 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Limit (cost=8.46..8.46 rows=1 width=16)
-> Sort (cost=8.46..8.46 rows=1 width=16)
Sort Key: id
-> Index Scan using students_school_id_idx on students (cost=0.43..8.45 rows=1 width=16)
Index Cond: (school_id = '67153fb1-8f79-441d-a747-ca3778cf6d3d'::uuid)
explain analyze select id from students where school_id='67153fb1-8f79-441d-a747-ca3778cf6d3d' order by id asc limit 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=8.46..8.46 rows=1 width=16) (actual time=1.853..1.855 rows=1 loops=1)
-> Sort (cost=8.46..8.46 rows=1 width=16) (actual time=1.851..1.852 rows=1 loops=1)
Sort Key: id
Sort Method: quicksort Memory: 25kB
-> Index Scan using students_school_id_idx on students (cost=0.43..8.45 rows=1 width=16) (actual time=1.841..1.843 rows=1 loops=1)
Index Cond: (school_id = '67153fb1-8f79-441d-a747-ca3778cf6d3d'::uuid)
Planning time: 0.145 ms
Execution time: 1.874 ms