之因此要測該場景,是由於merge多數據源結果的時候,有時候只是單個子查詢結果了,而此時採用sql數據庫處理並不必定可以合理(網絡延遲太大)。html
測試數據10萬行,結果1000行java
limit 20 offset 0的延時以下:sql
package com.hundsun.ta.base.service; import com.hundsun.ta.utils.JsonUtils; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; import static java.util.stream.Collectors.*; /** * @author zjhua * @description * @date 2019/10/3 15:35 */ public class JavaStreamCommonSQLTest { public static void main(String[] args) { List<Person> persons = new ArrayList<>(); for (int i=100000;i>0;i--) { persons.add(new Person("Person " + (i+1)%1000, i % 100, i % 1000,new BigDecimal(i),i)); } System.out.println(System.currentTimeMillis()); Map<String,Map<Integer, Data>> result = persons.stream().collect( groupingBy(Person::getName,Collectors.groupingBy(Person::getAge, collectingAndThen(summarizingDouble(Person::getQuantity), dss -> new Data((long)dss.getAverage(), (long)dss.getSum()))))); List<ResultGroup> list = new ArrayList<>(); result.forEach((k,v)->{ v.forEach((ik,iv)->{ ResultGroup e = new ResultGroup(k,ik,iv.average,iv.sum); list.add(e); }); }); list.sort(Comparator.comparing(ResultGroup::getSum).thenComparing(ResultGroup::getAverage)); list.subList(0,20); System.out.println(System.currentTimeMillis()); System.out.println(JsonUtils.toJson(list)); } } @lombok.Data@NoArgsConstructor@AllArgsConstructor class Person { String name; int group; int age; BigDecimal balance; double quantity; } @lombok.Data@NoArgsConstructor@AllArgsConstructor @Deprecated class ResultGroup { String name; int group; long average; long sum; } class Data { long average; long sum; public Data(long average, long sum) { this.average = average; this.sum = sum; } }
開始:1570093479002
結束:1570093479235 --200多毫秒數據庫
測試數據10萬行,結果90000行網絡
limit 20 offset 10000的延時以下:性能
package com.hundsun.ta.base.service; import com.hundsun.ta.utils.JsonUtils; import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; import static java.util.stream.Collectors.*; /** * @author zjhua * @description * @date 2019/10/3 15:35 */ public class JavaStreamCommonSQLTest { public static void main(String[] args) { List<Person> persons = new ArrayList<>(); for (int i=100000;i>0;i--) { persons.add(new Person("Person " + (i+1)%1000, i>90000 ? i%10000:i, i % 1000,new BigDecimal(i),i)); } System.out.println(System.currentTimeMillis()); Map<String,Map<Integer, Data>> result = persons.stream().collect( groupingBy(Person::getName,Collectors.groupingBy(Person::getGroup, collectingAndThen(summarizingDouble(Person::getQuantity), dss -> new Data((long)dss.getAverage(), (long)dss.getSum()))))); List<ResultGroup> list = new ArrayList<>(); result.forEach((k,v)->{ v.forEach((ik,iv)->{ ResultGroup e = new ResultGroup(k,ik,iv.average,iv.sum); list.add(e); }); }); list.sort(Comparator.comparing(ResultGroup::getSum).thenComparing(ResultGroup::getAverage)); System.out.println(list.size()); list.subList(10000,10020); System.out.println(System.currentTimeMillis()); System.out.println(JsonUtils.toJson(list)); } } @lombok.Data@NoArgsConstructor@AllArgsConstructor class Person { String name; int group; int age; BigDecimal balance; double quantity; } @lombok.Data@NoArgsConstructor@AllArgsConstructor @Deprecated class ResultGroup { String name; int group; long average; long sum; } class Data { long average; long sum; public Data(long average, long sum) { this.average = average; this.sum = sum; } }
開始:1570093823404測試
結束:1570093823758 -- 350多毫秒this
總的來講,到如今爲止,java stream還沒法較低成本的直接替換sql,好比典型的group by 多個字段不支持,須要多級map(不只複雜,性能也低),並且group by的統計i結果還必須在單獨的類中。開發成本就過高。spa
https://www.cnblogs.com/kuanglongblogs/p/11230250.htmlcode
參考:https://stackoverflow.com/questions/32071726/java-8-stream-groupingby-with-multiple-collectors