简体   繁体   English

Java MySQL JDBC慢/慢

[英]Java MySQL JDBC Slow/Taking turns

We're currently trying to make our server software use a connection pool to greatly reduce lag however instead of reducing the time queries take to run, it is doubling the time and making it even slower than it was before the connection pool. 我们目前正在尝试使我们的服务器软件使用连接池来大大减少延迟,但是,与其减少查询运行时间,不如将其加倍,甚至比连接池之前还慢。

Are there any reasons for this? 有什么原因吗? Does JDBC only allow a single query at a time or is there another issue? JDBC一次只允许一个查询还是有另一个问题?

Also, does anyone have any examples of multi-threaded connection pools to reduce the time hundreds of queries take as the examples we have found only made it worse. 而且,没有人有任何多线程连接池的示例来减少数百次查询所花费的时间,因为我们发现这些示例只会使情况变得更糟。

We've tried using BoneCP and Apache DBCP with similar results... 我们已经尝试过使用BoneCP和Apache DBCP获得类似的结果...

That one is using Apache's DBCP. 那是使用Apache的DBCP。 We also have tried using BoneCP with the same result... 我们还尝试了使用BoneCP的结果相同...

A connection pool helps mitigating the overhead/cost of creating new connections to the database, by reusing already existing ones. 通过重新使用现有连接池,连接池有助于减轻创建数据库新连接的开销/成本。 This is important if your workload requires many, short to medium living connections, eg an app that processes concurrent user requests by querying the database. 如果您的工作负载需要许多短至中等的实时连接(例如,通过查询数据库处理并发用户请求的应用程序),则这一点很重要。 Unfortunately your example benchmark code does not have such a profile. 不幸的是,您的示例基准代码没有这样的配置文件。 You are just using 4 connections in parallel and there is no reuse involved. 您仅并行使用4个连接,并且不涉及重用。

What a connection pool cannot achieve is magically speeding up execution times or improving the concurrency level beyond that, which is provided by the database. 连接池无法实现的是,神奇地加快了执行时间或提高了并发级别,这超出了数据库所提供的水平。 If the benchmark code represents the expected workload, I would advise you to look into batching statements instead of threading. 如果基准代码代表预期的工作量,我建议您研究批处理语句而不是线程。 That will massively increase performance of INSERT/UPDATE operations. 这将大大提高INSERT / UPDATE操作的性能。

update : 更新

Using multiple connections in parallel can enhance performance. 并行使用多个连接可以提高性能。 Just keep in mind, that there is not necessarily a relation between multiple threads in your Java application and in the database. 请记住,Java应用程序和数据库中的多个线程之间不一定存在关系。 JDBC is just a wrapper around the database driver, using multiple connections results in multiple queries being submitted to the database server in parallel. JDBC只是数据库驱动程序的包装,使用多个连接会导致将多个查询并行提交到数据库服务器。 If those queries are suited for it, every modern RDBMS will be able to process them in parallel. 如果这些查询适合它,那么每个现代RDBMS都将能够并行处理它们。 But if those queries are very work intensive, or even worse include table locks or conflicting updates, the DB may not be able to do so. 但是,如果这些查询的工作量很大,或者更糟糕的是包括表锁或冲突的更新,则数据库可能无法这样做。 If you experience bad performance, check which queries are lagging and optimize them (are they efficient? proper indexes in place? denormalizing the schema may help in more extreme cases. Use prepared statements and batch mode for larger updates, etc.). 如果您的性能不佳,请检查哪些查询滞后并对其进行优化(它们是否有效?是否有适当的索引?对模式进行非规范化可能在更极端的情况下有所帮助。对更大的更新使用准备好的语句和批处理模式等)。 If your db is overloaded with many, similar and small queries, consider caching frequently used data. 如果您的数据库过载了许多类似的小型查询,请考虑缓存常用数据。

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

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