且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以最有效的方式获取带有JDBC的MySQL查询返回的结果计数

更新时间:2023-11-25 15:41:16

根据我的说法,这取决于您的用例.如果resultset足够小,则性能差异可以忽略不计.我非常怀疑resultset是否在较大结果集的主存储器中获取整个结果.其次,这是链接关于这种特殊情况.在这种情况下,使用last()getrows()后跟first()来计算大小显然是效率低下的,因为它必须首先将resultset的一部分加载到内存中并执行这些操作(并且不要忘记网络传输时间)以计算净尺寸.另一方面,count(*)只会进入并计算结果集中的行.

According to me it depends on your use case. If you resultset is small enough then the performance difference would be negligible. I highly doubt that the resultset fetches the whole result in the main memory for larger resultsets. To second this here is the link that talks about this particular scenario. In such case calculating size using last() , getrows() followed by first() would be obviously inefficient as it has to first load the portion of resultset in memory and perform these operations(and don't forget the network transfer time) to calculate the net size. On the other hand count(*) would just go in and count the rows in your result set.

这是我对哪个执行其他操作的理解.我愿意接受别人的任何意见