0

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

  • This looks relevant (for mysql, but there are some general comments in the answers): [Is there a REAL performance difference between INT and VARCHAR primary keys?](http://stackoverflow.com/questions/332300/is-there-a-real-performance-difference-between-int-and-varchar-primary-keys) – solarissmoke Jul 18 '16 at 06:10

1 Answers1

0

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895