sql: add index on date_valeur, and use SUM() (#64745)
This commit is contained in:
parent
04543f7ff9
commit
734ef5bf06
|
@ -0,0 +1,17 @@
|
|||
# Generated by Django 2.2.28 on 2022-05-02 11:57
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('eo_banque', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddIndex(
|
||||
model_name='lignebanquepop',
|
||||
index=models.Index(fields=['date_valeur'], name='eo_banque_l_date_va_f8c455_idx'),
|
||||
),
|
||||
]
|
|
@ -33,13 +33,15 @@ def solde(t=None):
|
|||
if t == s.date:
|
||||
pass
|
||||
elif t < s.date:
|
||||
lignes = LigneBanquePop.objects.filter(date_valeur__gt=t, date_valeur__lte=s.date)
|
||||
for ligne in lignes:
|
||||
m -= ligne.montant
|
||||
montant = LigneBanquePop.objects.filter(date_valeur__gt=t, date_valeur__lte=s.date).aggregate(
|
||||
models.Sum('montant')
|
||||
)
|
||||
m -= montant['montant__sum']
|
||||
elif t > s.date:
|
||||
lignes = LigneBanquePop.objects.filter(date_valeur__gt=s.date, date_valeur__lte=t)
|
||||
for ligne in lignes:
|
||||
m += ligne.montant
|
||||
montant = LigneBanquePop.objects.filter(date_valeur__gt=s.date, date_valeur__lte=t).aggregate(
|
||||
models.Sum('montant')
|
||||
)
|
||||
m += montant['montant__sum']
|
||||
return m
|
||||
|
||||
|
||||
|
@ -71,6 +73,9 @@ class LigneBanquePop(models.Model):
|
|||
unique_together = (
|
||||
('compte', 'date_comptabilisation', 'date_operation', 'libelle', 'reference', 'montant'),
|
||||
)
|
||||
indexes = [
|
||||
models.Index(fields=['date_valeur']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return "%(date_valeur)s %(libelle)s %(montant)s" % self.__dict__
|
||||
|
|
Loading…
Reference in New Issue