使用Eclipselink从OracleDB获取数据的时间很长



在我的应用程序中,我使用Eclipselink作为OracleDB的ORM,遇到了性能问题。

我正在执行这样的代码:

entityManager
.createNamedQuery(RoleToPermissionEntity.FIND_BY_APPLICATION_ROLE, RoleToPermissionEntity.class)
.setParameter(RoleToPermissionEntity.APPLICATION_ROLES_QUERY_PARAM, applicationRoles)
.getResultList();

带有命名查询:

SELECT mapping 
FROM RoleToPermissionEntity mapping 
WHERE mapping.applicationRole IN :applicationRoles 
ORDER BY mapping.id

实体管理器由@PersistenceContext设置。

对于3个给定的应用程序角色,应用程序获得123行(393行(,每个行9列(2个带时区的时间戳,3个数字,4个短varchars(。

我将执行时间检查为给定代码执行前后System.nanoTime()之间的差异。它大约是550毫秒,无论是第一次执行还是连续第十次执行。我的假设是它应该更快。

我的第一个猜测是查询有问题,所以我检查了Eclipselink日志。执行的查询为:

SELECT *all_columns* 
FROM *table_name* 
WHERE (APPLICATION_ROLE IN (?,?,?)) ORDER BY ID
bind => [3_application_roles]

我觉得还可以。我试着把它作为本机查询执行,但结果是一样的。我还尝试了其他查询,如SELECT * FROM table_name,但时间仍然是500-600ms。

我想对这次进行一些比较,所以我手动创建了数据库连接,并执行了如下查询:

Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection(database_args);
Statement statement = connection.createStatement();
statement.executeQuery(query);

我执行了好几次,第一次(当建立连接时(花了很长时间,但接下来花了50-60毫秒。

我的第二个猜测是连接池有问题。我试图在Eclipselink文档中找到一些东西,但我只注意到了这些参数:

<property name="eclipselink.connection-pool.default.initial" value="1"/>
<property name="eclipselink.connection-pool.default.min" value="16"/>
<property name="eclipselink.connection-pool.default.max" value="16"/>

应设置。确实如此,但问题仍然存在。

我的persistence.xml的内容:

<persistence>
<persistence-unit name=unit transaction-type="JTA">
<jta-data-source>datasource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!-- cache needs to be deactivated for multiple pods -->
<!-- https://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching -->
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<!--<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>-->
<property name="eclipselink.weaving" value="false"/>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.platform.database.oracle.Oracle12Platform"/>
<property name="eclipselink.connection-pool.default.initial" value="1"/>
<property name="eclipselink.connection-pool.default.min" value="16"/>
<property name="eclipselink.connection-pool.default.max" value="16"/>
</properties>
</persistence-unit>
</persistence>

我能做些什么来修复这种行为?

接下来的几个小时后,我发现了问题。OJDDBC的默认提取大小是10,所以随着行数的增加,提取时间增加得非常快。

奇怪的是:这是我的第一个想法,所以我尝试在persistence.xml中设置<property name="eclipselink.jdbc.fetch-size" value="100"/>。它不起作用,所以我跳到其他解决方案。今天,我通过query.setHint("eclipselink.jdbc.fetch-size", 100)将其设置为单查询,并且它有效。

最新更新