简体   繁体   English

Mysql连接池问题:值得吗?

[英]Mysql connection pooling question: is it worth it?

I recall hearing that the connection process in mysql was designed to be very fast compared to other RDBMSes, and that therefore using a library that provides connection pooling (SQLAlchemy) won't actually help you that much if you enable the connection pool. 我记得听说mysql中的连接过程与其他RDBMS相比设计得非常快,因此如果启用连接池,那么使用提供连接池 (SQLAlchemy) 的库实际上不会对您有太大帮助。

Does anyone have any experience with this? 有人对这个有经验么?

I'm leery of enabling it because of the possibility that if some code does something stateful to a db connection and (perhaps mistakenly) doesn't clean up after itself, that state which would normally get cleaned up upon closing the connection will instead get propagated to subsequent code that gets a recycled connection. 我很乐意启用它,因为如果某些代码对数据库连接做了某些有状态的事情并且(可能是错误地)不会自行清理,那么在关闭连接时通常会被清理的状态将改为传播到后续代码,获得循环连接。

There's no need to worry about residual state on a connection when using SQLA's connection pool, unless your application is changing connectionwide options like transaction isolation levels (which generally is not the case). 使用SQLA的连接池时,无需担心连接上的剩余状态,除非您的应用程序正在更改连接范围的选项,如事务隔离级别(通常情况并非如此)。 SQLA's connection pool issues a connection.rollback() on the connection when its checked back in, so that any transactional state or locks are cleared. 签入时,SQLA的连接池在连接上发出connection.rollback(),以便清除任何事务状态或锁。

It is possible that MySQL's connection time is pretty fast, especially if you're connecting over unix sockets on the same machine. MySQL的连接时间可能非常快,特别是如果你在同一台机器上连接unix套接字。 If you do use a connection pool, you also want to ensure that connections are recycled after some period of time as MySQL's client library will shut down connections that are idle for more than 8 hours automatically (in SQLAlchemy this is the pool_recycle option). 如果您确实使用了连接池,那么您还需要确保在一段时间后回收连接,因为MySQL的客户端库将自动关闭空闲超过8小时的连接(在SQLAlchemy中这是pool_recycle选项)。

You can quickly do some benching of connection pool vs. non with a SQLA application by changing the pool implementation from the default of QueuePool to NullPool, which is a pool implementation that doesn't actually pool anything - it connects and disconnects for real when the proxied connection is acquired and later closed. 通过将池实现从默认的QueuePool更改为NullPool,您可以快速完成连接池与非SQLA应用程序的连接,NullPool是一个实际上不会集合任何内容的池实现 - 它连接和断开连接时为真实的获取代理连接,然后关闭。

Even if the connection part of MySQL itself is pretty slick, presumably there's still a network connection involved (whether that's loopback or physical). 即使MySQL本身的连接部分非常光滑,也可能存在涉及的网络连接(无论是环回还是物理连接)。 If you're making a lot of requests, that could get significantly expensive. 如果你提出了很多要求,那么这可能会非常昂贵。 It will depend (as is so often the case) on exactly what your application does, of course - if you're doing a lot of work per connection, then that will dominate and you won't gain a lot. 它取决于(通常情况下)你的应用程序究竟在做什么,当然 - 如果你在每个连接上做了很多工作,那么这将占主导地位并且你不会获得很多。

When in doubt, benchmark - but I would by-and-large trust that a connection pooling library (at least, a reputable one) should work properly and reset things appropriately. 如果有疑问,那么基准测试 - 但我总体上相信连接池库(至少是一个有信誉的库)应该正常工作并适当地重置事物。

Short answer: you need to benchmark it. 简短的回答:你需要对它进行基准测试。

Long answer: it depends. 答案很长:这取决于。 MySQL is fast for connection setup, so avoiding that cost is not a good reason to go for connection pooling. MySQL快速进行连接设置,因此避免这种成本并不是连接池的好理由。 Where you win there is if the queries run are few and fast because then you will see a win with pooling. 你赢了的地方就是如果查询运行很少而且速度很快,因为那样你就会看到一个带池的胜利。

The other worry is how the application treats the SQL thread. 另一个担心是应用程序如何处理SQL线程。 If it does no SQL transactions, and makes no assumptions about the state of the thread, then pooling won't be a problem. 如果它没有SQL事务,并且不对线程的状态做出任何假设,那么池化将不会成为问题。 OTOH, code that relies on the closing of the thread to discard temporary tables or to rollback transactions will have a lot of problems with pooling. OTOH,依赖于关闭线程来丢弃临时表或回滚事务的代码在池化时会遇到很多问题。

The connection pool speeds things up in that fact that you do not have create a java.sql.Connection object every time you do a database query. 连接池可以加快速度,因为每次执行数据库查询时都没有创建java.sql.Connection对象。 I use the Tomcat connection pool to a mysql database for web applications that do a lot of queries, during high user load there is noticeable speed improvement. 我将Tomcat连接池用于mysql数据库,用于执行大量查询的Web应用程序,在高用户负载期间,速度明显提高。

I made a simple RESTful service with Django and tested it with and without connection pooling. 我用Django做了一个简单的RESTful服务,并在有和没有连接池的情况下对它进行了测试。 In my case, the difference was quite noticeable. 就我而言,差异非常明显。

In a LAN, without it, response time was between 1 and 5 seconds. 在局域网中,没有它,响应时间在1到5秒之间。 With it, less than 20 ms. 有了它,不到20毫秒。 Results may vary, but the configuration I'm using for the MySQL & Apache servers is pretty standard low-end. 结果可能会有所不同,但我用于MySQL和Apache服务器的配置是非常标准的低端。

If you're serving UI pages over the internet the extra time may not be noticeable to the user, but in my case it was unacceptable, so I opted for using the pool. 如果您通过互联网提供UI页面,则用户可能无法注意到额外的时间,但在我的情况下,这是不可接受的,因此我选择使用该池。 Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM