Why does a simple query take more than 2 seconds using the Golang Mongo driver?

王林
Release: 2024-02-06 09:30:08
forward
1131 people have browsed it

为什么使用 Golang Mongo 驱动程序执行一个简单的查询需要花费 2 秒以上的时间?

Question content

I am writing a golang web service with mongo database, I am using go.mongodb.org/mongo-driver v1.11.6 , a simple query takes more than 2 seconds to complete. The database only has a few records, just for testing, no more than 10 records.

I was looking for the part of the code that was wasting my time and I found that the problem was with the mongo package. The find, findone and even insert and update methods all take more than 1 or 2 seconds to complete.

This is the mongo client instantiation

func newmongodb() *mongodb { uri := config.getenvconfig().mongouri database := config.getenvconfig().mongodatabase client, err := mongo.connect(context.todo(), options.client().applyuri(uri)) if err != nil { panic(err) } return &mongodb{ client: client, database: database, } }
Copy after login

This is the function code:

func getmessagebyidfromdb(id string) (*message, error) { conn := database.newmongodb() defer conn.disconnect() filter := map[string]string{ "message_id": id, } var message message start := time.now() err := conn.getcollection(collectionname).findone(context.todo(), filter).decode(&message) elapsed := time.since(start) log.printf("querying messages took %s", elapsed) if err != nil { return nil, err } return &message, nil }
Copy after login

This is the result of function time tracking:

Querying messages took 2.320409472s
Copy after login

This has nothing to do with the internet connection, as I tested the query with python and the request only took 0.003 seconds

I tried changing the version of the package but it didn't work. I also tried reinstalling all packages for the project with the same result.

I also tried creating a search index in the database but didn't get different results. The query also takes more than 2 seconds to complete.

I don't think the query should take more than a few milliseconds to complete.


Correct answer


##mongo.Connect()"only" Initialize by starting the background monitoring goroutineClient. There may not be a need to connect to a (remote) database.

When you execute queries, they of course need to establish a connection, which can take a few seconds.

You can use the

Client.Ping()method before forcing a connection to the database and verifying that the connection was created successfully, so when you execute the query, the connection will be ready.

You can also try repeating the same query because after the first query the connection is not closed but put into the connection pool and reused when needed again (for the second query).

The above is the detailed content of Why does a simple query take more than 2 seconds using the Golang Mongo driver?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!