Recent projects in the use of MongoDB, used to be very useful, but the user on the volume, obviously feel a little slow, why so slow, I began to slowly look for reasons.
Combined with online test scripts and some of my friends ' test results and the results of my own tests, I found that MongoDB is really not that slow, and that's where I wrote it wrong?
I started to find out where in the project code it was different from what was written in the test script, and the result was the way the database was connected, and the project started with the Pymongo recommended Mongoclient (Ip,port), but the test script used connection ( Ip,port), a number of different levels of testing have resulted in the following:
Method |
Number |
Time |
connection |
1 |
0.000314950942993 |
mongoclient |
1 |
0.000757217407227 |
connection |
10 |
0.00154709815979 |
mongoclient |
10 |
0.00501179695129 |
connection |
100000 |
6.14999890327 |
mongoclient |
100000 |
27.3278138638 |
connection |
1000000 |
59.0989689827 |
Mongoclient |
1000000 |
273.486183882 |
From the results of the above table can be seen, basically mongoclient the connection way to insert data is about 4 times times the connection connection method inserted data, of course, when the amount of data is small may establish a connection and some non-related operations may account for a larger proportion, the more stable later.
1, this let me have a question, pymongo the two ways of the implementation of the principle of how different, resulting in their write efficiency is so much difference?
2, the project of course to adopt a higher connection efficiency method, but now do not know the principle of implementation, the heart is relatively no end, not quite sure is the method of connection is relatively fast, but will not need to manually manage the connection resources, if the operation is too much, will not cause the use of resources
Also ask the gods to recommend some places to solve these problems, or to give a reference to my own study
Why Pymongo two ways to connect MongoDB's efficiency is so much different, to seek the principle of popular science