Skip to content

README: Recommend some DB settings #1141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2020
Merged

Conversation

methane
Copy link
Member

@methane methane commented Jul 28, 2020

Description

Recommend SetConnMaxLifetime, SetMaxOpenConns, and SetMaxIdleConns.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

Co-authored-by: Ichinose Shogo <[email protected]>
Copy link
Contributor

@shogo82148 shogo82148 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@methane methane merged commit e2ee2f3 into go-sql-driver:master Aug 16, 2020
@methane methane deleted the update-readme branch August 16, 2020 08:27
tz70s pushed a commit to tz70s/mysql that referenced this pull request Sep 5, 2020
tz70s pushed a commit to tz70s/mysql that referenced this pull request Sep 5, 2020
@dolmen
Copy link
Contributor

dolmen commented Feb 1, 2021

The README now says "Since some middlewares close idle connections by 5 minutes,". Could you be more precise about which middlewares you are talking about?

@zhixinwen
Copy link
Contributor

https://golang.org/pkg/database/sql/#DB.SetMaxOpenConns
golang doc says

If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit.

so MaxIdleConns <= MaxOpenConns, why are we suggesting setting higher idle connection number here?

@methane
Copy link
Member Author

methane commented May 22, 2021

why are we suggesting setting higher idle connection number here?

I wrote "same to (or greater than)" because greater value become same value automatically.
If it is confusing, I'm OK to remove "(or greater than)" part.

@zhixinwen
Copy link
Contributor

@methane can you explain why you suggest to set max open connections equal to max idle connections?
Looks like sql.DB is still trying to make additional connections when there are idle connections (maybe due to burst of traffic), more details here: https://stackoverflow.com/questions/67653872/golang-sql-db-waitcount-greater-than-0-even-when-there-are-enough-idle-connectio

@methane
Copy link
Member Author

methane commented May 23, 2021

@methane can you explain why you suggest to set max open connections equal to max idle connections?

I explained it already. "When it is smaller than SetMaxOpenConns(), connections can be opened and closed very frequently than you expect."

Looks like sql.DB is still trying to make additional connections when there are idle connections (maybe due to burst of traffic), more details here: https://stackoverflow.com/questions/67653872/golang-sql-db-waitcount-greater-than-0-even-when-there-are-enough-idle-connectio

It looks like what you see is what I said. DB opens new connection because no idle connections. You didn't see high number of InUse just because number of InUse connections is moving like "spike", and your sampling rate is too slow to catch the spike.

You should try this as I recommended in the README.

db.SetMaxOpenConns(100)
db.SetMaxIdleConns(100)
db.SetConnMaxLifetime(time.Minute * 3)

If you can not believe me, you should get DB.Stats() every time before using DB methods. You will catch the spike.

@shogo82148 shogo82148 mentioned this pull request May 24, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants