`

SELECT TOP n, Statement.setMaxRows(), Statement.setFetchSize()

 
阅读更多

**************************Answer 1********************************************

SetFetchSize() has nothing to do with what you get back from a query. It is only a hint from the coder to the driver (which the driver may ignore) about how many rows to fetch from the DBMS at a time. In other words, if the query returns 100 rows and you set fetch size to 10, the driver will fetch ten rows to start, and when you call next() the eleventh time, the driver will go to the DBMS and get the next ten rows. This trades client memory for minimizing client-DBMS roundtrips. A given driver and DBMS may or may not be able to alter their fetch size, so the driver may ignore your hint.


SELECT TOP is a specific one-time query criterion, implemented and satisfied by the DBMS. The driver knows nothing about it and gets whatever rows the DBMS sends. The setMaxRows() call means the driver will simply count the rows returned, and give you access only to the number you set. The DBMS still sends all the data, and the driver may still have to read all the data in order to clear the line.

Also note that this statement setting remains until reset. If you knowingly or unknowingly re-use the statement (such as if there is a statement cache in the driver or pool implementation) you will retain this non-default behavior. This means that it is better to use "SELECT TOP" when you know you need it. The other option is to write standard SQL (albeit fancy) that gets you only the rows you want. "SELECT TOP" if used with an "ORDER BY" clause may require the DBMS to internally select all the data, and make an internal temp table with all of it, and then sort it according to your "ORDER BY" clause, if there's no appropriate index to the original data


HTH,

Joe Weinstein at BEA Systems.


**************************Answer 2*********************************************


setFetchSize(int) defines the number of rows that will be read from the database when the ResultSet needs more rows. The method in the java.sql.Statement interface will set the 'default' value for all the ResultSet derived from that Statement; the method in the java.sql.ResultSet interface will override that value for a specific ResultSet. Since database fetches can be expensive in a networked environment, fetch size has an impact on performance.

setMaxRows(int) sets the limit of the maximum nuber of rows in a ResultSet object. If this limit is exceeded, the excess rows are "silently dropped". That's all the API says, so the setMaxRows method may not help performance at all other than to decrease memory usage. A value of 0 (default) means no limit.

Since we're talking about interfaces, be careful because the implementation of drivers is often different from database to database and, in some cases, may not be implemented or have a null implementation. Always refer to the driver documentation. 

分享到:
评论

相关推荐

    sqljdbc.jar包

    8. **性能优化**:`sqljdbc.jar` 支持一些高级特性,如`Statement.setFetchSize()` 和 `ResultSet.setFetchSize()` 可以控制结果集的缓存大小,减少内存消耗;`Statement.setMaxRows()` 可以限制查询返回的最大行数...

    mysql-connector-java-5.1.30工具

    4. **结果集分页**:使用`LIMIT`子句或`Statement.setMaxRows()`限制返回结果的数量,实现数据分页。 5. **元数据查询**:利用`DatabaseMetaData`接口获取表结构、列信息等数据库元数据。 ### 注意事项 1. **版本...

    使用JDBC连接数据的步骤.doc

    `Statement`对象支持执行SQL查询和命令,例如`SELECT`, `UPDATE`, `INSERT`, 和 `DELETE`。 4. **执行SQL语句和处理结果** - `executeQuery()`: 用于执行SQL查询,并返回一个`ResultSet`对象,包含查询结果。 - ...

    java操作MSSQL数据库[参照].pdf

    stmt.setMaxRows(100); // 设置最大输出记录数 ResultSet rs = stmt.executeQuery("SELECT * FROM pJoiner"); ``` - 遍历`ResultSet`获取数据: ```java while (rs.next()) { int userId = rs.getInt("userid...

    java操作MSSQL数据库.pdf

    stmt.setMaxRows(100); // 控制最多返回100条记录 ResultSet rs = stmt.executeQuery("SELECT ..."); while (rs.next()) { int userId = rs.getInt("userid"); String userName = rs.getString("username"); // ....

    java一次性查询处理几百万数据解决方法

    stat.setMaxRows(onerun); stat.setFetchSize(1000); rs = stat.executeQuery(sql); String text = ""; int i = 1; while (rs.next()) { text += rs.getString(2) + "|" + rs.getString(3) + "|" + rs....

    snow-jdbc:Snowflake JDBC驱动程序

    2. **结果集缓存**:利用`Statement.setMaxRows()`限制返回的结果集大小。 3. **批量插入**:使用`PreparedStatement.addBatch()`和`PreparedStatement.executeBatch()`进行批量数据插入。 4. **错误处理**:通过...

    jsp中的数据库编程

    stmt.setMaxRows(10); // 设置返回的最大行数 ResultSet rs = stmt.executeQuery("SELECT * FROM table_name"); ``` **2. 处理查询结果** - 可以通过 `ResultSet` 对象获取查询结果中的数据。 - **代码示例**...

    java开发之Jdbc分页源码详解

    PreparedStatement pre = this.getConn().prepareStatement(sql); pre.setMaxRows(maxSize); ResultSet rs = pre.executeQuery(); rs.absolute(firstSize * maxSize); return rs; } ``` relative方式 ...

    在JSP页面中实现检索数据的分页显示.doc

    stmt.setMaxRows(PAGE_SIZE); // 构造SQL查询,例如 "SELECT * FROM table WHERE condition LIMIT offset, PAGE_SIZE" String sql = buildSql(condition, calculateOffset(request.getParameter("page"))); ...

    H2Database高级特性.pdf

    为了避免不必要的性能开销,可以使用LIMIT子句或Statement.setMaxRows(max)方法来限制返回的结果集行数。对于大结果集,数据库会将其缓存到磁盘,并可以通过SETMAX_MEMORY_ROWS来设置阀值。 H2数据库在存储BLOB(二...

    JDBCWeb试题(卷)答案解析.doc

    7. 限制查询结果数量,可使用Statement的setMaxRows方法 8. ResultSet中的Cursor定位方法: - next():移动到下一行 - beforeFirst():移动到结果集开头 - afterLast():移动到结果集末尾 - isBeforeFirst():...

    web测试题[收集].pdf

    使用Statement的`setMaxRows(int max)`方法可以限制查询返回的最大记录数。 12. JDBC 2.0新特性: JDBC 2.0引入了许多增强功能,比如ResultSet的滚动(scrolling)能力,允许在结果集中前后移动,即使结果集没有...

    JDBC分页 absolute实现

    标签中的"sexMaxrows"可能是打错了,正常应该是"setMaxRows",这是一个`Statement`对象的方法,用于设置结果集的最大行数,它可以用来限制查询返回的结果数量,但并不直接用于分页。 综上所述,JDBC中的`absolute()...

    java试题与解答(web部分)

    A(setFetchSize)设置结果集每次获取的行数,C(setMaxRows)限制查询返回的最大行数,B(setMaxFieldSize)限制列的最大长度。这些方法可以帮助减少内存消耗和提高处理速度。 8. ResultSet是一个游标,可以用来...

    jsp动态网站开发与实例之数据变动功能网页.pptx

    此外,`setMaxRows(int max)`方法用于设置ResultSet中最多能容纳的数据条数,而`getConnection()`方法则返回创建Statement对象的Connection对象。 ResultSet接口是用于存储Statement执行后返回的结果集,它就像是一...

    jsp servlet 分页

    为了限制每次查询返回的数据量,可以通过设置`Statement`对象的`setMaxRows(int max)`方法,限制最多返回`max`条记录。这对应于每一页的数据量。在Servlet中,根据当前页数和每页显示的记录数计算出应获取的数据...

    Jdbc_xml_html_css_js选择题(含答案).doc

    2. 在Java中,限制SQL查询返回最多记录数的方法是`setMaxRows()`. 选项C是正确的选择。 3. PreparedStatement继承自Statement,这是一个用于执行预编译SQL语句的接口,能够提高效率并防止SQL注入。因此,选项A是...

    数据库的分页查询.doc

    在Java中,使用JDBC时,最初可能会尝试使用PreparedStatement的setMaxRows方法和ResultSet的absolute方法来定位并读取指定范围的数据。然而,当数据量巨大,页数众多时,这种方法效率低下,因为需要跳过大量记录。...

    JDBC使用游标实现分页查询的方法

    - 使用`setMaxRows(startNo+maxCount-1)`限制返回的最大记录数,这里`startNo`表示从第几条记录开始,`maxCount`表示总共获取多少条记录。 - 通过`first()`将游标移动到结果集的第一条记录,再使用`relative...

Global site tag (gtag.js) - Google Analytics