Skip to content

Optimize Queries to Database #2730

Closed
Closed
@ankitpopli1891

Description

@ankitpopli1891

I was profiling one of my models to serialize the list of countries against DRF and found the following query was being made to the database.

'SELECT "const_country"."id", "const_country"."name", "const_country"."iso_code_2", "const_country"."iso_code_3", "const_country"."flag", "const_country"."tld", "const_country"."area", "const_country"."population", "const_country"."phone", "const_country"."postal_code_format", "const_country"."postal_code_regex" FROM "const_country" ORDER BY "const_country"."name" ASC, "const_country"."iso_code_3" ASC, "const_country"."iso_code_2" ASC'

This query runs in ~.04 seconds.

So, I added the fields to make sure only the fields I need are serialized. The serialization was OK, but the query that ran was exactly the same.

As per my limited knowledge, the following query should fire instead:

'SELECT "const_country"."id", "const_country"."name", "const_country"."iso_code_2" ORDER BY "const_country"."name" ASC, "const_country"."iso_code_3" ASC, "const_country"."iso_code_2" ASC'

This query runs in ~.004 seconds.

This is ~10 times faster.

Environment:

  • Python 3.4.0
  • SQLite version 3.8.2

Not sure If I missed something, or the issue actually exist. It would be great if someone can look into the issue.

Thanks
Ankit

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions