插入第一个元素时为什么 collection.InsertOne 的速度较慢?
来源:stackoverflow
时间:2024-03-20 16:21:31 184浏览 收藏
在使用 Go 的官方 MongoDB 驱动程序时,首次调用 collection.InsertOne 会出现明显延迟,而后续调用则非常迅速。这是因为该驱动程序使用延迟连接,首次调用时需要建立连接。可以通过使用 context.WithTimeout 强制连接来避免这种延迟。
问题内容
我正在测试 go 的新官方 mongodb 驱动程序,我注意到第一次调用 collection.insertone 总是需要大量时间,而所有后续调用都非常快。为什么?如何避免这种破坏性行为?
package main import ( "context" "log" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type trainer struct { name string age int city string } func main() { t1 := time.now() // set client options clientoptions := options.client().applyuri("mongodb://localhost:27017") log.println("setting client options took", time.now().sub(t1)) t1 = time.now() // connect to mongodb client, err := mongo.connect(context.todo(), clientoptions) if err != nil { log.fatal(err) } log.println("connecting took", time.now().sub(t1)) t1 = time.now() // some dummy data to add to the database ash := trainer{"ash", 30, "pallet town"} // get a handle for your collection collection := client.database("test").collection("trainers") // insert a single document log.println("getting the collection took", time.now().sub(t1)) t1 = time.now() for i := 0; i < 10; i++ { _, err := collection.insertone(context.todo(), ash) if err != nil { log.fatal(err) } log.println("inserting document took", time.now().sub(t1)) t1 = time.now() } err = client.disconnect(context.todo()) }
我预计所有插入操作都需要毫秒或纳秒,而第一个插入操作大约需要 0.6 秒。这是带有计时的日志:
2019/07/31 17:41:39 Setting client options took 0s 2019/07/31 17:41:39 Connecting took 0s 2019/07/31 17:41:39 Getting the collection took 0s 2019/07/31 17:41:40 Inserting document took 606.0339ms 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 875.2µs 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s 2019/07/31 17:41:40 Inserting document took 0s
解决方案
只是为了结束评论中的问题:
mongodb 驱动程序使用 lazy connection。具体请参阅 MongoDB docs:
ctx, _ = context.WithTimeout(context.Background(), 2*time.Second) err = client.Ping(ctx, readpref.Primary())
这将强制连接并消除第一次插入时的插入延迟。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习