Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
3317 Vistas

Example:

I have a model with a many2one to 'res.partner'. I have a lot of records in that model, dozens of thousands of records, and a few thousands in res.partner. I also have the necessary indexes.

Why is this:

search([('customer_id.name', 'like', 'somename')])

way faster than this:

search([('customer_id', 'like', 'somename')])

?

Avatar
Descartar
Autor

Hi, thanks. I forgot to mention that I was using Odoo 8, but anyway the code is basically the same for that method.

I am not quite sure about why the double search is made. And I don't understand why the method is named "_search" and not "search".

But you gave me good hint.

Thank you.

The double search is to find the 10 people who work for Azure when you search for "Azure".

Mejor respuesta

Good Question!

The default search for res.partner adds the searching of parent_id:

https://github.com/odoo/odoo/blob/14.0/odoo/addons/base/models/res_partner.py#L739

Your first search will look for Azure Interior in the name.

Your second search will look for Azure Interior in both the name AND the parent_id name.


Your second search is doing TWO searches at once!


Avatar
Descartar
Autor Mejor respuesta

Yes, I see. The difference is in the active_test parameter.

But the delay is not doubled, it is much more. About 1 min with one method and 1 second with the other one.

Anyway, I found a way to fix this, I redifined search method on my model to replace searches with customer_id with customer_id.name.

Thanks.

Avatar
Descartar
Autor

Actually, I don't think method _name_search has something to do. I commented that method out and the delay is still there.

My previous solution cause some other problems.

So, I had to implement a new one.

1- Create a function field that returns the same value as customer_id. Let's say customer_id2

2- Add a search function for that field, fnct_search=_search_by_customer. This function will search by customer_id.name

3- Remove customer_id from Advanced Search.

Publicaciones relacionadas Respuestas Vistas Actividad
1
jul 25
562
1
feb 25
1262
0
sept 23
2224
2
jun 23
4047
1
ago 22
12788