Skip to content

Commit a3dff9e

Browse files
committed
Add index on pending_changes <> '{}'
This `WHERE` clause is used on each page to render the count of import issues in the navigation bar, therefore we should try and speed up this query as it will have an impact on every page render. Adding an index on this filter should ensure that selecting the number of patients or vaccination records with import issues is quicker. Jira-Issue: MAV-2001
1 parent 97cf18c commit a3dff9e

File tree

8 files changed

+81
-55
lines changed

8 files changed

+81
-55
lines changed

app/models/patient.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

app/models/vaccination_record.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class AddIndexOnPendingChangesNotEmpty < ActiveRecord::Migration[8.0]
4+
disable_ddl_transaction!
5+
6+
def change
7+
add_index :patients,
8+
:id,
9+
where: "pending_changes <> '{}'",
10+
algorithm: :concurrently,
11+
name: "index_patients_on_pending_changes_not_empty"
12+
add_index :vaccination_records,
13+
:id,
14+
where: "pending_changes <> '{}'",
15+
algorithm: :concurrently,
16+
name: "index_vaccination_records_on_pending_changes_not_empty"
17+
end
18+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_09_12_134432) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_09_16_074716) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -715,6 +715,7 @@
715715
t.index ["given_name", "family_name"], name: "index_patients_on_names_given_first"
716716
t.index ["given_name"], name: "index_patients_on_given_name_trigram", opclass: :gin_trgm_ops, using: :gin
717717
t.index ["gp_practice_id"], name: "index_patients_on_gp_practice_id"
718+
t.index ["id"], name: "index_patients_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)"
718719
t.index ["nhs_number"], name: "index_patients_on_nhs_number", unique: true
719720
t.index ["school_id"], name: "index_patients_on_school_id"
720721
end
@@ -1013,6 +1014,7 @@
10131014
t.integer "source", null: false
10141015
t.index ["batch_id"], name: "index_vaccination_records_on_batch_id"
10151016
t.index ["discarded_at"], name: "index_vaccination_records_on_discarded_at"
1017+
t.index ["id"], name: "index_vaccination_records_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)"
10161018
t.index ["location_id"], name: "index_vaccination_records_on_location_id"
10171019
t.index ["nhs_immunisations_api_id"], name: "index_vaccination_records_on_nhs_immunisations_api_id", unique: true
10181020
t.index ["patient_id"], name: "index_vaccination_records_on_patient_id"

spec/factories/patients.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

spec/factories/vaccination_records.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#

spec/models/patient_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

spec/models/vaccination_record_spec.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#

0 commit comments

Comments
 (0)