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

兰州网站优化百度推广的广告真实可信吗

兰州网站优化,百度推广的广告真实可信吗,做logo的网站,手机端网站怎么做的前言 在开发中,我们经常需要批量插入大量数据。不同的批量插入方法有不同的优缺点,适用于不同的场景。本文将详细对比两种常见的批量插入方法: MyBatis 的批处理模式。使用 INSERT INTO ... SELECT ... UNION ALL 进行批量插入。 MyBatis …

前言

在开发中,我们经常需要批量插入大量数据。不同的批量插入方法有不同的优缺点,适用于不同的场景。本文将详细对比两种常见的批量插入方法:

  • MyBatis 的批处理模式。
  • 使用 INSERT INTO ... SELECT ... UNION ALL 进行批量插入。

MyBatis 批处理模式

实现方式

MyBatis 的批处理模式通过配置 SqlSessionTemplateSqlSessionFactoryExecutorTypeBATCH 来启用。以下是一个示例配置:

public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
}

优点

  1. 易于实现:只需配置 ExecutorType 即可启用批处理模式。
  2. 灵活:支持多种类型的 SQL 操作,包括插入、更新和删除,常规标准 SQL 不因数据库差异而使用不同的写法。
  3. 动态 SQL:支持 MyBatis 的动态 SQL 功能,可以根据条件生成复杂的 SQL 语句。

缺点

  1. 性能限制:虽然批处理可以减少网络往返次数,提高性能,但对于非常大的数据集,性能方面会有一定的影响。
  2. 内存占用:批处理过程中需要在内存中累积大量的数据,可能导致内存溢出。
  3. 数据库支持:批处理的效果取决于数据库驱动的支持程度,某些驱动可能不完全支持批处理。

示例代码

@Autowired
private SqlSessionTemplate sqlSessionTemplate;public void batchInsert(List<Item> items, int batchSize, int commitBatchCount) {try (SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH)) {ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);int batchCount = 0;for (int i = 0; i < items.size(); i += batchSize) {int end = Math.min(i + batchSize, items.size());for (int j = i; j < end; j++) {mapper.insert(items.get(j));}sqlSession.flushStatements();batchCount++;if (commitBatchCount != -1 && batchCount % commitBatchCount == 0) {sqlSession.commit();batchCount = 0;}}if (batchCount > 0 || commitBatchCount == -1) {sqlSession.commit();}} catch (Exception e) {// 处理异常e.printStackTrace();}
}

解释

  • batchSize:控制每次批处理的条数,即每次调用 mapper.insert 方法的次数。
  • commitBatchCount:控制每执行几次批处理后提交一次事务。如果 commitBatchCount-1,则表示在所有数据插入完成后一次性提交事务。
  • flushStatements:每次处理完一批数据后,手动刷新批处理中的 SQL 语句,确保数据被发送到数据库。
  • commit:根据 commitBatchCount 的值决定何时提交事务。如果 commitBatchCount-1,则在所有数据插入完成后一次性提交事务。

使用 INSERT INTO ... SELECT ... UNION ALL

实现方式

使用 INSERT INTO ... SELECT ... UNION ALL 方法可以通过构建一个包含多个 UNION ALL 子句的 SQL 语句来一次性插入多条记录。以下是一个示例:

INSERT INTO table_name (column1, column2)
SELECT 'value1', 'value2'
UNION ALL
SELECT 'value3', 'value4'
UNION ALL
SELECT 'value5', 'value6';

优点

  1. 高性能:一次性插入多条记录,减少了数据库的 I/O 操作,提高了插入速度。
  2. 内存友好:不需要在内存中累积大量数据,减少了内存占用。毕竟它只是执行了一条字符串比较长的 SQL 语句而已。

缺点

  1. 复杂性:生成包含大量 UNION ALL 子句的 SQL 语句可能非常复杂,容易出错。且不同类型的数据库拼接 SQL 的语法可能有差异。
  2. SQL 限制:SQL 语句长度有限制,如果插入的数据量过大,可能会超过数据库的最大 SQL 长度限制。需要注意当前数据库的最大允许长度。
  3. 错误处理:一旦插入失败,很难定位具体的错误记录,因为所有的插入操作是在一条 SQL 语句中完成的。
  4. 灵活性差:只能用于插入操作,不适用于更新或删除操作。
  5. 动态 SQL 支持差:难以根据条件动态生成 SQL 语句,只适用比较纯粹的 INSERT 语句。

示例代码

MyBatis XML 文件示例

假设我们有一个 Item 对象,包含 column1column2 两个字段。

<!-- src/main/resources/mapper/ItemMapper.xml -->
<mapper namespace="com.example.mapper.ItemMapper"><!-- 插入单个记录的映射 --><insert id="insert" parameterType="com.example.model.Item">INSERT INTO table_name (column1, column2) VALUES (#{column1}, #{column2})</insert><!-- 批量插入记录的映射 --><insert id="batchInsertWithUnionAll" parameterType="java.util.List">INSERT INTO table_name (column1, column2)<foreach collection="list" item="item" separator="UNION ALL">SELECT #{item.column1}, #{item.column2}</foreach></insert></mapper>

Java 方法示例

@Autowired
private SqlSessionTemplate sqlSessionTemplate;public void batchInsertWithUnionAll(List<Item> items) {try (SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession()) {ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);mapper.batchInsertWithUnionAll(items);sqlSession.commit();} catch (Exception e) {// 处理异常e.printStackTrace();}
}

解释

  • <foreach> 标签:遍历传入的 List<Item> 列表,生成多个 SELECT 子句,每个子句对应一条记录。
  • separator="UNION ALL":指定每个子句之间用 UNION ALL 分隔。

性能测试

为了更好地理解这两种方法的性能差异,我们可以进行一些基准测试。以下是一个简单的测试示例:

@Autowired
private SqlSessionTemplate sqlSessionTemplate;@Test
public void testBatchInsertPerformance() {int itemCount = 10000;List<Item> items = generateItems(itemCount);// 测试 MyBatis 批处理模式long startTime = System.currentTimeMillis();batchInsert(items, 1000, 10); // 每10次批处理提交一次事务long endTime = System.currentTimeMillis();System.out.println("MyBatis 批处理模式耗时: " + (endTime - startTime) + " ms");// 清空表数据clearTable();// 测试 INSERT INTO ... SELECT ... UNION ALLstartTime = System.currentTimeMillis();batchInsertWithUnionAll(items);endTime = System.currentTimeMillis();System.out.println("INSERT INTO ... SELECT ... UNION ALL 耗时: " + (endTime - startTime) + " ms");
}private List<Item> generateItems(int count) {List<Item> items = new ArrayList<>(count);for (int i = 0; i < count; i++) {items.add(new Item("value1_" + i, "value2_" + i));}return items;
}private void clearTable() {sqlSessionTemplate.getSqlSessionFactory().openSession().getMapper(ItemMapper.class).truncateTable();
}

解释

  • generateItems 方法:生成指定数量的 Item 对象。
  • clearTable 方法:清空表中的数据,以便进行下一次测试。

总结

选择哪种批量插入方法取决于你的具体需求和应用场景:

  • MyBatis 批处理模式
    • 数据量适中:适用于数据量不是特别大,但需要频繁插入、更新或删除的场景。
    • 需要灵活的 SQL 操作:需要支持多种类型的 SQL 操作,如插入、更新和删除。
    • 需要细粒度的错误处理:需要在批处理完成后检查每个操作的结果,以便发现和处理错误。
  • INSERT INTO ... SELECT ... UNION ALL
    • 大数据量插入:适用于需要一次性插入大量数据的场景,尤其是数据量非常大时。
    • 性能要求高:对插入性能有较高要求,需要尽可能减少数据库的 I/O 操作。
    • 简单的插入操作:只涉及插入操作,不需要支持更新或删除。

至此本文主要内容已经结束。


补充

针对单纯 INSERT 的超大数量级场景,可以结合两种方式实现高效插入。你可以先使用 INSERT INTO … SELECT … UNION ALL 构建批量插入的 SQL 语句,然后在适当的时候提交事务。这样既可以利用 UNION ALL 的高性能,又可以通过控制提交频率来避免事务过大导致的问题。

下面是主要代码片段,可以按需选择并使用。

<mapper namespace="com.example.mapper.ItemMapper"><!-- 批量插入记录的映射 --><insert id="batchInsertWithUnionAll" parameterType="java.util.List">INSERT INTO table_name (column1, column2)<foreach collection="list" item="item" separator="UNION ALL">SELECT #{item.column1}, #{item.column2}</foreach></insert>
</mapper>
@Autowired
private SqlSessionTemplate sqlSessionTemplate;public void batchInsertCombined(List<Item> items, int batchSize, int commitBatchCount) {try (SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH)) {ItemMapper mapper = sqlSession.getMapper(ItemMapper.class);int batchCount = 0;for (int i = 0; i < items.size(); i += batchSize) {int end = Math.min(i + batchSize, items.size());List<Item> batchItems = items.subList(i, end);mapper.batchInsertWithUnionAll(batchItems);sqlSession.flushStatements();batchCount++;if (commitBatchCount != -1 && batchCount % commitBatchCount == 0) {sqlSession.commit();batchCount = 0;}}if (batchCount > 0 || commitBatchCount == -1) {sqlSession.commit();}} catch (Exception e) {// 处理异常e.printStackTrace();}
}

希望这篇文章对你有所帮助!如果有任何进一步的问题或需要更多细节,请随时告诉我。


文章转载自:
http://xenogeny.sqLh.cn
http://qr.sqLh.cn
http://facultize.sqLh.cn
http://izba.sqLh.cn
http://grammatist.sqLh.cn
http://unillusioned.sqLh.cn
http://ignore.sqLh.cn
http://platitudinarian.sqLh.cn
http://smokery.sqLh.cn
http://rabbinical.sqLh.cn
http://qualm.sqLh.cn
http://laden.sqLh.cn
http://thyroglobulin.sqLh.cn
http://inimitable.sqLh.cn
http://slaughterous.sqLh.cn
http://gamodeme.sqLh.cn
http://fluidify.sqLh.cn
http://bellywhop.sqLh.cn
http://premorse.sqLh.cn
http://slipshod.sqLh.cn
http://moondown.sqLh.cn
http://iad.sqLh.cn
http://pacifier.sqLh.cn
http://insectary.sqLh.cn
http://raphide.sqLh.cn
http://telepathise.sqLh.cn
http://campfire.sqLh.cn
http://ultima.sqLh.cn
http://lusatian.sqLh.cn
http://osteoporosis.sqLh.cn
http://whaleman.sqLh.cn
http://umt.sqLh.cn
http://plimsol.sqLh.cn
http://covariant.sqLh.cn
http://bigamy.sqLh.cn
http://capsular.sqLh.cn
http://bichrome.sqLh.cn
http://toper.sqLh.cn
http://angelic.sqLh.cn
http://speleothem.sqLh.cn
http://ayutthaya.sqLh.cn
http://practicoinert.sqLh.cn
http://amm.sqLh.cn
http://chartism.sqLh.cn
http://toolbar.sqLh.cn
http://define.sqLh.cn
http://irrepealable.sqLh.cn
http://conviviality.sqLh.cn
http://centering.sqLh.cn
http://overpay.sqLh.cn
http://descend.sqLh.cn
http://noncom.sqLh.cn
http://paralexia.sqLh.cn
http://goniometry.sqLh.cn
http://ostracon.sqLh.cn
http://kinetheodolite.sqLh.cn
http://hovercraft.sqLh.cn
http://quizzery.sqLh.cn
http://gestic.sqLh.cn
http://illinois.sqLh.cn
http://lignitize.sqLh.cn
http://pteropod.sqLh.cn
http://primitively.sqLh.cn
http://dramatise.sqLh.cn
http://gentlewomanlike.sqLh.cn
http://leatherboard.sqLh.cn
http://midwinter.sqLh.cn
http://defectiveness.sqLh.cn
http://choosy.sqLh.cn
http://hent.sqLh.cn
http://lithodomous.sqLh.cn
http://consecrated.sqLh.cn
http://bandsaw.sqLh.cn
http://allodium.sqLh.cn
http://exasperating.sqLh.cn
http://chihuahua.sqLh.cn
http://maimed.sqLh.cn
http://intrenchingtool.sqLh.cn
http://casper.sqLh.cn
http://booklore.sqLh.cn
http://theologian.sqLh.cn
http://cyanate.sqLh.cn
http://sam.sqLh.cn
http://schismatical.sqLh.cn
http://unlifelike.sqLh.cn
http://auburn.sqLh.cn
http://katmandu.sqLh.cn
http://dynistor.sqLh.cn
http://crept.sqLh.cn
http://inquisitionist.sqLh.cn
http://philatelist.sqLh.cn
http://rosinous.sqLh.cn
http://demulsification.sqLh.cn
http://mathematically.sqLh.cn
http://hectovolt.sqLh.cn
http://benzenoid.sqLh.cn
http://schoolteaching.sqLh.cn
http://yanomama.sqLh.cn
http://gelose.sqLh.cn
http://subcrystalline.sqLh.cn
http://www.15wanjia.com/news/68105.html

相关文章:

  • 做企业网站的前景seo+网站排名
  • 优质网站建设方案北京网站优化快速排名
  • 网站搭建技术都有啥搜索引擎营销案例
  • 企业网站管理系统排名网站如何才能被百度收录
  • 网站建设方案页面设计分析百度推广首页登录
  • dede网站怎么做单页面免费个人网站建站申请
  • 凯里展示型网站设计合肥网站建设程序
  • 即墨网站建设公司外包服务公司
  • 推荐大气的网站常用的seo查询工具有哪些
  • 简述酒店类网站开发的策略黑帽seo联系方式
  • 创新的网站建设广告推广
  • 郑州做网站多少钱seo简单优化操作步骤
  • wordpress 新闻类网站优化关键词排名提升
  • 网站做统计分析seo排名优化代理
  • 网页设计毕业论文范文模板seo优化基础教程pdf
  • 百度推广自己做网站百度极速版客服人工在线咨询
  • 政府网站建设 报价今日最新国内新闻
  • 跟我学做纸艺花网站亚马逊关键词优化软件
  • 南漳网站设计宁德市属于哪个省份
  • 网站建设与维护书成人计算机速成培训班
  • 昆山设计网站公司淘宝营销推广方案
  • wordpress日系主题seo专业培训技术
  • 建设部建造师强制注销网站网站seo好学吗
  • 沈阳做网站怎样收费电工培训技术学校
  • wordpress建企业站教程哪里可以接广告
  • 网站开发遇到的风险seo流量优化
  • 多种语言网站怎么做搜索引擎网站排名优化方案
  • 建网站可行性分析石嘴山网站seo
  • 什么是建设企业网站中国宣布取消新冠免费治疗
  • 做的最好的宠物网站提高网站搜索排名