- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 431字
- 2021-07-02 15:47:50
How it works...
In step 1 we remove any existing indexes. Next, in order to better simulate index creation delays, what we do is simply keep reimporting our sample dataset about 20 times. This should give us about 2 million records in our collection after the end of step 2. As I have the previous recipes' sample dataset, my document count may be slightly higher so don't worry about it.
Now, in order to test how foreground index creation hinders database operations, we need to be able to perform two tasks simultaneously. For this, we set up two terminal windows, preferably side by side, with mongo shells connected and ensure mydb is selected. In step 4, we create a index on three fields city, first_name, and last_name. Again, this is intentional to add a bit of computational overhead for our test database setup. Note that, unlike previous runs, this command will not yield immediately. So, switch to the next terminal windows and try inserting a simple record, as shown in step 5. If you have both window stacked side by side, you will notice that both of them yield almost simultaneously. If you look at mongod server logs, you can see that both operations, in this case, took roughly 10 seconds to complete. Also, as expected, our insert query did not complete until the index creation had released the lock on the collection.
In step 7, we delete the index again and in step 8 we recreate the index but this time with the option {background: 1}. This tells mongod to start the index creation process in the background. In step 9, we switch to the other terminal window and try inserting a random document to our collection. Lo and behold, our document gets inserted immediately. Now is a good time to switch to the mongod server logs. As shown in step 10, you can now see that the index creation is happening in small batches. When the index creation completes, you can see that mongod acquired about 20,354 locks for this process as opposed to 1, when creating index in foreground. This lock and release method allowed our insert query to go through. However, this does come with a slight trade-off. The index creation time in the background was about 130 seconds as compared to 10 seconds, when created in the foreground.
There you have it, a simple test to show the effectiveness of creating background indexes. As real-world production scenarios go, it is always safe to create indexes in the background unless you have a very strong reason otherwise.
- Spring Boot 2實戰(zhàn)之旅
- Visual Basic程序開發(fā)(學(xué)習(xí)筆記)
- React Native Cookbook
- C#程序設(shè)計(慕課版)
- Git高手之路
- Scratch 3游戲與人工智能編程完全自學(xué)教程
- Learning ArcGIS Pro
- Mastering macOS Programming
- Mastering JavaScript Design Patterns(Second Edition)
- Express Web Application Development
- 蘋果的產(chǎn)品設(shè)計之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗的七個原則
- Python算法詳解
- Hands-On Nuxt.js Web Development
- Visual Basic 6.0程序設(shè)計實驗教程
- Java并發(fā)編程之美