我不断查询数据库;应该有什么问题吧?
在我的应用程序中,我每秒使用相同的 sql 查询数据库。我需要知道我应该采取什么措施。它是否会导致任何问题,如“ 从池中获取连接之前已过了超时时间。”或任何其他类似的情况?
目前,我每秒创建一个新连接,并在使用后将其丢弃。在这种情况下我应该重用连接吗?
最好的方法是什么?
In my application, I am querying database with same sql every one second. I need to know what measure should I take. Will it ever cause any problem like " The
timeout period elapsed prior to obtaining a connection from the pool." or any other like that?
Currently, i am creating a new connection every second and the disposing it after it is used. Should I reuse connection in this case.
Whats the best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
恕我直言,最佳实践是尽快抽出并转储连接 - 使用它们来访问您需要的内容并立即关闭它们。在性能方面,您的敌人不是创建连接所需的开销(尽管其中涉及一些开销),而是锁定数据库中的表。
如果您希望优化您的应用程序,您应该尝试实现某种缓存机制,这样您就不必为每次查找都往返数据库。这对您的性能有利。
您可以做的另一件事是尽可能使用只读连接 - 它们比传统连接需要更少的开销,并且也会提高您的性能。
IMHO best practice is to pump-and-dump connections as quickly as possible - use them to get access to what you need and close them right away. Your enemy performance-wise isn't the overhead it takes to create a connection (although there is some involved there) - it's locking a table in your database.
If you're looking to optimize your application, you should try to implement some sort of caching mechanism that saves you from having to make a round-trip to the database for each lookup. That would be to your benefit performance-wise.
Another thing you can do is use read-only connections where you can - they require less overhead than traditional ones and will improve your performance also.
您每次都应该打开和关闭连接。事实上,如果您的
using
块在上次使用连接后有很多代码,请调用Close()
以尽快将其放回池中。这样,其他用途不需要打开全新连接的机会就会减少(请参阅“打开连接”实际上意味着什么?详细了解何时打开真正的连接以及何时从池中取出连接)。这“每秒一次”是不同线程的平均值,还是一个线程上的平均值?如果全部都在一个线程上,那并不重要,事实上,保持连接对象打开甚至可能稍微更快,因为无论哪种方式都不会发生争用。
我当然会考虑缓存结果,尽管这在内存使用方面有缺点,并且还可能存在有关何时需要刷新缓存结果的复杂问题 - 实际上,这可能是微不足道的,也可能是不可能的,具体取决于您正在做什么。
显然,在优化时还需要付出额外的努力。
You should definitely open and close the connection each time. Indeed, if your
using
block has much code after the last use of the connection, callClose()
to get it back in the pool as soon as possible. That way the chance of another use not needing to open a completely new connection is reduced (see What does "opening a connection" actually mean? for a bit more on when a real connection is opened and when one is taken from the pool).Is this "once a second" an average across different threads, or all on the one thread? If it's all on the one thread it doesn't matter, indeed it might even be slightly faster to keep the connection object open, because either way there won't be contention for it.
I would certainly consider caching results, though this has downsides in memory use, along with potentially complicated issues about when the cached results need to be refreshed - really this could be anywhere from trivial to impossible depending on just what you are doing.
It's also clearly a query to go that extra mile when optimising.
为什么需要这样做?
您可以尝试缓存数据以减少数据库的负载。您需要 1 秒的数据还是 5 秒的数据?
每次使用后关闭连接就可以了。它并没有真正关闭,它只是返回到连接池中。
Why do you need to do this?
You could try caching the data to reduce the load on your database. Do you need data that is 1 second old, or is 5 seconds ok.
Closing the connection after each time you use it is OK. It does not really get closed, it just goes back into the connection pool.
如果您使用的库确实为您汇集了连接,那么它没有什么区别。如果没有,那么最好多次使用同一个连接。创建连接非常耗时。
有几个问题...
If the library you're using does indeed pool the connections for you then it doesn't make a difference. If not, then it would be better to use the same connection multiple times. Creating a connection is time consuming.
A few questions...
如果您像这样处理连接,那么这种方法没有问题:
唯一的问题可能会发生 - 如果您的硬件不够好,则会降低您的数据库性能。
There is no problem in such approach if you dispose connections like this:
The only problem may happen - is decreasing your db performance if your hardware isn't good enought.