Search code examples
djangodatabasequery-optimizationdjango-querysetdatabase-performance

Querying an object with pk vs custom object_id


I have a model as shown below,

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    middle_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30)
    person_id = models.CharField(max_length=32, blank=True)

where the person_id is populated on save, which is a random hex string generated by uuid, which would look something like 'E4DC6C20BECA49E6817DB2365924B1EF'

so my question is, in a database of a large magnitude of objects, does the queries

Person.objects.get(pk=10024)

(pk) vs (person_id)

Person.objects.get(person_id='E4DC6C20BECA49E6817DB2365924B1EF')

does any of the method has a performance advantage in a large scale of data?

I am not much aware of the database internals.

My database is postgresql


Solution

  • To get good performance from querying a column in a database, it needs to be indexed. The primary key column is indexed automatically (by definition), but your person_id one won't be; you should add db_index=True to the declaration, then make and run migrations.