Reputation: 1057
I know that I add indexes on columns when I want to speed up searches on that column.
Here is an example model
class Blog(models.Model):
title = models.CharField(max_length=100)
added = models.DateTimeField(auto_now_add=True)
body = models.TextField()
I need to look up title
and added
columns and I should set db_index=True
on that columns.
class Blog(models.Model):
title = models.CharField(db_index=True, max_length=100)
added = models.DateTimeField(db_index=True, auto_now_add=True)
body = models.TextField()
But I search internet resource about more examples, I still can't understand or conclude how to use it. What time would/wouldn't be good for setting db_index=True?
Upvotes: 1
Views: 1019
Reputation: 47846
When to consider adding an index to a column?
In general, you need to consider many points before deciding to add an index to a column.
Oracle in its docs, has defined multiple guidelines on when to add an index to a column: http://docs.oracle.com/cd/B19306_01/server.102/b14211/data_acc.htm#i2769
WHERE
clauses.UPDATE
statements that modify indexed columns and INSERT
and DELETE
statements that modify indexed tables take longer than if there were no index. Such SQL statements must modify data in indexes as well as data in tables. They also generate additional undo and redo.WHERE
clauses with functions or operators. A WHERE
clause that uses a function, other than MIN
or MAX
, or an operator with an indexed key does not make available the access path that uses the index except with function-based indexes.INSERT
, UPDATE
, and DELETE
statements access the parent and child tables. Such an index allows UPDATEs
and DELETEs
on the parent table without share locking the child table.INSERTs
, UPDATEs
, and DELETEs
and the use of the space required to store the index. Remember when you add additional indexes, Read operations get faster but Write operations becomes slower because of recalculation of the indexes. So use them as per your use case demands.
Upvotes: 4
Reputation: 4250
The penalty for using indexes is slower write performance -- given you're unlikely to be posting a new blog post every 0.0001s you should feel free to add indexes for anything you're searching on.
Upvotes: 2