Skip to content

Instantly share code, notes, and snippets.

@jdreaver
Last active September 19, 2017 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdreaver/dfe7e7d97f0eacd47273626ca7bbc580 to your computer and use it in GitHub Desktop.
Save jdreaver/dfe7e7d97f0eacd47273626ca7bbc580 to your computer and use it in GitHub Desktop.
Slow INNER JOIN planning time
localhost [classroom_prod] # EXPLAIN ANALYZE SELECT * FROM knowledge_snapshots ks INNER JOIN answers a ON a.knowledge_snapshot_id = ks.id WHERE ks.student_id = 7538860;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=0.87..2899.01 rows=235 width=105) (actual time=0.095..5.197 rows=72 loops=1)
-> Index Scan using knowledge_snapshots_parent_student_id_created_at_idx on knowledge_snapshots ks (cost=0.43..95.05 rows=26 width=53) (actual time=0.047..0.086 rows=6 loops=1)
Index Cond: (student_id = 7538860)
-> Index Scan using answers_parent_knowledge_snapshot_id_idx on answers a (cost=0.44..106.56 rows=128 width=52) (actual time=0.838..0.845 rows=12 loops=6)
Index Cond: (knowledge_snapshot_id = ks.id)
Planning time: 15.655 ms
Execution time: 5.288 ms
(7 rows)
db-standby-1-prod [classroom_prod] # EXPLAIN ANALYZE SELECT * FROM knowledge_snapshots ks INNER JOIN answers a ON a.knowledge_snapshot_id = ks.id WHERE ks.student_id = 7538860;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=0.87..2899.01 rows=236 width=105) (actual time=0.080..0.301 rows=72 loops=1)
-> Index Scan using knowledge_snapshots_parent_student_id_created_at_idx on knowledge_snapshots ks (cost=0.43..95.05 rows=26 width=53) (actual time=0.028..0.063 rows=6 loops=1)
Index Cond: (student_id = 7538860)
-> Index Scan using answers_parent_knowledge_snapshot_id_idx on answers a (cost=0.44..106.56 rows=128 width=52) (actual time=0.022..0.028 rows=12 loops=6)
Index Cond: (knowledge_snapshot_id = ks.id)
Planning time: 169.347 ms
Execution time: 0.404 ms
(7 rows)
db-standby-1-prod [classroom_prod] # \d+ answers
Table "public.answers"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------------+--------------------------+------------------------------------------------------+----------+--------------+-------------
math_question_id | character varying(12) | not null | extended | |
student_id | integer | not null | plain | |
created_at | timestamp with time zone | not null default now() | plain | |
id | bigint | not null default nextval('answers_id_seq'::regclass) | plain | |
duration_seconds | smallint | | plain | |
knowledge_snapshot_id | integer | not null | plain | |
correctness | real | not null | plain | |
skill_id | character varying(8) | not null | extended | |
Indexes:
"answers_parent_pkey" PRIMARY KEY, btree (id)
"answers_parent_knowledge_snapshot_id_idx" btree (knowledge_snapshot_id)
"answers_parent_student_id_created_at_idx" btree (student_id, created_at DESC)
Foreign-key constraints:
"answers_knowledge_snapshot_id_fkey" FOREIGN KEY (knowledge_snapshot_id) REFERENCES knowledge_snapshots(id)
"answers_math_question_id_fkey" FOREIGN KEY (math_question_id) REFERENCES math_questions(id)
"answers_student_id_fkey" FOREIGN KEY (student_id) REFERENCES students(id)
db-standby-1-prod [classroom_prod] # \d+ knowledge_snapshots
Table "public.knowledge_snapshots"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------------+--------------------------+------------------------------------------------------------------+----------+--------------+-------------
id | bigint | not null default nextval('knowledge_snapshots_id_seq'::regclass) | plain | |
student_id | integer | not null | plain | |
current_standard | text | not null | extended | |
sub_standard_perc | real | not null | plain | |
sub_sub_standard_perc | real | | plain | |
created_at | timestamp with time zone | not null default now() | plain | |
domain_id | character varying(8) | not null | extended | |
standard_id | character varying(8) | not null | extended | |
Indexes:
"knowledge_snapshots_parent_pkey" PRIMARY KEY, btree (id)
"knowledge_snapshots_parent_student_id_created_at_idx" btree (student_id, created_at DESC)
Foreign-key constraints:
"knowledge_snapshots_domain_id_fkey" FOREIGN KEY (domain_id) REFERENCES domains(id)
"knowledge_snapshots_standard_id_fkey" FOREIGN KEY (standard_id) REFERENCES standards(id)
Referenced by:
TABLE "answers" CONSTRAINT "answers_knowledge_snapshot_id_fkey" FOREIGN KEY (knowledge_snapshot_id) REFERENCES knowledge_snapshots(id)
Triggers:
populate_knowledge_snapshots_standard_id_column BEFORE INSERT ON knowledge_snapshots FOR EACH ROW WHEN (new.standard_id IS NULL) EXECUTE PROCEDURE populate_knowledge_snapshots_standard_id_column()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment