-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_queries.txt
137 lines (92 loc) · 5.57 KB
/
sample_queries.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
>>> result = Booking.objects.filter(booked_on__description__icontains='ouderschap').values('booked_by__name', 'year_week__year').annotate(Sum('hours'))
>>> for item in result:
... print("%s,%s,%s" % (item['booked_by__name'], item['year_week__year'], int(item['hours__sum'])))
>>> from faker import Factory
>>> fake = Factory.create()
>>> fake.name()
'Paul Torres'
>>> from trs.models import Person
>>> for person in Person.objects.exclude(name__icontains='reinout'):
... person.name = fake.name()
... person.save()
...
Hours per WBSO project
>>> from django.db.models import Sum
>>> result = Booking.objects.filter(year_week__year=2016, booked_on__wbso_project__isnull=False, booked_on__wbso_project__end_date__gte='2016-01-01').order_by('booked_on__wbso_project__number').values('booked_on__wbso_project__number', 'booked_on__wbso_project__title', 'booked_on__code', 'booked_on__description').annotate(Sum('hours'))
>>> for line in result:
... print(';'.join([str(line['booked_on__wbso_project__number']), line['booked_on__wbso_project__title'], line['booked_on__code'], line['booked_on__description'], str(line['hours__sum'])]))
Idem with a line per person:
>>> result = Booking.objects.filter(year_week__year=2016, booked_on__wbso_project__isnull=False, booked_on__wbso_project__end_date__gte='2016-01-01').order_by('booked_on__wbso_project__number').values('booked_on__wbso_project__number', 'booked_on__wbso_project__title', 'booked_on__code', 'booked_on__description', 'booked_by__name').annotate(Sum('hours'))
>>> for line in result:
... print(';'.join([str(line['booked_on__wbso_project__number']), line['booked_on__wbso_project__title'], line['booked_on__code'], line['booked_on__description'], line['booked_by__name'], str(line['hours__sum'])]))
Projects for a certain group, added after a certain date:
>>> for p in Project.objects.filter(group=3).filter(added__gte=datetime.datetime(2017, 9, 1)):
... print('%10s https://trs.lizard.net%s %9s %s' % (p.code, p.get_absolute_url(), p.contract_amount, p.description))
-----------------------------
Voor presentatie over django queries. Eerst 'sqllog' in `settings.py` aanzetten.
>>> from trs.models import Person, Project, Booking
>>> Person.objects.all()[0]
<Person: Andrew Mcguire>
>>> Person.objects.all()[0].name
'Andrew Mcguire'
>>> Person.objects.all()[0].group
<Group: Bedrijfsvoering/Algemeen>
>>> Person.objects.filter(name='reinout')
[]
>>> Person.objects.filter(name__icontains='reinout')
[<Person: Reinout van Rees>]
>>> Person.objects.filter(name__icontains='reinout')[0].group
<Group: Beheer en onderhoud>
>>> Person.objects.filter(group__name='Systemen')
>>> Person.objects.filter(group__name__icontains='beheer')
>>> Person.objects.filter(group=None)
>>> Person.objects.filter(group=None).count()
>>> Person.objects.exclude(group=None).count()
>>> Person.objects.exclude(group__isnull=True).count()
>>> Person.objects.filter(group__name='Systemen').filter(name__lte='C')
>>> Person.objects.exclude(group=None).filter(name__lte='C')
>>> groupless_persons = Person.objects.exclude(group=None) # No sql log
>>> groupless_persons.count() # Now we see output
query speed
>>> Person.objects.filter(group__name='Systemen') # 1 query
>>> for person in Person.objects.filter(group__name='Systemen'):
... print(person.name, person.group.name)
# ^^^^ veel queries
>>> for person in Person.objects.filter(group__name='Systemen').select_related('group'):
... print(person.name, person.group.name)
# ^^^ inner join
>>> for person in Person.objects.filter(group__name='Systemen').prefetch_related('group'):
... print(person.name, person.group.name)
# ^^^ twee queries
values/objects
>>> relevant_persons = Person.objects.filter(group__name='Systemen').select_related('group')
("om het leesbaarder te maken")
>>> for person in relevant_persons:
... print(person.name, person.group.name)
En dan nu values:
>>> relevant_persons
>>> relevant_persons.values('name', 'group__name') # dict
>>> relevant_persons.values_list('name', 'group__name') # list of tuples
>>> relevant_persons.values_list('name', flat=True) # list & hele kleine query
Aggregatie/annotatie
>>> Booking.objects.filter(booked_by__name__icontains='reinout')
>>> Booking.objects.filter(booked_by__name__icontains='reinout').count()
>>> from django.db import models
>>> Booking.objects.filter(booked_by__name__icontains='reinout').aggregate(models.Sum('hours'))
>>> relevant_persons = Person.objects.filter(group__name='Systemen').select_related('group')
>>> relevant_persons.aggregate(models.Sum('bookings__hours'))
>>> relevant_persons.annotate(models.Sum('bookings__hours')) # je ziet eigenlijk niets
>>> relevant_persons.annotate(models.Sum('bookings__hours'))[0].bookings__hours__sum # Nu wel
>>> for person in relevant_persons.annotate(models.Sum('bookings__hours')):
... print(person.name, person.bookings__hours__sum)
group by
>>> Booking.objects.filter(booked_on__description__icontains='ouderschap')
>>> Booking.objects.filter(booked_on__description__icontains='ouderschap').count()
>>> Booking.objects.filter(booked_on__description__icontains='ouderschap').values(
'booked_by__name', 'year_week__year')
>>> Booking.objects.filter(booked_on__description__icontains='ouderschap').values(
'booked_by__name', 'year_week__year').annotate(models.Sum('hours'))
>>> result = Booking.objects.filter(booked_on__description__icontains='ouderschap').values(
'booked_by__name', 'year_week__year').annotate(models.Sum('hours'))
>>> for item in result:
... print("%s,%s,%s" % (item['booked_by__name'], item['year_week__year'], int(item['hours__sum'])))