Postgresql批量入库方式

需要一次提交批量数据到数据库,采用如下写法在性能上能提高10倍左右!

public synchronized static void sendToPG(List standList) {

		System.out.println(" insert data"+standList.size());
		try {
			PreparedStatement pstmt = null;
			String sql = "INSERT INTO table_name (uuid,threat_score,type,src_ip,timestamp,des_ip,username,log_source,"
					+ "error_message,error_content,filesize,udflag,filename,filetype,login_type,process_name,raw_data) "
					+ " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			Class.forName(Configs.getPgDriver());
			Connection conn = DriverManager.getConnection(Configs.getPgJdbcUrl(), Configs.getPgUsername(),
					Configs.getPgPassword());
			conn.setAutoCommit(false);
			SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pstmt = conn.prepareStatement(sql);
			int count_insert = 0;
			for (StandardOutput stand : standList) {
				count_insert++;
				pstmt.setString(1, stand.getUuid());
				pstmt.setFloat(2, stand.getThreat_score());
				pstmt.setString(3, stand.getType());
				pstmt.setString(4, stand.getSrc_ip());
				pstmt.setTimestamp(5, Timestamp.valueOf(sdformat.format(Long.parseLong(stand.getTimestamp()))));
				pstmt.setString(6, stand.getDes_ip());
				pstmt.setString(7, stand.getUsername());
				pstmt.setString(8, stand.getLog_source());
				pstmt.setString(9, stand.getError_message());
				pstmt.setString(10, stand.getError_content());
				pstmt.setString(11, stand.getFilesize());
				pstmt.setString(12, stand.getUdflag());
				pstmt.setString(13, stand.getFilename());
				pstmt.setString(14, stand.getFiletype());
				pstmt.setString(15, stand.getLogin_type());
				pstmt.setString(16, stand.getProcess_name());
				pstmt.setString(17, stand.getRaw_data());
				pstmt.addBatch();
				if (count_insert ==standList.size() ) {
					pstmt.executeBatch();
					conn.commit();
					pstmt.clearBatch();
				}
			}

			if (pstmt != null) {
				pstmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

你可能感兴趣的:(Postgresql批量入库方式)