From 554a46071fbdb2305bce8c3865b1efdeeafce17d Mon Sep 17 00:00:00 2001 From: Michael Kennedy Date: Tue, 13 Jun 2017 16:10:29 -0700 Subject: [PATCH] Improved performance by adding indexes, demo final --- src/08_perf/big_dealership/nosql/car.py | 7 +++++++ src/08_perf/big_dealership/nosql/owner.py | 2 ++ src/08_perf/big_dealership/q_and_a.py | 12 +++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/08_perf/big_dealership/nosql/car.py b/src/08_perf/big_dealership/nosql/car.py index 0def06f..c4a92ce 100644 --- a/src/08_perf/big_dealership/nosql/car.py +++ b/src/08_perf/big_dealership/nosql/car.py @@ -21,5 +21,12 @@ class Car(mongoengine.Document): 'db_alias': 'core', 'collection': 'cars', 'indexes': [ + 'mileage', + 'year', + 'service_history.price', + 'service_history.customer_rating', + 'service_history.description', + {'fields': ['service_history.price', 'service_history.description']}, + {'fields': ['service_history.price', 'service_history.customer_rating']}, ] } diff --git a/src/08_perf/big_dealership/nosql/owner.py b/src/08_perf/big_dealership/nosql/owner.py index 08a8a44..4fc7b68 100644 --- a/src/08_perf/big_dealership/nosql/owner.py +++ b/src/08_perf/big_dealership/nosql/owner.py @@ -21,5 +21,7 @@ class Owner(mongoengine.Document): 'db_alias': 'core', 'collection': 'owners', 'indexes': [ + 'name', + 'car_ids' ] } diff --git a/src/08_perf/big_dealership/q_and_a.py b/src/08_perf/big_dealership/q_and_a.py index 23fcb8c..dc5c15a 100644 --- a/src/08_perf/big_dealership/q_and_a.py +++ b/src/08_perf/big_dealership/q_and_a.py @@ -27,7 +27,7 @@ timed( ) timed( - 'Find the 10,000th owner?', + 'Find the 10,000th owner by name?', lambda: Owner.objects().order_by('name')[10000:10001][0] ) @@ -47,6 +47,7 @@ timed( def find_owners_by_car(car_id): + print(car_id) owners = Owner.objects(car_ids=car_id) return list(owners) @@ -70,18 +71,19 @@ timed( timed( 'Cars with expensive service and spark plugs?', - lambda: Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs').count() + lambda: Car.objects(service_history__price__gt=16800, + service_history__description='Spark plugs').count() ) timed( 'Load cars with expensive service and spark plugs?', - lambda: list(Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs')) + lambda: list(Car.objects(service_history__price__gt=15000)[:100]) ) timed( 'Load car name and ids with expensive service and spark plugs?', - lambda: list(Car.objects(service_history__price__gt=16800, service_history__description='Spark plugs') - .only('make', 'model', 'id')) + lambda: list(Car.objects(service_history__price__gt=15000) + .only('make', 'model', 'id')[:100]) ) timed(