What are the best practices for database searches?
Indexes
Indexes work in a similar way to the old card-file indexes that were once used in libraries, where cards would be stored in a box, sorted by title or name, allowing the librarian to find a card for a particular book. The card would contain the location in the library where the book could be found, so the librarian could walk directly to the right place, instead of having to search every book in the library by hand.
Most databases support indexes in some form, even if you don't explicitly create an index on your data. In MongoDB, for example, every document has a primary key, stored in the "_id" field, that is always indexed. So if you only ever look up your document by _id", then you won't need to create any other indexes. We can check it by running the following command:
One of the most important ways to make your searches more efficient is to have an index that supports the queries you are executing. If you don't have an appropriate index for the query being executed, then MongoDB must scan every document in the collection to find matching results. If you have an appropriate index, then MongoDB can jump directly to a subset of matching documents instead of searching all of them. Even if the index only supports part of your query, it can still be used to whittle down all of the documents so that only a small subset needs to be more slowly scanned.
Indexes come with a performance cost for database writes, so in this case, you need to balance the speed of searching for data with the speed of updating data.
Data modeling
In any database, how you organize your data is going to have a big impact on how quickly you can find the data that you're looking for. Data modeling is an advanced topic, but the golden rule in MongoDB is that data that is accessed together should be stored together. What this means is that if related data, such as the comments associated with a blog post, are always loaded with the blog content, then the comments and the blog post itself should be stored in a single document. If the comments are stored in separate documents, then the database needs to do extra work, effectively searching for many documents before returning the results.
You can find out more about how to structure your data to make queries more efficient in the MongoDB documentation on data modeling, and you can learn some useful, reusable MongoDB design patterns, many of which are designed to make your database searches more efficient.
Optimizing your database searches
It's important to understand the work that your database has to do to find the documents you're searching for, and you can find out what the database is doing by using MongoDB's explain feature. Providing a database query to explain will list the steps that the database executed when searching for the relevant data and will highlight when no index was used to look up the data and how many documents had to be scanned.
You should use explain as a routine part of building your database application and not just when you notice a particularly slow query. Optimizing your database searches will save you money as well as make your application more responsive because less hardware will be required to run all of your database searches.