Is the performance of mongodb,upsert getting worse as the number of documents increases?

Is the performance of

upsert getting worse as the number of documents increases?
what if it"s insert? Wouldn"t there be any of the above problems?
I now have a scenario of writing more and reading less. How to ensure the optimal write speed, and as there is more and more data in the collection, the write speed will not get worse?

Thank you

Mar.04,2021

upsert is better than insert , definitely insert . Because upsert requires find to exist before insert , which has extra overhead. But there is no essential difference in theory, because the time complexity of B-tree is O (log2 (N)) -- the time consumed does not increase significantly with the increase of data volume:
clipboard.png

but if the condition of upsert fails to hit the index, then the time complexity is that O (N), can also see the 45-degree line in the image above, which means that the time is proportional to the amount of data.

so the conclusion is:

  • if the condition of upsert can hit the index, it will not differ much from insert in theory, nor will it change obviously with the increase of data volume.
  • if the condition of upsert fails to hit the index, the time spent increases in proportion to the amount of data.

the theory is that in engineering you have to make sure that there is enough room for the index, otherwise swapping with disk will greatly slow down the speed of index search. The more exchanges, the worse the performance, which breaks the curve of O (log2 (N)) )

.
Menu