Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
4 Replies
3328 Tampilan

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
Buang
Penulis

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".

Jawaban Terbai

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
Buang
Penulis Jawaban Terbai

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
Buang
Penulis

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.

Post Terkait Replies Tampilan Aktivitas
1
Jul 25
563
Search a message Diselesaikan
1
Feb 25
1262
0
Sep 23
2226
2
Jun 23
4049
1
Agu 22
12790