How to index a field in Odoo

It is a good practice to create an index for the most searchable fields in the model. In that way, the database search performance can be significantly increased. 

However, should be careful not to add indexes on too many fields because the size of the database can rapidly increase. This means should be detected the most searchable fields and to create indexes only for them. More general info about database indexes can be found here, while a more depth overview of the PostgreSQL indexes can be found in their official documentation.

In Odoo, an index can be added to a  particular field by setting up the index​ attribute to True​ in the field's definition. Other possible values​ for the index​ attribute are: btree​, btree_not_null​, trigram​ and None​ or False​ which means no indexed field and it is the default value. More details for each of these values can be found in Odoo's documentation.

An example of adding index to the field sequence​ in the PartnerContact​ model is shown below:

from odoo import models, fields

class PartnerContact(models.Model):
​_name = 'partner.contact'
_description = 'PartnerContact'

sequence = fields.Integer(index=True)