Go语言 查询速度慢,schemaLock时间长

7rtdyuoh  于 2023-04-09  发布在  Go
关注(0)|答案(1)|浏览(153)

I am working on a project, in which I have used mongoDb as database. I am working on the cron, in which there was high mongoDb input/output operations, i.e. there was around 10k/s i/o operations on mongoDb including get, insert and update queries. My system specifications are:- RAM:- 384GB OS:- ubunutu 22.04 MongoDb version:- 4.4 MongoDb storage engine:- WiredTiger
During my entire cron, there was no change into the schema of the database, there was only get and update queries are running. In which around 95% of the queries are Get query.
My problem is that, mongoDb becomes slow after a certain period of time, to find out the problem, I start analysing slow query logs, and I have found that there was high time of schemaLock into the db queries.
{"t":{"$date":"2023-04-05T12:23:26.702+00:00"},"s":"I", "c":"COMMAND", "id":51803, "ctx":"conn4409","msg":"Slow query","attr":{"type":"command","ns":"canadaloctest1867_bk_db.users","command":{"find":"users","filter":{"_id":1,"status":{"$exists":true,"$ne":9}},"limit":1,"projection":{"status":1},"singleBatch":true,"lsid":{"id":{"$uuid":"c00762d0-98b6-41ab-adac-163039f60c0f"}},"$db":"canadaloctest1867_bk_db"},"planSummary":"IXSCAN { _id: 1 }","keysExamined":1,"docsExamined":1,"cursorExhausted":true,"numYields":1,"nreturned":1,"queryHash":"D7A7C611","planCacheKey":"1FD4C0F8","reslen":147,"locks":{"FeatureCompatibilityVersion":{"acquireCount":{"r":2}},"ReplicationStateTransition":{"acquireCount":{"w":2}},"Global":{"acquireCount":{"r":2}},"Database":{"acquireCount":{"r":2}},"Collection":{"acquireCount":{"r":2}},"Mutex":{"acquireCount":{"r":1}}},"storage":{"data":{"bytesRead":133345,"timeReadingMicros":126},"timeWaitingMicros":{"handleLock":120764,"schemaLock":8429126}},"protocol":"op_msg","durationMillis":8654}}
According to the mongoDb documentation, schemaLock will happen if we are modifying the schema. I have two questions:-

  • Why this schemaLock happens, if there was no change into the schema in the entire transaction?
  • What can be the possible solution to prevent this situation?
a8jjtwal

a8jjtwal1#

schemaLock是MongoDB在需要修改集合的schema时获取的锁,例如创建或删除索引。然而,schemaLock也可以被一些需要访问集合元数据的读操作获取,例如解释或listIndexes。这可能会导致与其他需要相同锁的操作发生争用。
防止这种情况的一些可能的解决方案是:

  • 减少架构修改的频率,例如创建或删除索引,尤其是在大型集合上。
  • 避免对其他操作频繁访问的集合运行explain或listIndexes。
  • 使用WiredTiger作为存储引擎,它具有比MMAPv1更细粒度的锁定机制。
  • 升级到MongoDB 4.2或更高版本,它通过减少schemaLock的范围和持续时间来提高其性能。
  • 使用探查器、Performance Advisor或db.currentOp.等工具监视和优化查询。还可以使用索引、投影和查询提示来提高查询效率。

相关问题