Java Resultset.get***(...) 通过字符串或整数更快?

发布于 2024-09-15 00:29:28 字数 313 浏览 9 评论 0原文

当以标准方式迭代结果集时

while (rs.next()){
...
}

要检索列数据(例如长数据),使用起来更快

rs.getLong(String columnLabel)

,或者

rs.getLong(int columnIndex)

大概使用columnLabel可以更好地以多种方式使用以获得更强大的代码,但是通过将列与字符串匹配是否会带来重大的操作损失每次(我们谈论的表大小约为 40m 行)?

When iterating through a Resultset in the standard way

while (rs.next()){
...
}

To retrieve column data (e.g long) is it quicker to use

rs.getLong(String columnLabel)

or

rs.getLong(int columnIndex)

Presumably columnLabel is better to use in many ways for stronger code, but is there a significant operational loss by matching a column to the string every time (we're talking table sizes of ~40m rows)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人生百味 2024-09-22 00:29:28

我敢打赌,与准备结果集的成本相比,差异可以忽略不计。喜欢健壮的代码。

I'd wager that compared to the cost of preparing the result set the difference is negligable. Favour the robust code.

扶醉桌前 2024-09-22 00:29:28

您可以两全其美!使用索引的速度以及使用列名的可维护性和安全性。

首先 - 除非您循环遍历结果集,否则只需使用列名称。

1.定义一组整型变量,每个变量对应您将访问的每一列。变量的名称可以包括列的名称:例如iLast_Name。

2.在结果集循环之前迭代列元数据并将每个整数变量的值设置为相应列名的列索引。如果“Last_Name”列的索引为 3,则将“iLast_Name”的值设置为 3。

3.在结果集循环中,使用 GET/SET 方法中的整型变量名称。变量名称是开发人员/维护人员关于正在访问的实际列名称的视觉线索,但值是列索引,将提供最佳性能。

注意:初始映射(即列名到索引映射)仅在循环之前完成一次,而不是针对循环中的每个记录和列。

You can have the best of both! The speed of using indexes with the maintainability and security of using column names.

First - unless you are looping thru a result set just use column names.

1.Define a set of integer variables, one for each column you will access. The names of the variables can include the name of the column: e.g. iLast_Name.

2.Before the result set loop iterate thru the column metadata and set the value of each integer variable to the column index of the corresponding column name. If the index of the 'Last_Name' column is 3 then set the value of 'iLast_Name' to 3.

3.In the result set loop use the integer variable names in the GET/SET methods. The variable name is a visual clue to the developer/maintainer as to the actual column name being accessed but the value is the column index and will give the best performance.

NOTE: the initial mapping (i.e. column name to index mapping) is only done once before the loop rather than for every record and column in the loop.

宁愿没拥抱 2024-09-22 00:29:28

我建议你更多地考虑什么是更清晰的,而不是什么是更快的。

这是一个例子:
假设您有一个查询,select * from ....,当您迭代 ResultSet 时,您将调用 String a = resultSet.getString(1), < code>int i = resultSet.getInt(2) 等等

然后你更改你的表定义,以便你有更多的列......突然之间你的所有获取都会中断,因为你通过 int 引用了所有内容。如果您按列名称引用,则不会有中断。

对我来说,这似乎比按列号引用要清楚得多。当您稍后尝试调试旧代码以找出其损坏原因时,您还可以节省大量时间。

I'd suggest that you think more about what's clearer than what's quicker.

Here's an example:
Say you have a query, select * from .... and when you iterate over the ResultSet, you're calling String a = resultSet.getString(1), int i = resultSet.getInt(2) etc etc

Then you change your table definition so that you have some more columns... all of a sudden all your gets are going to break because you referenced everything by int. If you'd referenced by column name, then there would be no break.

To me this seems a lot clearer than referencing by column number. You'll also save a lot of time later on when trying to debug your old code to figure out why it's breaking.

强者自强 2024-09-22 00:29:28

完全取决于内部结构。如果他们操作任何类型的缓存,那就没什么大不了的。如果他们使用哈希图,那么它就不太可能是实质性的。一个专为支持这一点而设计的系统不会有太大的困难。但是,您应该真正考虑什么在您的代码上下文中更有意义,如果配置文件显示出性能问题,请返回到此。

Depends entirely on the internal structure. If they operate any sort of cache, it won't be a big deal. If they use a hashmap, it's unlikely to be substantial. A system engineered to support this won't have too much trouble coping. However, you should really think about what makes more sense in the context of your code, and come back to this if a profile demonstrates performance problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文