Skip to content

Commit 554a460

Browse files
committedJun 13, 2017
Improved performance by adding indexes, demo final
1 parent c870962 commit 554a460

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed
 

‎src/08_perf/big_dealership/nosql/car.py

+7
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ class Car(mongoengine.Document):
2121
'db_alias': 'core',
2222
'collection': 'cars',
2323
'indexes': [
24+
'mileage',
25+
'year',
26+
'service_history.price',
27+
'service_history.customer_rating',
28+
'service_history.description',
29+
{'fields': ['service_history.price', 'service_history.description']},
30+
{'fields': ['service_history.price', 'service_history.customer_rating']},
2431
]
2532
}

‎src/08_perf/big_dealership/nosql/owner.py

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ class Owner(mongoengine.Document):
2121
'db_alias': 'core',
2222
'collection': 'owners',
2323
'indexes': [
24+
'name',
25+
'car_ids'
2426
]
2527
}

‎src/08_perf/big_dealership/q_and_a.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def timed(msg, func):
2727
)
2828

2929
timed(
30-
'Find the 10,000th owner?',
30+
'Find the 10,000th owner by name?',
3131
lambda: Owner.objects().order_by('name')[10000:10001][0]
3232
)
3333

@@ -47,6 +47,7 @@ def find_cars_by_owner(owner_id):
4747

4848

4949
def find_owners_by_car(car_id):
50+
print(car_id)
5051
owners = Owner.objects(car_ids=car_id)
5152
return list(owners)
5253

@@ -70,18 +71,19 @@ def find_owners_by_car(car_id):
7071

7172
timed(
7273
'Cars with expensive service and spark plugs?',
73-
lambda: Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs').count()
74+
lambda: Car.objects(service_history__price__gt=16800,
75+
service_history__description='Spark plugs').count()
7476
)
7577

7678
timed(
7779
'Load cars with expensive service and spark plugs?',
78-
lambda: list(Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs'))
80+
lambda: list(Car.objects(service_history__price__gt=15000)[:100])
7981
)
8082

8183
timed(
8284
'Load car name and ids with expensive service and spark plugs?',
83-
lambda: list(Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs')
84-
.only('make', 'model', 'id'))
85+
lambda: list(Car.objects(service_history__price__gt=15000)
86+
.only('make', 'model', 'id')[:100])
8587
)
8688

8789
timed(

0 commit comments

Comments
 (0)
Failed to load comments.