[20250125]如何编译与运行FetchSize.java.txt
--//https://connor-mcdonald.com/2025/01/17/jdbc-the-fetchsize-and-staying-up-to-date/
--//看了里面的链接,测试看看。
$ cat FetchSize.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FetchSize {
public static void main(String[] args) throws SQLException {
try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:scott/book@//127.0.0.1/book01p")) {
// try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:MYUSER/MYPASS@//MYHOST/MYDB?defaultRowPrefetch=1000")) {
try (Statement stmt = con.createStatement()) {
System.out.println("Creating test table");
try {
stmt.execute("drop table tmp_fetch_demo purge");
}
catch (SQLException ignored) {
}
stmt.execute("create table tmp_fetch_demo as select a.* from all_objects a, ( select 1 from dual connect by level <= 10) ");
// stmt.execute("""
// create table tmp_fetch_demo as
// select a.*
// from all_objects a, (select 1 from dual connect by level <= 50)""");
System.out.println("Done.");
// If you want to trace this
//
// stmt.execute("alter session set tracefile_identifier = FETCH");
// stmt.execute("alter session set sql_trace = true");
int newFetchSize;
if (args.length > 0 && (newFetchSize = Integer.parseInt(args[0])) > 0) {
System.out.println("Setting JDBC Statement default fetch size to " + newFetchSize);
stmt.setFetchSize(newFetchSize);
}
else if (stmt.getFetchSize() == 10) {
System.out.println("Keeping default JDBC Statement default fetch size (" + stmt.getFetchSize() + ")");
}
else {
System.out.println("JDBC Statement default fetch size set to " + stmt.getFetchSize() + " using connection string parameter");
}
try (ResultSet rs = stmt.executeQuery("select object_id, object_name from tmp_fetch_demo")) {
System.out.println("Querying all rows");
long startTime = System.nanoTime();
long cnt = 0;
while (rs.next()) {
int v1 = rs.getInt(1);
String v2 = rs.getString(2);
cnt++;
}
System.out.println("ResultSet fetch size " + rs.getFetchSize());
long duration = (System.nanoTime() - startTime) / 1000000L;
long tp = (long) ((double) cnt / duration * 1000d);
System.out.printf("Rows fetched %d%nDuration %d ms%nThroughput %d rows/sec%n", cnt, duration, tp);
}
}
}
}
}
--//已经根据要求修改连接数据库的字符串。仅仅减少select 1 from dual connect by level <= 10,不然建立的表有点大。
--//这样建立的表为120M。
$ javac FetchSize.java
--//编译通过。
$ java FetchSize 0
Error: Could not find or load main class FetchSize
$ java FetchSize 100
Error: Could not find or load main class FetchSize
--//不熟悉java,视乎提示找不到对应的class FetchSize。
$ java -cp . FetchSize 100
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:scott/book@//127.0.0.1/book01p
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at FetchSize.main(FetchSize.java:9)
--//jdbc没有找到。
$ java -cp /u01/app/oracle/product/21.0.0/dbhome_1/jdbc/lib/ojdbc8.jar:. FetchSize 100
Creating test table
Done.
Setting JDBC Statement default fetch size to 100
Querying all rows
ResultSet fetch size 100
Rows fetched 699150
Duration 1066 ms
Throughput 655863 rows/sec
$ java -cp /u01/app/oracle/product/21.0.0/dbhome_1/jdbc/lib/ojdbc8.jar:. FetchSize 0
Creating test table
Done.
Keeping default JDBC Statement default fetch size (10)
Querying all rows
ResultSet fetch size 10
Rows fetched 699150
Duration 3471 ms
Throughput 201426 rows/sec
$ java -cp /u01/app/oracle/product/21.0.0/dbhome_1/jdbc/lib/ojdbc8.jar:. FetchSize
Creating test table
Done.
Keeping default JDBC Statement default fetch size (10)
Querying all rows
ResultSet fetch size 10
Rows fetched 699150
Duration 3563 ms
Throughput 196225 rows/sec
--//设置0的缺省等于10。两者时间相差3倍。
--//不加参数如何执行,不是很清楚,那位知道。
--//应该设置某个环境变量就可以执行,不熟悉java。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-3070828/ ,如需转载,请注明出处,否则将追究法律责任。