添加索引(db_index = True) [英] Add Indexes (db_index=True)

查看:662
本文介绍了添加索引(db_index = True)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本有关Django编码风格的书,他们讨论的一件事是 db_index = True 。自从我开始使用Django以来,我就从未使用过此函数,因为我不确定它的作用。

I'm reading a book about coding style in Django and one thing they discuss is db_index=True. Ever since I started using Django, I've never used this function because I'm not really sure what it does.

所以我的问题是,何时考虑添加索引?

So my question is, when to consider adding indexes?

推荐答案

这并不是真正的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.

像大多数规模问题一样,请记住,这些问题仅在统计上有大量行(10,000个不大)时才适用。

Keep in mind, like most problems of scale, these only apply if you have a statistically large number of rows (10,000 is not large).

此外,每次执行插入操作时,都需要更新索引。因此,请注意添加索引的列。

Additionally, every time you do an insert, indexes need to be updated. So be careful on which column you add indexes.

和往常一样,您只能优化可衡量的内容-因此请使用 EXPLAIN 语句和您的数据库日志(尤其是所有慢查询日志),以找出索引在哪里有用。

As always, you can only optimize what you can measure - so use the EXPLAIN statement and your database logs (especially any slow query logs) to find out where indexes can be useful.

这篇关于添加索引(db_index = True)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆