Last active
August 29, 2015 14:03
-
-
Save saintc0d3r/e75e5b13e83cdb6d8b0d to your computer and use it in GitHub Desktop.
Exercise 1 - Which Query that hit the indexes ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say we have a store database where the database has products collection with several documents in it | |
use store | |
db.products.insert({'sku': '119-4567-7895', 'price':359.99, 'description': 'Intel Core i7-4770K Haswell Quad-Core 3.5GHz LGA 1150 84W Desktop Processor Intel HD Graphics BX80646I74770K', 'category':'CPU-Processors-Desktops', 'brand': 'Intel', 'reviews': [{'author': 'Mr.T', 'pros':"I play video game and I do video editiing. When it's time to compress movie and work on it, it's really fast.", 'cons': "The temperature is on the high side, but I think it's the way they run."}]}) | |
db.products.insert({'sku': '121-6478-7234', 'price':374.99, 'description': 'ASUS GTX770-DC2OC-2GD5 GeForce GTX 770 2GB 256-Bit GDDR5 PCI Express 3.0 HDCP Ready SLI Support Video Card', 'category': 'Desktop Graphics Cards','brand': 'ASUS', 'reviews': [{'author': 'Slyder', 'pros':"Plug and play super fast works great so far nothing wrong with the way it's been working", 'cons':"Nothing yet"}]}) | |
db.products.insert({'sku': '121-6478-7234', 'price':529.99, 'description': 'SAMSUNG 840 EVO MZ-7TE1T0BW 2.5" 1TB SATA III MLC Internal Solid State Drive (SSD)', 'category': 'Internal SSD','brand': 'Samsung', 'reviews': [{'author': 'Joana', 'pros':"Super fast drive for load times, no noise, less power use for laptops.", 'cons':" Expensive compared to standard hard drives."}]}) | |
// Put index on sku field | |
db.products.ensureIndex({'sku':1}) | |
// Put index on price field | |
db.products.ensureIndex({'price':1}) | |
// Put index on description field | |
db.products.ensureIndex({'description':1}) | |
// Put a compound index on both category & brand fields | |
db.products.ensureIndex({'category':1, 'brand':1}) | |
// Put index on reviews.author | |
db.products.ensureIndex({'reviews.author':1}) | |
// If we find product by specific brand, does it utilise any index ? | |
db.products.find({'brand': 'ASUS'}).explain() | |
// What if we sort the prior query by price, ascending ? | |
db.products.find({'brand': 'ASUS'}).sort({'price':1}).explain() | |
// different criteria but still use sorting | |
db.products.find({$and: [{'price':{$gt:300.0}}, {'price': {$lt: 400.0}}]}).sort({'price':1}).explain() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment