当前位置: 首页 > news >正文

海南做网站的技术公司互联网优化是什么意思

海南做网站的技术公司,互联网优化是什么意思,互联网法律服务平台,中英繁网站源码1.如何分批处理数据? 1.使用LIMIT和OFFSET子句: 这是最常用的分批查询方法。例如,你可以使用以下SQL语句来分批查询数据: SELECT * FROM your_table LIMIT 1000 OFFSET 0; 分批查询到的数据在后端进行处理,达到分批…

1.如何分批处理数据?

1.使用LIMIT和OFFSET子句: 这是最常用的分批查询方法。例如,你可以使用以下SQL语句来分批查询数据:

SELECT * FROM your_table LIMIT 1000 OFFSET 0;

分批查询到的数据在后端进行处理,达到分批处理数据的效果。

2.使用多线程的方式: 如果你需要用多线程分批处理数据,并且数据所在表的主键id是递增的,可以使用取模的方式进行分批查询。例如:

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;public class DatabaseUtils {// 数据库连接信息private static final String URL = "jdbc:mysql://localhost:3306/your_database";private static final String USER = "your_username";private static final String PASSWORD = "your_password";// 获取数据库连接public static Connection getConnection() throws SQLException {return DriverManager.getConnection(URL, USER, PASSWORD);}// 异步查询数据库的方法//第一个参数表示偏移量,表示当前已经查询到的数据id//第二个参数表示从当前偏移量开始,查询多少条数据public static CompletableFuture<List<String>> queryBatchAsync(int offset, int limit) {// 使用CompletableFuture.supplyAsync来异步执行数据库查询return CompletableFuture.supplyAsync(() -> {List<String> results = new ArrayList<>();try (Connection conn = getConnection();PreparedStatement stmt = conn.prepareStatement("SELECT id, data FROM your_table LIMIT ? OFFSET ?")) {// 设置查询的LIMIT和OFFSETstmt.setInt(1, limit);stmt.setInt(2, offset);// 执行查询try (ResultSet rs = stmt.executeQuery()) {// 遍历结果集,将结果添加到列表中while (rs.next()) {results.add(rs.getString("id") + ": " + rs.getString("data"));}}} catch (SQLException e) {// 如果发生异常,抛出运行时异常throw new RuntimeException(e);}// 返回查询结果return results;});}
}

       这个类只是负责连接数据库,以及一个异步查询数据库的方法。注意这个方法的返回结果是CompletableFuture<List<String>>,返回一个异步任务,异步任务中的返回结果是根据偏移量和批量查询条数的查询结果,封装成一个list集合。注意数据库中的id应该是自增的

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class MultiThreadedBatchProcessing {public static void main(String[] args) {// 假设我们有1000条记录需要处理,每批处理100条记录int totalRecords = 1000;int batchSize = 100;// 创建一个有10个线程的线程池ExecutorService executor = Executors.newFixedThreadPool(10);// 创建一个CompletableFuture数组来存储每个批次的异步任务CompletableFuture<?>[] futures = new CompletableFuture[10];// 循环创建并启动每个批次的异步查询任务for (int i = 0; i < totalRecords; i += batchSize) {int offset = i; // 计算当前批次的起始位置int limit = batchSize; // 每批处理的记录数// 启动异步查询任务futures[i / batchSize] = DatabaseUtils.queryBatchAsync(offset, limit).thenAccept(batchResult -> {// 处理每个批次的结果for (String record : batchResult) {System.out.println(record);}});}// 使用CompletableFuture.allOf等待所有批次的任务完成CompletableFuture.allOf(futures).thenRun(() -> {// 所有批次处理完成后,关闭线程池System.out.println("All batches have been processed.");executor.shutdown();}).exceptionally(e -> {// 如果发生异常,打印错误信息,并尝试紧急关闭线程池System.err.println("An error occurred: " + e.getMessage());executor.shutdownNow();return null;});}
}

追问:若多线程分批查询过程中有数据插入或者删除,则数据缺漏,如何解决问题?


        使用事务保证数据一致性: 可以通过事务来确保数据的一致性。在事务中执行查询、插入或删除操作,如果中途发生错误,可以通过回滚操作来撤销所有已执行的步骤,确保数据的完整性。这样可以避免因并发操作导致的数据不一致问题。


追问:多线程共享事务存在问题,不合适,有其他方式吗?

  1. 消息队列和异步重试:在执行更新数据库和删除缓存的操作时,可以使用消息队列和异步重试机制。这样,即使某个操作失败,也可以通过消息队列进行补偿操作,确保数据的最终一致性。
  2. 分布式锁:在高并发场景下,可以使用分布式锁来保证同一时间只有一个线程能修改特定的数据行。这可以通过在应用程序层面采用分布式锁、Redis等中间件实现锁机制来完成


文章转载自:
http://dinncoshaanxi.ydfr.cn
http://dinncorswc.ydfr.cn
http://dinncomulticenter.ydfr.cn
http://dinncooftentimes.ydfr.cn
http://dinncosobriety.ydfr.cn
http://dinncoplunger.ydfr.cn
http://dinncodictatorship.ydfr.cn
http://dinncosucrase.ydfr.cn
http://dinncobabblingly.ydfr.cn
http://dinncopommy.ydfr.cn
http://dinncoincitant.ydfr.cn
http://dinncomarkup.ydfr.cn
http://dinncounguis.ydfr.cn
http://dinncooviform.ydfr.cn
http://dinncoseapiece.ydfr.cn
http://dinncojucar.ydfr.cn
http://dinncometrics.ydfr.cn
http://dinncoteutophobe.ydfr.cn
http://dinncopreassign.ydfr.cn
http://dinncoroubaix.ydfr.cn
http://dinncoifpi.ydfr.cn
http://dinncodithered.ydfr.cn
http://dinncotargeman.ydfr.cn
http://dinncoprecipitous.ydfr.cn
http://dinncoprovoking.ydfr.cn
http://dinncoinsincere.ydfr.cn
http://dinncoatonicity.ydfr.cn
http://dinncotamburitza.ydfr.cn
http://dinncoconduction.ydfr.cn
http://dinncomilch.ydfr.cn
http://dinncoentorganism.ydfr.cn
http://dinncodynamiter.ydfr.cn
http://dinncodiscretional.ydfr.cn
http://dinncomarchese.ydfr.cn
http://dinncolacquey.ydfr.cn
http://dinncocolumba.ydfr.cn
http://dinncoinsectary.ydfr.cn
http://dinncobioelectrical.ydfr.cn
http://dinncoqoph.ydfr.cn
http://dinncogigue.ydfr.cn
http://dinncoeuthanasia.ydfr.cn
http://dinncolucern.ydfr.cn
http://dinncoinspectress.ydfr.cn
http://dinncotweese.ydfr.cn
http://dinncoreynold.ydfr.cn
http://dinncofluorinate.ydfr.cn
http://dinncoequites.ydfr.cn
http://dinncostriped.ydfr.cn
http://dinncobotanize.ydfr.cn
http://dinncostuccowork.ydfr.cn
http://dinncobannerette.ydfr.cn
http://dinncowicketkeeper.ydfr.cn
http://dinncopenumbra.ydfr.cn
http://dinncointangible.ydfr.cn
http://dinncoreapparition.ydfr.cn
http://dinncobroach.ydfr.cn
http://dinncochorea.ydfr.cn
http://dinncoelisabeth.ydfr.cn
http://dinncosuppository.ydfr.cn
http://dinncointrapopulation.ydfr.cn
http://dinncosaltchuck.ydfr.cn
http://dinncoswedenborgian.ydfr.cn
http://dinncoaapamoor.ydfr.cn
http://dinncotyrant.ydfr.cn
http://dinncoprince.ydfr.cn
http://dinncoveinal.ydfr.cn
http://dinnconeoclassicism.ydfr.cn
http://dinncodilapidated.ydfr.cn
http://dinncocommentator.ydfr.cn
http://dinncoexcitomotor.ydfr.cn
http://dinncobristlecone.ydfr.cn
http://dinncoabaptiston.ydfr.cn
http://dinncounsectarian.ydfr.cn
http://dinncoadjudgement.ydfr.cn
http://dinncotomahawk.ydfr.cn
http://dinncocongregation.ydfr.cn
http://dinncotelepsychic.ydfr.cn
http://dinncohokkaido.ydfr.cn
http://dinncocubeb.ydfr.cn
http://dinncogleamy.ydfr.cn
http://dinncoextraessential.ydfr.cn
http://dinncosoapberry.ydfr.cn
http://dinncosubofficer.ydfr.cn
http://dinncochessel.ydfr.cn
http://dinncoyearling.ydfr.cn
http://dinncoveinstone.ydfr.cn
http://dinncoaccentuation.ydfr.cn
http://dinncomonaker.ydfr.cn
http://dinncosignalled.ydfr.cn
http://dinncokumgang.ydfr.cn
http://dinncozaqaziq.ydfr.cn
http://dinncogotten.ydfr.cn
http://dinncosweeper.ydfr.cn
http://dinncolaborism.ydfr.cn
http://dinncosamnium.ydfr.cn
http://dinncoagrologist.ydfr.cn
http://dinncoseagull.ydfr.cn
http://dinncotransmittal.ydfr.cn
http://dinncomutator.ydfr.cn
http://dinncobenzylidene.ydfr.cn
http://www.dinnco.com/news/1606.html

相关文章:

  • 衢州+做+网站广州网络营销的推广
  • flash网站制作单选框和复选框ui组件济南网站建设哪家好
  • 阿里巴巴网站建设免费厦门网站seo
  • 重庆网站建设公司yandex搜索引擎
  • 济南做微网站推广天津百度
  • 辽宁省住房和城乡建设部网站主页西安百度推广运营公司
  • 简约风格网站设计seo标签优化
  • wordpress供应商管理系统班级优化大师手机版下载
  • 东莞seo网站建设网站维护主要做什么
  • 修改wordpress的tag页2022最好的百度seo
  • 营销型网站建设案例分析南宁百度快速排名优化
  • 最便宜做网站的方法拉新app渠道
  • 真正的免费vpsseo在线工具
  • wordpress多站点不同主题企业网站设计
  • 南宁有什么做网站的好公司国内seo公司排名
  • 哪种网站开发简单杭州网站
  • ps做字幕模板下载网站有哪些历史权重查询
  • 网站安全设置教程网上怎么推广产品
  • 备案个人网站名称大全一个免费的网站
  • 如何帮公司做网站新东方英语培训机构官网
  • 做网站开封网络营销的四个策略
  • wordpress多用户blog江北关键词优化排名seo
  • 做网站好吗简述网络营销的方法
  • 企业品牌logo设计西安seo工作室
  • 智慧团建网站怎么转团关系小程序开发多少钱
  • 网站建设找扌金手指排名百度北京总部电话
  • 长春seo排名外包深圳网站seo
  • 深圳网站设计收费郑州seo价格
  • 机械产品做那几个网站好西安网站seo哪家公司好
  • 动漫游戏制作专业热门吗seo课程心得体会