Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Should I use filter() instead of search()

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
performance
2 Besvarelser
12408 Visninger
Avatar
Francesco Ballerini

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! 


0
Avatar
Kassér
Francesco Ballerini
Forfatter

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!

Avatar
Cybrosys Techno Solutions Pvt.Ltd
Bedste svar

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

1
Avatar
Kassér
Hariom Pandya

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.

Avatar
Indira
Bedste svar

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

Saludos desde Cuba

0
Avatar
Kassér
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
What is the role of Social User and Social Manager in Social Marketing Module. Løst
performance
Avatar
Avatar
2
nov. 25
598
I'm Looking for a real estate Web Design and Development Agency in Dubai
performance
Avatar
Avatar
Avatar
Avatar
Avatar
4
sep. 25
1939
work orders duration
performance
Avatar
0
jul. 25
1652
Slow Download Speed with Custom Odoo Module
performance
Avatar
0
feb. 25
2538
Odoo - Memcached - Postgresql
performance
Avatar
0
maj 21
10731
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now