Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional indices to StaticGroupAssociation #5732

Merged
merged 6 commits into from
May 20, 2024

Conversation

glennmatthews
Copy link
Contributor

@glennmatthews glennmatthews commented May 17, 2024

Closes #5472

What's Changed

Add indices to StaticGroupAssociation reflecting likely lookup/access patterns. While Django automatically adds indices for regular ForeignKeys, it does not do so for GenericForeignKeys, so we have to add our own. These are very similar to the indices we already have on the ObjectChange table.

Screenshots

Before:

nautobot=# \d extras_staticgroupassociation
                     Table "public.extras_staticgroupassociation"
          Column           |           Type           | Collation | Nullable | Default 
---------------------------+--------------------------+-----------+----------+---------
 id                        | uuid                     |           | not null | 
 created                   | timestamp with time zone |           |          | 
 last_updated              | timestamp with time zone |           |          | 
 _custom_field_data        | jsonb                    |           | not null | 
 associated_object_id      | uuid                     |           | not null | 
 associated_object_type_id | integer                  |           | not null | 
 static_group_id           | uuid                     |           | not null | 
Indexes:
    "extras_staticgroupassociation_pkey" PRIMARY KEY, btree (id)
    "extras_staticgroupassoci_static_group_id_associat_cb19851a_uniq" UNIQUE CONSTRAINT, btree (static_group_id, associated_object_type_id, associated_object_id)
    "extras_staticgroupassociat_associated_object_type_id_d3db04b2" btree (associated_object_type_id)
    "extras_staticgroupassociation_associated_object_id_81e97e16" btree (associated_object_id)
    "extras_staticgroupassociation_static_group_id_f85f6416" btree (static_group_id)
Foreign-key constraints:
    "extras_staticgroupas_associated_object_ty_d3db04b2_fk_django_co" FOREIGN KEY (associated_object_type_id) REFERENCES django_content_type(id) DEFERRABLE INITIALLY DEFERRED
    "extras_staticgroupas_static_group_id_f85f6416_fk_extras_st" FOREIGN KEY (static_group_id) REFERENCES extras_staticgroup(id) DEFERRABLE INITIALLY DEFERRED

After:

nautobot=# \d extras_staticgroupassociation
                     Table "public.extras_staticgroupassociation"
          Column           |           Type           | Collation | Nullable | Default 
---------------------------+--------------------------+-----------+----------+---------
 id                        | uuid                     |           | not null | 
 created                   | timestamp with time zone |           |          | 
 last_updated              | timestamp with time zone |           |          | 
 _custom_field_data        | jsonb                    |           | not null | 
 associated_object_id      | uuid                     |           | not null | 
 associated_object_type_id | integer                  |           | not null | 
 static_group_id           | uuid                     |           | not null | 
Indexes:
    "extras_staticgroupassociation_pkey" PRIMARY KEY, btree (id)
    "extras_sga_associated_object" btree (associated_object_type_id, associated_object_id)
    "extras_sga_double" btree (static_group_id, associated_object_id)
    "extras_staticgroupassoci_static_group_id_associat_cb19851a_uniq" UNIQUE CONSTRAINT, btree (static_group_id, associated_object_type_id, associated_object_id)
    "extras_staticgroupassociat_associated_object_type_id_d3db04b2" btree (associated_object_type_id)
    "extras_staticgroupassociation_associated_object_id_81e97e16" btree (associated_object_id)
    "extras_staticgroupassociation_static_group_id_f85f6416" btree (static_group_id)
Foreign-key constraints:
    "extras_staticgroupas_associated_object_ty_d3db04b2_fk_django_co" FOREIGN KEY (associated_object_type_id) REFERENCES django_content_type(id) DEFERRABLE INITIALLY DEFERRED
    "extras_staticgroupas_static_group_id_f85f6416_fk_extras_st" FOREIGN KEY (static_group_id) REFERENCES extras_staticgroup(id) DEFERRABLE INITIALLY DEFERRED

Also:

>>> print(Tag.objects.get(name="Amber").static_groups.query.explain(using="default"))
Sort  (cost=12.59..12.59 rows=1 width=184)
  Sort Key: extras_staticgroup.name
  ->  Hash Join  (cost=8.30..12.58 rows=1 width=184)
        Hash Cond: (extras_staticgroup.id = u0.static_group_id)
        ->  Seq Scan on extras_staticgroup  (cost=0.00..4.00 rows=100 width=184)
        ->  Hash  (cost=8.29..8.29 rows=1 width=16)
              ->  Index Scan using extras_sga_associated_object on extras_staticgroupassociation u0  (cost=0.27..8.29 rows=1 width=16)
                    Index Cond: ((associated_object_type_id = 85) AND (associated_object_id = '662d8948-08a6-4ba1-ace8-242f87f8ab22'::uuid))

TODO

  • Explanation of Change(s)
  • Added change log fragment(s) (for more information see the documentation)
  • Attached Screenshots, Payload Example
  • n/a Unit, Integration Tests
  • n/a Documentation Updates (when adding/changing features)
  • n/a Example App Updates (when adding/changing features)
  • Outline Remaining Work, Constraints from Design

@glennmatthews glennmatthews self-assigned this May 17, 2024
@glennmatthews glennmatthews merged commit 4343441 into next May 20, 2024
17 checks passed
@glennmatthews glennmatthews deleted the u/glennmatthews-staticgroupassociation-indices branch May 20, 2024 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants