Query Analyzing
Introduction into indexes
Indexes In Mongo
Managing indexes in MongoDB
Using index to sort query results.
When should I use indexes.
When should we avoid using indexes.
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
Mongo indexes
1. MongoDB
Indexes
●
Query Analyzing
●
Introduction into indexes
●
Indexes In Mongo
●
Managing indexes in MongoDB
●
Using index to sort query results.
●
When should I use indexes.
●
When should we avoid using indexes.
2. MongoDB Indexes
Creating experimental collection
●
Selecting Database
●
Inserting fake data in our test collection
“product”. 900000 documents.
●
Products are categorized into 10 categories and
the status of each product could be 0 or 1.
3. _id name category status
1236**** Prod 1 1 0
1237**** Prod 2 5 0
1238**** Prod 3 9 1
1238**** Prod 4 6 0
1239**** Prod 5 5 1
MongoDB Indexes
Query Analyzing
● db.product.find({category : 5});
●
Searching for products in a specific category.
●
Number of scanned documents.
●
Mongo scans all documents in the collection and
match them with the given condition.
4. MongoDB Indexes
Query Analyzing
●
If we tried to search for a single document
by _Id using “find” function, we would get
significantly different results.
●
In our case right now we are able to get
same performance just in case we search for
an “_id”.
●
What makes this happen is an index on “_id”
attribute.
5. MongoDB Indexes
Introduction into indexes
●
How does the normal query work? “linear
search”
●
Indexes can be described as the single and
most critical tool to increase the database
performance.
●
What is it?
An index is a data structure that contains a
copy of some data from database.
6. MongoDB Indexes
Indexes In Mongo
●
Index Types:
1) Default: _id
2) Single: Similar to Default but could be applied on any of
document fields.
3) Compound: Means defining an index on multiple fields.
Ie : employeeId, salary
4) Multikey: This type of index is used to index a field contains an
array.
We can just define one multikey index per document.
5) GeoSpatial: A set of indexes and query mechanisms to handle
geospatial information.
6) Text: This index is used with text supporting search for string
content.
7) Hashed: This index hashes the value of a field.
Doesn't support range-based queries. Doesn't support multi-key
arrays
Supports only equality matches.
Indexes in Mongo are tree data structured.
For More info search for “b-tree”.
●
Index Properties:
1) Unique.
2) TTL: These are special indexes used to
automatically remove documents after a certain
period of time.
7. MongoDB Indexes
Indexes In Mongo
Single Index:
It can be applied on any field of a collection.
Embedded Fields & Embedded Documents:
●
We can create indexes on fields within embedded documents.
●
We can create indexes on embedded documents.
8. MongoDB Indexes
Indexes In Mongo
Compound Index:
Where a single index structure references to more than one field “max of 31” in the same collection.
Let's go back to our example:
We have 900000 products.
Single document example.
Continue to the next slide.
9. MongoDB Indexes
Indexes In Mongo : Compound
Find query scenarios explanation: more about Explain results
Now Let's create a compound index on both category and status fields:
Find query scenarios explanation:
Continue to the next slide.
10. MongoDB Indexes
Indexes In Mongo
Find query scenarios explanation: more about Explain results
In the previous slide, it seemed like the index doesn't take effect when we filter documents using “status”
filed, Mongo scanned all 900000 documents. This is because indexing in Mongo is tree structured.
Let's try to explain it again:
Tests from previous slide:
11. MongoDB Indexes
Indexes In Mongo
Conclusions from compound indexes:
●
Sort indexes is supported by compound index as well as in single index.
●
Sort order can matter in determining whether the index can support a sort operation.
●
Compound index supports indexing by prefixes.
12. MongoDB Indexes
Indexes In Mongo
Multikey Index:
To index an array or subdocument MongoDB creates an index for each element in the array.
Creation to multikey index is similar to other types, MongoDB would automatically create multikey
index if any indexed field is in array.
Index Arrray with Embedded documents
Simple Array indexing:
13. MongoDB Indexes
GeoSpatial Index
GeoSpatial Index:
MongoDB offers a number of indexes that allows us to handle geospatial data. That geospatial data
is a geographical information that points to a specific location using longitude and latitude or x and
y in the Cartesian coordinates.
There are two surface types:
●
Flat (1): To calculate distances on a Euclidean plane. Use “2d” index. Supports data stored as
two-dimensional plane, legacy coordinate pairs [x, y].
●
Spherical (2): To calculate geometry over an Earth-like sphere. Use “2dsphere” index. Supports
data stored as GeoJSON object and as legacy coordinate pairs
(1)(2)
14. MongoDB Indexes
GeoSpatial Index
Simple example of 2d index:
Creating a geospatial data for restaurants
A sample of documents generated by
the previous code.
Creating index:
15. MongoDB Indexes
GeoSpatial Index
Simple example of 2dsphere index:
Creating a geospatial data for restaurants
A sample of documents generated by
the previous code.
Creating index:
Note: 2dshpere supports data stored as GeoJSON Objects.
For more info about GeoJSON : geojson.org , The following website is a tool to show how GeoJson is structured : geojson.io
for (var i = 0; i < 1000; i++) {
var x = (Math.floor(Math.random() * 20) % 2 == 1 ? "" : "-") + (i + Math.floor(Math.random() * 150)) % 99 + '.' + (i + Math.floor(Math.random() * 100000)) % 1000;
var y = (Math.floor(Math.random() * 20) % 2 == 1 ? "" : "-") + (i + Math.floor(Math.random() * 150)) % 99 + '.' + (i + Math.floor(Math.random() * 100000)) % 1000;
db.places.insert({
"loc": {
type: "Point",
coordinates: [+x, +y]
},
name: "Resturant Num. " + i,
status: i % 2
});
}
16. MongoDB Indexes
GeoSpatial Index
Geospatial query operators: more info
$geoWithin$near
$polygon $geoWithin
$geoIntersects
$near
$nearSphere
$geometry
$minDistance
$maxDistance
$center
$centerSphere
$box
$polygon
$uniqueDocs