Database Reference
In-Depth Information
Table 10-1. ( continued )
Element
Description
scanAndOrder
A true/false value indicating whether the documents need to read in order to be
sorted, rather than taking advantage of an index.
indexOnly
Indicates that no documents needed to be scanned in order to return the results for
the query; all the fields being queried and being returned are on one index.
nYields
The number of times this query yielded its read lock to allow writes to execute.
nChunkSkips
The number of documents skipped because of an active chunk migration.
server
The server on which this query was executed.
Using the Profiler and explain() to Optimize a Query
Now let's walk through a real-world optimization scenario and look at how we can use MongoDB's profiler and
explain() tools to fix a problem with a real application.
The example discussed in this chapter is based on a small sample blog application. This database has a function
to get the posts associated with a particular tag; in this case it's the even tag. Let's assume you have noticed that this
function runs slowly, so you want to determine whether there is a problem.
Let's begin by writing a little program to fill the aforementioned database with data so that we have something to
run queries against, to demonstrate the optimization process.
<?php
// Get a connection to the database
$mongo = new MongoClient();
$db=$mongo->blog;
// First let's get the first AuthorsID
// We are going to use this to fake a author
$author = $db->authors->findOne();
if(!$author){
die("There are no authors in the database");
}
for( $i = 1; $i < 10000; $i++){
$blogpost=array();
$blogpost['author'] = $author['_id'];
$blogpost['Title'] = "Completely fake blogpost number {$i}";
$blogpost['Message'] = "Some fake text to create a database of blog posts";
$blogpost['Tags'] = array();
if($i%2){
// Odd numbered blogs
$blogpost['Tags'] = array("blog", "post", "odd", "tag{$i}");
} else {
 
Search WWH ::




Custom Search