在 SQL 或 CODE 中更好地对聚合数据进行分组(就性能而言)
我想征求关于通过连接字符串来生成聚合数据的意见。如果我有一个列聚合,但我想在聚合列中连接,哪个在性能方面更快?执行一条 SQL,然后在代码中聚合。或者选择主要数据然后一次查询一个。
例如:
TABLE_A
COL_A_1 COL_A_2
A a
B b
C c
TABLE_B
COL_B_1 COL_B_2
a Text1
a Text2
a Text3
b Text4
b Text5
表在 COL_A_2 = COL_B_1 处连接,聚合列为 COL_B_2。
选项1(执行一个SQL)
SELECT TABLE_1.COL_A_1, TABLE_1.COL_A_2, TABLE_2.COL_B_2
FROM TABLE_A
LEFT OUTER JOIN TABLE_B ON TABLE_A.COL_A_2 = TABLE_2.COL_B_1
ORDER BY TABLE_1.COL_A_1
然后在代码中,仅循环结果集并聚合COL_B_1。 (例如使用Java)
String oldColA1 = "";
InfoEntity currInfo = null;
for (InfoEntity info : infoList) {
if (!oldColA1.equals(info.colA1)) {
currInfo = info;
}
if (currInfo.colB2 == null || currInfo.colB2.equals("")) {
currInfo.colB2 = info.colB2;
} else {
currInfo.colB2 += info.colB2;
}
oldColA1 = info.colA1;
}
选项2(执行多个SQL)
SELECT TABLE_1.COL_A_1, TABLE_1.COL_A_2
FROM TABLE_A
ORDER BY TABLE_1.COL_A_1
然后在代码中选择每个(例如Java)
for (InfoEntity info : infoList) {
// Select TableB entity based on info.colA2
...
tableBList = dao.selectTableB(info.colA2);
...
for (TableBEntity b : tableBList) {
info.colB2 += b.colB2;
}
}
我通常执行选项1,因为我认为进行多个SQL选择可能成本很高。但我对此并不确定。但是还有其他方法可以进行这种聚合吗?我已经在网上搜索过,在标准 SQL 中没有字符串连接聚合。另外,TableA 上的说明过于简化,通常 TableA 是多个表连接在一起的复杂查询,TableB 也是如此。
I would like to ask for opinion on making aggregate data by concatenating strings. If I have a column aggregate but I want to concatenate then in an aggregate column, which is faster in terms of performance? Doing one SQL then just aggregating then in the CODE. Or selecting the main data then querying one at a time.
For Example:
TABLE_A
COL_A_1 COL_A_2
A a
B b
C c
TABLE_B
COL_B_1 COL_B_2
a Text1
a Text2
a Text3
b Text4
b Text5
The table is joined at COL_A_2 = COL_B_1, the aggregate column is COL_B_2.
Option 1 (Doing one SQL)
SELECT TABLE_1.COL_A_1, TABLE_1.COL_A_2, TABLE_2.COL_B_2
FROM TABLE_A
LEFT OUTER JOIN TABLE_B ON TABLE_A.COL_A_2 = TABLE_2.COL_B_1
ORDER BY TABLE_1.COL_A_1
Then in the code, just loop for the result set and aggregate the COL_B_1. (ex using Java)
String oldColA1 = "";
InfoEntity currInfo = null;
for (InfoEntity info : infoList) {
if (!oldColA1.equals(info.colA1)) {
currInfo = info;
}
if (currInfo.colB2 == null || currInfo.colB2.equals("")) {
currInfo.colB2 = info.colB2;
} else {
currInfo.colB2 += info.colB2;
}
oldColA1 = info.colA1;
}
Option 2 (Doing several SQL)
SELECT TABLE_1.COL_A_1, TABLE_1.COL_A_2
FROM TABLE_A
ORDER BY TABLE_1.COL_A_1
Then select for each in code (Ex Java)
for (InfoEntity info : infoList) {
// Select TableB entity based on info.colA2
...
tableBList = dao.selectTableB(info.colA2);
...
for (TableBEntity b : tableBList) {
info.colB2 += b.colB2;
}
}
I usually do option 1, because I think making multiple SQL select maybe costly. But I am not sure about this. But is there another way on doing this kind of aggregation. I already search the net and in standard SQL there is no string concatenation aggregation. Also, the illustration on TableA is over simplified, usually TableA is a complex query of multiple table joined together and so is TableB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的——数据库的访问应该尽可能少。
不过,组串联是可能的 - MySQL 有 GROUP_CONCAT,自 4.1 起。对于 SQL Server,您可以使用:
检查此链接了解一个应该在 Oracle 9i+ 中运行的示例。
You're correct - trips to the database should be as few as possible.
Group concatenation is possible though - MySQL has GROUP_CONCAT, since 4.1. With SQL Server, you can use:
Check this link for an example that should work in Oracle 9i+.