📜  django db_index=true - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:52.476000             🧑  作者: Mango

代码示例1
When we should use db_index=True in Django?

This is not really django specific; more to do with databases. You add indexes on columns when you want to speed up searches on that column.

Typically, only the primary key is indexed by the database. This means look ups using the primary key are optimized.

If you do a lot of lookups on a secondary column, consider adding an index to that column to speed things up
I would say you should use db_index=True when you have a field that is unique for more useful lookups.

For example if you a table customers with records of many users they'll each have their own unique user_id. 
Each user_id would be unique and having to index this field to find that unique user is more often desirable than say their first_name or last_name.
Actually this is also ok but since they're not unique you will get probably recieve multiple results from your queries and this might not be as useful as using an referencing their id.