计算Golang中数据库查询的数量?

我试图计算 golangapi 服务器发出请求时对数据库运行的查询的数量,这是为了在我们的API中优化+查找瓶颈。

这有个可参考的例子:https://stackoverflow.com/questions/19556139/get-sql-query-count-during-a-django-shell-session

你知道在 Golang 是否有一个简单的方法可以做到这一点吗?

No easy solution that I'm aware of.

You could wrap your db in your own struct, and then implement Exec() or whichever function you use directly on that struct. Your function would just call the database one, and count it however you see fit.

A similar example, but with a logger, can be found here: How to log queries to database drivers?

In order to find bottlenecks I would suggest going straight to the MySQL database and enabling the slow query log and slowly setting the long_query_time down to a low millisecond value.

Use tools like pt-query-digest to help digest these to get a frequency of like queries. Attack those as slow queries that need fixing and then set a lower value.

The actual count of the queries on each isn't actually that useful.

When attacking the problem from a go point of view measuring the API response time of each interface will help you look holisticly at the service.