You connect to your database in your handler, every time a request is made to your server. This operation alone could take multiple seconds. Once the handler returns, you just throw away that connection (you don't even close that, so that may be idling for some time before getting closed, hogging db server resources as connections are most likely limited on the server). Don't do that.
Connect to your database once, on app startup, and use this connection in your handler. The database will keep idle connections open, ready to be reused immediately should you need it in another request.
db variable outside, e.g. to a package-level variable, connect to your database and initialzie this
db variable once, e.g. in your
main() or in a package
init() function, and just use it in your handler.
sql.Open() documents that:
The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
See similar question: mgo - query performance seems consistently slow (500-650ms)