sql select语句,如何查找最后一行的数据

sql select语句,如何查找最后一行的数据,第1张

如果数据表有自增ID列的的话,可运行下列语句获得:

select

from

tableName

where

id=

(select

max(id)

from

tablename);

如果没有则增ID列的话,那就要使用游标了或者利用应用程序端编程获取最后那行记录了。当然如果版本支持ROW_NUMBER()函数的也可以用它获取最后一行数据。

问题描述 在通常的三层构架下 客户通过Browser请求Web服务器查询数据库 而查询结果是上千条甚至是上百万条记录 要求查询结果传送到客户端浏览器并分页显示

考虑因素

Web服务器的资源消耗 包括 内存(用来存储查询结果) 数据库相关资源(数据库连接对象 ResultSet对象等等)

DB服务器资源的消耗 包括游标 会话等等

网络开销 包括与数据库建立会话 传输查询结果等等

JDBC中的几个重要Class:

A ResultSet object maintains a cursor pointing to its current row of data Initially the cursor is positioned before the first row The next method moves the cursor to the next row and because it returns false when there are no more rows in the ResultSet object it can be used in a while loop to iterate through the result set

ResultSet是直接在数据库上建立游标 然后通过ResultSet的行位置定位接口来获得指定行位置的记录 当用户通过get方法获取具体纪录的内容时 ResultSet才从数据库把所需数据读到客户端

Oracle的ResultSet实现似乎会在本地缓存用户读取过的数据 导致内存消耗会随读取数据的增加而增加 这样 如果一次查询并读取海量数据 即使读出数据后马上丢弃(比如直接写入文件) 内存消耗也会随查询结果的增加而递增

The RowSet interface extends the standard java sql ResultSet interface A RowSet object may make a connection with a data source and maintain that connection throughout its life cycle in which case it is called a connected rowset A rowset may also make a connection with a data source get data from it and then close the connection Such a rowset is called a disconnected rowset A disconnected rowset may make changes to its data while it is disconnected and then send the changes back to the original source of the data but it must reestablish a connection to do so

RowSet是JDBC 中提供的接口 Oracle对该接口有相应实现 其中很有用的是 oracle jdbc rowset OracleCachedRowSet OracleCachedRowSet实现了ResultSet中的所有方法 但与ResultSet不同的是 OracleCachedRowSet中的数据在Connection关闭后仍然有效

解决方案一 直接使用ResultSet来处理

从ResultSet中将查询结果读入collection 缓存在>

以上就是关于sql select语句,如何查找最后一行的数据全部的内容,包括:sql select语句,如何查找最后一行的数据、Java中的大量数据查询、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9628424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存