首页 > 其他分享 >分片+异步 优化in子查询

分片+异步 优化in子查询

时间:2023-09-02 12:47:33浏览次数:27  
标签:异步 int userIds 查询 add 分片 size

将一次性查询改为通过分片、异步优化 in子查询

集合数据分片是将数据按指定大小进行分组,像java中使用guava或hutool工具的partition进行分组,然后分批处理或者获取数据,in子查询会通过创建临时表,不易将大量数据放入in子查询中

案例:分片+异步操作

@Test
void test32() {

    ExecutorService orderExecutorService = Executors.newFixedThreadPool(10);

    List<Integer> userIds = new CopyOnWriteArrayList<>();
    userIds.add(1);
    userIds.add(2);
    userIds.add(3);
    userIds.add(4);
    userIds.add(5);
    List<List<Integer>> partitionUserIds = ListUtil.partition(userIds, 2);

    final List<Object> allOrders = new ArrayList<>(partitionUserIds.size());

    // 分片+异步
    partitionUserIds.stream().map(item -> {
        return CompletableFuture.runAsync(() -> {
            // 查询数据库并添加到集合中
            ArrayList<Object> data = new ArrayList<>();
            allOrders.addAll(data);
        }, orderExecutorService);
    }).forEach(CompletableFuture::join);

    // 执行业务操作
    allOrders.forEach(System.out::println);

}

分片实现原理

/**
 * 集合分片处理
 *
 * @param datas 需要分片的集合
 * @param limit 一个分片大小
 * @param <T>
 * @return
 */
public static <T> List<List<T>> toPart(List<T> datas, int limit) {

    if (datas == null || datas.size() == 0) {
        throw new RuntimeException("数据不能为空");
    }
    int size = datas.size();
    if (size <= limit) {
        return Collections.singletonList(datas);
    }

    List<List<T>> partsList = new ArrayList<>();
    // 已经查询的条数
    int passIndex = 0;
    do {
        int endIndex = Math.min(passIndex + limit, size);
        // 注意subList是前闭后开,开始索引与结束索引不能一样
        List<T> currPartList = datas.subList(passIndex, endIndex);
        partsList.add(currPartList);
        passIndex = endIndex;
    } while (passIndex < size);

    return partsList;
}

@Test
public void test23() {
    List<Integer> nums = new ArrayList<>(5);
    for (int i = 0; i < 5; i++) {
        nums.add(i);
    }
    toPart(nums, 3).forEach(System.out::println);
}

自动分页查询

@Test
public void selectBatch() {
    // 1.查询总条数
    int total = 5;

    // 每次查询条数
    int limit = 2;
    // 已经查询的条数
    int offset = 0;
    do {
        System.out.println("select * from a limit " + offset + "," + limit);
        offset = limit + offset;
    } while (offset <= total);
}

标签:异步,int,userIds,查询,add,分片,size
From: https://www.cnblogs.com/party-abu/p/17673541.html

相关文章

  • Seurat Tutorial 4:映射和注释查询数据集
    写在前面学习一个软件最好的方法就是啃它的官方文档。本着自己学习、分享他人的态度,分享官方文档的中文教程。软件可能随时更新,建议配合官方文档一起阅读。推荐先按顺序阅读往期内容:文献篇:1.文献阅读:(SeuratV1)单细胞基因表达数据的空间重建2.文献阅读:(SeuratV2)整合跨......
  • MongoDB系列之分片管理
    1.查看当前状态1.1查看配置信息mongos>useconfig//查看分片mongos>db.shards.find(){"_id":"study","host":"study/localhost:27018,localhost:27019,localhost:27020","state":1}//查看分片集合信息mongos>db.collecti......
  • ORA-01031: insufficient privileges 19c跨用户视图查询 with grant option
    问题概述某客户从11.2.0.1升级迁移到19.16版本后,应用反应部分查询功能无法使用报无权限,数据库中增加了ORA-01031:insufficientprivileges跟踪event进行分析问题复现创建三个测试用户createusertest1identifiedbyoracle123;createusertest2identifiedbyoracle123;......
  • openGauss学习笔记-57 openGauss 高级特性-并行查询
    openGauss学习笔记-57openGauss高级特性-并行查询openGauss的SMP并行技术是一种利用计算机多核CPU架构来实现多线程并行计算,以充分利用CPU资源来提高查询性能的技术。在复杂查询场景中,单个查询的执行较长,系统并发度低,通过SMP并行执行技术实现算子级的并行,能够有效减少查询执行时......
  • postgresql流复制四(查询冲突)
    部署流复制环境后,备库可提供只读操作,通常会将一些执行时间较长的分析任务、统计SQL跑在备库上,从而减轻主库压力,在备库上执行一些长时间SQL时,可能会出现以下错误并被中止:FATAL:terminatingconnectionduetoconflictwithrecoveryDETAIL:Userwasholdingarelation......
  • C# 异步执行操作
    为了方便测试异步,先加个计时计时相关(可以直接跳过该部分)//开始计时Stopwatchstopwatch=newStopwatch();stopwatch.Start();//停止计时stopwatch.Stop();//输出计时毫秒数stopwatch.ElapsedMilliseconds阻塞延迟,下面两个分别表示阻塞三秒钟同步阻塞:Thr......
  • 5分钟带你回顾大文件分片以及异步计算hash的步骤
    背景  文件上传功能在中后台项目中是最常见的功能,分片上传是一种将大文件分割成多个小片段进行上传的技术,可以有效提高文件上传的速度和稳定性。思路  1.首先就是使用File.slice对文件进行分割产出一个数组用于存储每个小的chunk片段  2.异步计算hash值,可用作标识文......
  • winform,c#左链接查询两张表或多张表,数据库正常,但是发现查出来的同一条记录变成了好几
    这个样子就是犯了笛卡尔积,我有两张表那我自己项目来说一下吧:a表的内容如下: b表的内容如下: 到这里,你会发现,又五六个字段内容是一模一样的,该字段两张表都用,那么查询出来数据翻倍就很好解决了在where后面加上a.字段1=b.字段1and......anda.字段n=b.字段n就可以了......
  • Excel函数查询-----------------------------------filter函数(可以代替vlookup函数)
     求a=FILTER(B2:G8,A2:A8=I11,"")  先选定几列,然后在第一行去输入函数,再拉取就可以了......
  • 查看es结构,es _search查询基础语法
    查看es结构,es_search查询基础语法http://xx.xx.xx.xx:9200/ ES地址car_info/_searchPOST{}POST{"query":{"match":{"carNo":"573702440"}}}{ "query":{"term":{......