bsassoli / milan_culture_map

An interactive website mapping cultural venues in Milan, Italy.
https://milan-culture-map.vercel.app
MIT License
0 stars 1 forks source link

Models should have indexes #3

Closed thmsrmbld closed 3 years ago

thmsrmbld commented 3 years ago

A very quick and easy way to make your DB queries substantially faster in the database is to set indexes in your Model's meta class.

For example, from a personal project:

Screenshot 2021-04-08 at 11 57 38

You should set indexes on anything your data is commonly queried for. Indexes can also be pairs of fields, if you query on multiple parameters. This makes DB read operations much faster and DB write operations marginally slower (not noticeable unless you are writing multiple millions of rows). This app seems to be mostly a read application, so this might be an acceptable trade off?

Setting indexes requires a migration, as it's a change made to the database, not your application layer.

Docs: https://docs.djangoproject.com/en/3.1/ref/models/indexes/ Theory: https://en.wikipedia.org/wiki/Database_index

bsassoli commented 3 years ago

This is very very helpful.