针对 PostgreSQL 8.4.6 的 java.sql.Connection 测试
我有一个带有 > 的 postgresql 数据库两个表中有 300 万行。 有些东西会降低整体性能,所以我用 Ubuntu 命令 pg_top 进行了一些分析。
我发现总是有这样的查询:
SELECT 1 FROM <tablename>;
看起来像来自java.sql.Connection
的连接测试语句,对吗?
当我在 postgres 命令中输入相同的命令时,需要4秒!索引都在那里,我查了一下。
问题: 如何更改 Connection 对象使用的测试语句?
多谢!
I have a postgresql DB with > 3 million rows in two tables.
Something slows down the whole performance, so I analyzed a bit with the Ubuntu command pg_top
.
I found out there is always a query like this:
SELECT 1 FROM <tablename>;
Looks like a connection test statement from the java.sql.Connection
, right?
When I enter the same command in the postgres command, it takes 4 seconds! Indexes are all there, I checked that.
Question:
How can I change the test statement used by the Connection object?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能正在直接或间接地通过数据源或其他一些框架(如 hibernate)使用某种连接池。
检查连接池的文档并搜索术语“验证查询”。
对于 postgres,您应该能够设置如下查询:
这应该要快得多。
You are probably using some kind of connection pool, directly or indirectly over a datasource or some other framework like hibernate.
Check in the documentation of the connection pool and search for the term 'validation query'.
For postgres you should be able to set a query like:
This should be much faster.
根本原因已找到:
我的代码所依赖的框架确实测试了该表是否可用。
这是通过
This 实际上返回该表中的所有行来完成的,因为其中有 300 万行,所以需要一段时间。
一种更好的方法(至少对于 POSTGRESQL 来说)是这样的:
感谢大家已经发表的评论!
请参阅解决方案的来源。
Root Cause has been found:
A framework my code was relying on did test whether the table is available or not.
This was done via
This actually returns ALL ROWS from that table and because I have 3 million rows in it, it takes a while.
A much better approach (at least for POSTGRESQL) is this one:
Thanks for all for the already posted comments!
See source of the solution .