If you have come to Django with a fairly good MySQL, PostgreSQL background this will be obvious. If your experience of databases is creating a Django model, this may be of use to you.

Here is an example model

class Blog(models.Model):
    title = models.CharField(max_length=100)
    added = models.DateTimeField(auto_now_add=True)
    body = models.TextField()

You might be thinking, what’s wrong?

Well now lets assume you have 2000 entries in your database and you decied you want to list your items in order they were added, or by title. Databases are great, but without indexes it needs to scan over 2000 rows to figure out the order.

Heres the better version of the same model

class Blog(models.Model):
    title = models.CharField(db_index=True, max_length=100)
    added = models.DateTimeField(db_index=True, auto_now_add=True)
    body = models.TextField()

So, whats better?

Well, now there are indexes, your database server just needs to take a look over the list of indexes. Its a lot faster.

When to use indexes

A good rule, if you are going to order by, or filter by, it should be indexed.