简体   繁体   English

如何使用 JDBC 跟踪大型 SQL 服务器查询的进度?

[英]How to track progress of a large SQL Server query with JDBC?

My JavaFX application has a process that loads a very large amount of data from a single SQL Server query at startup.我的 JavaFX 应用程序有一个进程在启动时从单个 SQL 服务器查询加载大量数据。 This can often take several minutes to complete.这通常需要几分钟才能完成。

My goal is to include a ProgressBar to show the current/total rows that have already been retrieved, but I can not find a way to retrieve the progress of a SELECT statement with JDBC.我的目标是包含一个ProgressBar来显示已经检索到的当前/总行数,但是我找不到一种方法来检索SELECT语句和 JDBC 的进度。

Right now, I am just executing the one query to get a single ResultSet in return when all data has been returned by the server.现在,当服务器返回所有数据时,我只是执行一个查询以获取单个ResultSet作为回报。

Is it possible to "listen" to the progress of a query in this way?是否可以通过这种方式“监听”查询的进度? Or some way to limit the query (ie: SELECT TOP 100 ) in a loop, picking up where the last iteration left off, until all results have been retrieved?或者以某种方式限制循环中的查询(即: SELECT TOP 100 ),从上次迭代停止的地方开始,直到检索到所有结果?

With JDBC there is no way to see what is the status of the query because the query is being executed by the database.使用 JDBC 无法查看查询的状态,因为查询正在由数据库执行。 So, the JDBC driver sends the query to database and waits for a response.因此,JDBC 驱动程序将查询发送到数据库并等待响应。 JDBC is the one that get the results, it doesn't track the complexity of the query or the status of one. JDBC 是获得结果的那个,它不跟踪查询的复杂性或一个的状态。 My suggestion for solving this problem is to get the total count of documents of being processed, then make a batch of queries.我解决这个问题的建议是获取正在处理的文档总数,然后进行一批查询。 That's the way I made for a large set of results.这就是我获得大量结果的方式。 Divide and conquer.分而治之。

The anwser given by @Gatusko is good but does not solve your problem... @Gatusko 给出的答案很好,但不能解决你的问题......

Actually you will see everywhere that progress bar is less and less used, because it is not adapted for SOA architectures (distant calls of processes).实际上,您会到处看到进度条越来越少使用,因为它不适用于 SOA 架构(进程的远程调用)。

The trick that replace the progress bar is a circle thats turns eternally until the result appear, Which such a component, there is no needs to requests the state of progress of the process, that you really never can have in a RDBMS !替换进度条的技巧是一个循环,直到结果出现,这样的组件,没有必要请求进程的进度,你真的永远不可能在RDBMS中拥有!

First do a select count(*) to know the total number of rows.首先做一个select count(*)来知道总行数。

Instead of getting a single ResultSet , invoke the use of a cursor, updating your progress meter as you process rows:与其获取单个ResultSet ,不如调用 cursor,在处理行时更新进度表:

conn.setAutoCommit(false); // you must turn auto commit off
Statement stmt = conn.createStatement();

stmt.setFetchSize(1000); // Turn use of the cursor on
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
int count = 0;
while (rs.next()) {
    // process row
    // update progress meter every 10000 rows
    if (++count % 10000 == 0) {
        float progress = 100f * count / totalRows;
        // display progress
    } 
}

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

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