Spring 批量insert 效率問題

背景

  • 使用框架spring mvc
  • dao層用到是的spring jdbc

namedParameterJdbcTemplate batchUpdate 批量

  • 具體代碼以下
public void testBatch(int count){
		String sqlStr ="INSERT INTO C_DSS_BASE_INP_SETTLE (\n" +
				"	ORG_ID,\n" +
				"	PK_HIS,\n" +
				"	SETTLING_DATETIME,\n" +
				"	ORDERED_BY,\n" +
				"	PERFORMED_BY,\n" +
				"	DEPT_DISCHARGE_FROM,\n" +
				"	ITEM_CLASS,\n" +
				"	COSTS,\n" +
				"	CHARGES,\n" +
				"	CREATE_TIME,\n" +
				"	DATA_TIME\n" +
				")\n" +
				"VALUES\n" +
				"	(\n" +
				"		:ORG_ID,\n" +
				"		:PK_HIS,\n" +
				"		:SETTLING_DATETIME,\n"+
				"		:ORDERED_BY,\n" +
				"		:PERFORMED_BY,\n" +
				"		:DEPT_DISCHARGE_FROM,\n" +
				"		:ITEM_CLASS,\n" +
				"		:COSTS,\n" +
				"		:CHARGES,\n" +
				"		:CREATE_TIME,\n" +
				"		:DATA_TIME\n" +
				"	)";
		SqlParameterSource[] params = new MapSqlParameterSource[count];
		for (int i = 0; i < count; i++) {
			MapSqlParameterSource param = new MapSqlParameterSource();
			param.addValue("ORG_ID", "290001");
			param.addValue("PK_HIS", "2016-07-14 00:00:00");
			param.addValue("SETTLING_DATETIME", "2016-07-14 00:00:00");
			param.addValue("ORDERED_BY", "1000");
			param.addValue("PERFORMED_BY", "600A");
			param.addValue("DEPT_DISCHARGE_FROM", "3401");
			param.addValue("ITEM_CLASS", "3401");
			param.addValue("COSTS", "5.34");
			param.addValue("CHARGES", "5.34");
			param.addValue("CREATE_TIME", "2016-07-14 14:23:43");
			param.addValue("DATA_TIME", "2016-07-14 14:23:43");
			params[i] = param;
		}
		namedParameterJdbcTemplate.batchUpdate(sqlStr,params);
	}
  • 10000條平均須要290s左右

使用拼接SQL方式方式批量操做

  • 拼接方式以下
insert  test (....)  values(),(),().......();
  • 具體代碼以下
StringBuilder builder = new StringBuilder("INSERT INTO C_DSS_BASE_INP_SETTLE (\n" +
				"	ORG_ID,\n" +
				"	PK_HIS,\n" +
				"	SETTLING_DATETIME,\n" +
				"	ORDERED_BY,\n" +
				"	PERFORMED_BY,\n" +
				"	DEPT_DISCHARGE_FROM,\n" +
				"	ITEM_CLASS,\n" +
				"	COSTS,\n" +
				"	CHARGES,\n" +
				"	CREATE_TIME,\n" +
				"	DATA_TIME\n" +
				") VALUES  \n" );
		for (int i = 0; i <count; i++) {
			builder.append(
					" \n" +
					"	(\n" +
					"		'290001',\n" +
					"		'2016-07-14 00:00:00',\n" +
					"		'2016-07-14 00:00:00',\n" +
					"		'1000' ,\n" +
					"		'600A' ,\n" +
					"		'3401' ,\n" +
					"		'A',\n" +
					"		'5.34',\n" +
					"		'5.34',\n" +
					"		'2016-07-14 14:23:43',\n" +
					"		'2016-07-14 14:23:43'\n" +
					"	),");
		}
		String sqlStr = builder.toString();
		sqlStr = sqlStr.substring(0, sqlStr.length()-1);
		namedParameterJdbcTemplate.getJdbcOperations().update(sqlStr);
  • 10000條平均須要1s左右

結論

兩種方式差距至關大,故批量操做建議使用第二種,不過至於爲第二種效率高,
本菜鳥還須要進一步研究下,若是有知道緣由的大神請指教哈。spring

相關文章
相關標籤/搜索