官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 洛隆县| 浪卡子县| 理塘县| 探索| 新竹市| 夹江县| 柘城县| 沙田区| 都匀市| 江油市| 仙居县| 鄂温| 锡林郭勒盟| 扬中市| 宕昌县| 邛崃市| 曲阳县| 咸丰县| 温泉县| 新巴尔虎左旗| 永顺县| 巴里| 渝北区| 临泉县| 常德市| 迭部县| 乾安县| 建宁县| 汕头市| 孟津县| 黄冈市| 恩施市| 中山市| 文昌市| 东平县| 斗六市| 沙湾县| 阿巴嘎旗| 游戏| 卓资县| 鸡泽县|