Optimizing the DB Query with Indexes and includes

LETs Suppose I’m going to RUN the Query
CREATE INDEX id ON Student(id) INCLUDE(name)
This is not supported in DJANGO (What I saw) so far. This is an inherent feature to certain DBs if I’m not wrong, But this thing in Certain situations becomes very helpful for DB Query Optimization as We do not need to hit the DISK (Heap) [Index only scan] everytime we are fetching name based on Id.

In fact its not only the name , We can do much more besides that. I’m putting it here to be included in the Core Django. Please comment (reply) for further queries.

Directly, no.

You can create the index yourself using a custom migration.

Django supports covering indexes and constraints on Postgres since 3.2.

Simply use the include option when defining your index

class Student(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        indexes = [
            Index(
                fields=["id"], include=["name"], name="id_cov_name"
            ),
        ]
5 Likes