- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 220字
- 2021-07-02 15:47:49
There's more...
Compound indexes, if used smartly, can dramatically reduce your document seek times. For example, let's assume our application had a view that only required us to show a list of names in a city. A traditional approach would be to run a find query and get the list of documents and send them to the application's view. However, we know that other fields in the document are not needed for this view. Then, by having a compound index on city and first_name with the addition of field projection, we simply send the index values down to the application that is:
db.mockdata.find({city:'Boston', first_name:'Sara'}, {city:1, first_name:1, _id:0})
By doing this, not only do we leverage the speed of the index but we negate the need to fetch the non-indexed keys. Another term used for this is a covered query and it can improve our applications significantly!
Also, compound indexes allow us to use the index for the leftmost key. In our example, if we were to run db.mockdata.find({city:'Boston'}), then the result would be fetched from the index. However, if we were to search on the first_name that is, db.mockdata.find({first_name:'Sara'}), the server would do a full collection scan and fetch the result. I would encourage you to run the preceding queries chained with the explain() function and see the details yourself.
- 程序員面試筆試寶典(第3版)
- ASP.NET動(dòng)態(tài)網(wǎng)頁設(shè)計(jì)教程(第三版)
- Java:Data Science Made Easy
- Full-Stack Vue.js 2 and Laravel 5
- 手把手教你學(xué)C語言
- 精通Python設(shè)計(jì)模式(第2版)
- Lighttpd源碼分析
- Mastering Linux Security and Hardening
- Test-Driven JavaScript Development
- 零基礎(chǔ)學(xué)C語言第2版
- Mastering Elixir
- Mastering VMware Horizon 7(Second Edition)
- 從“1”開始3D編程
- 安卓工程師教你玩轉(zhuǎn)Android
- Android 5從入門到精通