Skip to Content
Menu
This question has been flagged
2 Replies
8791 Views

EDIT --> Probably I should have print the return of model.filtered(function) before asking because I discovered that if you don't make a search() before filtered you just get the model object in return, not a recordset. So basically you have to search/browse first, no matter what. 


I still wonder if it's better to get subrecords with a second search or filtered function. 

I think we should absolutely avoid to use search() or filtered() in loops, but I see that search() seems to be more efficient for a little number of iterations, while filtered seems to be slightly better for a large number.. might that be a reason to prefer filtered over search? 

___________________________________________________________________________________________________

Hello everyone, I'm testing performance for my project and I'm trying to figure out cases when i should use search() instead of filtered(). 


I ask because of 2 reasons:  

1) I did some tests and I can see that generally self.env['model.name'].filtered()  is much faster than  self.env['model.name'].search([])


For example, 


variable = self.env['model.name'].search([('name', '=', 'value')])

is about 800 microseconds for me, EVEN IF i already istantiated model or recordset with another search and perform search() on the model or recordset (in the latter case is like 600 microseconds but still worse than filtered)


When


variable = self.env['model.name'].filtered(lambda r: r.name == name) 

is about 250 microseconds.

And if I try to iterate n times the instructions, the results are basically the same.


2) BUT i also see that in Odoo source code, models recordsets are firstly fetched by using search() or browse() , and only then the results are filtered.


Now, from what I understand, one problem of filtered() might be that if I need to fetch the full recordset of a model to be able loop through it, I probably need to use search (or maybe browse) because I don't think there is a way to get the full recordset out the filtered method.


But in other situations, where I just need to fetch a subrecordset or even a single record, why not directly use filtered if it's faster instead of make a search() and then filter it later? Can some one explain? I find very poor explanations about it. Thanks! 


Avatar
Discard
Author

Thanks Cybrosys, so basically with search() you're fetching from database while filtered() doesn't make a query. Interesting.

By the way, your tutorials are very helpful!

Best Answer

Hi.

As you said .filtered() is faster than .search()
.filtered() is used for an object of a record that can be specific.That is we are filtered out of a record from a record set.
But .search() is used for the whole model records. That is searching a record from a database table.
In general, you usually use a .search for doing database queries and getting specific results from the database. You'll use filtered() mostly when you want to filter out a part of a record set in the Python code without doing database operations anymore.

Regards

Avatar
Discard

I was also having same doubt but able to locate this question.
Awesome question and to-the-point answer by cybrosys.
Thank you Francesco Ballerini and Cybrosys.

Best Answer

Muchas gracias a todos, tenía esta misma duda y he podido aclararla, 

Saludos desde Cuba

Avatar
Discard
Related Posts Replies Views Activity
0
Feb 25
386
0
May 21
8508
2
Jun 17
3780
0
Apr 24
3357
3
Mar 15
6455