İçereği Atla
Odoo Menü
  • Portal
  • Ücretsiz deneyin
  • Uygulamalar
    Finans
    • Muhasebe
    • Faturalama
    • Masraf Yönetimi
    • Elektronik Çizelge (BI)
    • Belgeler
    • İmza
    Satış
    • Müşteri İlişkileri Yönetimi (CRM)
    • Satış
    • Satış Noktası Mağaza
    • Satış Noktası Restoran
    • Abonelikler
    • Kiralama
    Web Sitesi
    • Web Sitesi Oluşturucu
    • eTicaret
    • Blog
    • Forum
    • Canlı Sohbet
    • eÖğrenme
    Tedarik Zinciri
    • Envanter
    • Üretim
    • Ürün Yaşam Döngüsü Yönetimi
    • Satın Alma
    • Bakım
    • Kalite
    İnsan Kaynakları
    • Çalışanlar
    • İşe Alım
    • İzin
    • Değerlendirme
    • Referans
    • Filo Yönetimi
    Pazarlama
    • Sosyal Medyada Pazarlama
    • E-posta ile Pazarlama
    • SMS ile Pazarlama
    • Etkinlikler
    • Pazarlama Otomasyonu
    • Anket
    Hizmetler
    • Proje Yönetimi
    • Çalışma Çizelgeleri
    • Saha Hizmeti
    • Yardım Masası
    • Planlama
    • Randevular
    Verimlilik
    • Sohbet
    • Onay
    • Nesnelerin İnterneti
    • VoIP
    • Bilgi Bankası
    • WhatsApp
    Üçüncü taraf uygulamalar Odoo Stüdyo Odoo Bulut Platformu
  • Sektörler
    Perakende satış
    • Kitapçı
    • Giyim Mağazası
    • Mobilya Mağazası
    • Gıda Marketi
    • Hırdavat Dükkanı
    • Oyuncak Dükkanı
    Gıda ve Konaklama
    • Bar ve Pub
    • Restoran
    • Fast Food Restoranı
    • Konuk Evi
    • İçecek Distribütörü
    • Otel
    Gayrimenkul
    • Emlak Acentesi
    • Mimarlık Firması
    • İnşaat
    • Emlak Yönetimi
    • Bahçe Tasarımı
    • Mülk Sahipleri Derneği
    Uzmanlık
    • Muhasebe Firması
    • Odoo Partner
    • Pazarlama Ajansı
    • Hukuk Firması
    • Yetenek Kazanımı
    • Denetim ve Belgelendirme
    Üretim
    • Tekstil
    • Metal
    • Mobilyalar
    • Gıda
    • Bira fabrikası
    • Kurumsal Hediye
    Sağlık ve Spor
    • Spor Kulübü
    • Optik Mağazası
    • Fitness Merkezi
    • Sağlıklı Yaşam Merkezi
    • Eczane
    • Kuaför Salonu
    Ticaret
    • Tamirci
    • BT Donanım & Destek
    • Güneş Enerjisi Sistemleri
    • Ayakkabı İmalatçısı
    • Temizlik Hizmetleri
    • HVAC Hizmetleri
    Diğerleri
    • Kar Amacı Gütmeyen Kuruluş
    • Çevre Ajansı
    • Reklam Panosu Kiralama
    • Fotoğrafçılık
    • Bisiklet Kiralama
    • Yazılım Bayisi
    Tüm Sektörlere Göz Atın
  • Topluluk
    Öğrenim
    • Eğitim Araçları
    • Dokümantasyon
    • Sertifikasyonlar
    • Eğitim Etkinlikleri
    • Blog
    • Podcast
    Eğitim ve Gelişim
    • Eğitim Programı
    • Scale Up! İşletme Oyunu
    • Odoo'yu Ziyaret Edin
    Yazılım
    • İndirin
    • Sürümleri Kıyaslayın
    • Sürümler
    İş Birliği
    • Github
    • Forum
    • Etkinlikler
    • Çeviriler
    • Partner Olun
    • Partnerler için Hizmetler
    • Muhasebe Firmanızı Kaydettirin
    Hizmetler
    • Partner Bulun
    • Muhasebeci Bulun
    • Bir danışmanla görüşün
    • Kurulum Hizmetleri
    • Müşteri Referansları
    • Destek
    • Sürüm Yükseltme
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Demo randevusu alın
  • Fiyatlandırma
  • Yardım

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

  • Müşteri İlişkileri Yönetimi
  • e-Commerce
  • Muhasebe
  • Envanter
  • PoS
  • Proje Yönetimi
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiketler (View all)
odoo accounting v14 pos v15
About this forum
Yardım

SQL vs. ORM ongoing amount

Abone Ol

Get notified when there's activity on this post

Bu soru işaretlendi
ormsql
1 Cevapla
5772 Görünümler
Avatar
Hermel

Dear community,

I have come up with a ORM method to compute the ongoing sale + credit amount of partners grouped by same register code (aka siren character). However the method is awfully slow and causes performance issues. 

In order to fix the method I have tried translating the ORM method into a SQL query (for greater perfomance). Testing shows that the SQL query performs even worse than the ORM method.

I expect this shouldn't be the case..so... what am I doing wrong ?

class ResPartner(models.Model):
    _inherit = 'res.partner'

    ongoing_amount = fields.Float(
        string='Encours HT',
        help='Le montant total encours hors taxes',
        compute='_get_ongoing_amount',
        store=False,
    )

ORM Method:

    @api.one
    @api.depends('siren')
    def _get_ongoing_amount(self):
        lines_invoice = self.env['account.invoice.line'].search([
            ('invoice_id.state','in',['open']),
            ('partner_id.siren','=',self.siren),
            ('partner_id.siren','!=',False),
            ])

        lines_order = self.env['sale.order.line'].search([
            ('order_partner_id.siren','=',self.siren),
            ('order_partner_id.siren','!=',False),
            ('qty_invoiced','=','0'),
            ('state','in',['sale']),
            ('cash','=',False)
            ])
        self.ongoing_amount = sum(line.price_subtotal for line in lines_invoice) \
        + sum(line.price_subtotal for line in lines_order)

SQL method :

    @api.one
    @api.depends('siren')
    def _get_ongoing_amount(self):
        # make sure param doesn't get to false
        param = self.siren
        if not param:
            param = '999999999'
        sum_lines_invoice = self._cr.execute("""
            SELECT SUM(price_subtotal)
            FROM account_invoice_line
            FULL JOIN account_invoice so ON (state=so.state)
            FULL JOIN res_partner res ON (siren=res.siren)
            WHERE siren = %s
            AND state IN ('open')""", (param,))
        sum_lines_invoice = self._cr.fetchone()[0] or 0.0

        sum_lines_order = self._cr.execute("""
            SELECT SUM(price_subtotal)
            FROM sale_order_line
            WHERE siren = %s
            AND qty_invoiced = '0'
            AND state IN ('sale')
            AND cash IS FALSE""", (param,))
        sum_lines_order = self._cr.fetchone()[0] or 0.0
        
        self.ongoing_amount = sum_lines_invoice + sum_lines_order
0
Avatar
Vazgeç
Avatar
faOtools
En İyi Yanıt

Just a few notes regarding:

  1. Replace your @api.one with @api.multi. Beside @api.one is a deprecated decorator, it is also slower in case of multi records. In particular, if you show the field ongoing_amount in kanban or list view, your method would be launched as many times as many records are shown (usually 80) and all checks would be 80 times more frequent (e.g. the check for non-existing param). If you are sure you require only a single record, put self.ensure_one() at the beginning of your @api.multi method.

  2. When you avoid using ORM, you loose some critical features including security rights. For example, in your case a simple user would see sum by all records by all companies, even though he/she doesn't have right for those. So, just take that in account.

  3. I would also offer to keep 'siren' field right in account.invoice.line as a stored computed field depending on a partner siren (perhaps, also the invoice line state). Although it contradicts sql tables normalizing, it would let you avoid joining the tables which, I guess, is quite resource-consuming. Besides, in my opinion it is better to firstly search by siren and than by state, since the latter records should be represented by a bigger number.

As for the point that SQL is slower than ORM. I can't see here the real reason for that (although 'full join' leads to a few doubts). Perhaps, it is because of a used decorator, or since ORM implied SQL queries are optimized in comparison to your statements. Just as a hint: have a look at _search, search and search_count methods right in the Odoo core. Perhaps, it would lead you to some insights. Besides, if you consider migration to odoo 11 or 12: their cashing is optimized.

0
Avatar
Vazgeç
Enjoying the discussion? Don't just read, join in!

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

Üye Ol
İlgili Gönderiler Cevaplar Görünümler Aktivite
Usage Of SQl Queries In odoo
sql
Avatar
Avatar
1
Kas 22
7372
sql constraints aren't being detected in my model
sql
Avatar
Avatar
1
Eki 21
4255
update one model from other model in odoo 12 Çözüldü
orm sql clone rowid
Avatar
Avatar
1
Ara 19
5368
ORM Relationship in Odoo 12
orm
Avatar
0
Eyl 19
98
Odoo on microsoft sql server instead postgres sql?
sql
Avatar
2
Kas 18
10975
Topluluk
  • Eğitim Araçları
  • Dokümantasyon
  • Forum
Açık Kaynak
  • İndirin
  • Github
  • Runbot
  • Çeviriler
Hizmetler
  • Odoo.sh Hosting
  • Destek
  • Sürüm Yükseltme
  • Özel Geliştirmeler
  • Eğitim
  • Muhasebeci Bulun
  • Partner Bulun
  • Partner Olun
Hakkında
  • Şirketimiz
  • Pazarlama Gereçleri
  • İletişim
  • Kariyer
  • Etkinlikler
  • Podcast
  • Blog
  • Müşteriler
  • Hukuki • Gizlilik
  • Güvenlik
الْعَرَبيّة 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, müşteri ilişkileri yönetimi, eTicaret, muhasebe, envanter, satış noktası, proje yönetimi gibi şirketinizin tüm ihtiyaçlarını karşılayan bir açık kaynak işletme uygulamaları paketidir.

Odoo’nun eşsiz değer önermesi, aynı anda hem kullanımının çok kolay olup hem de tamamen entegre olmasıdır.

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