我想使用Java一次将多行插入MySQL表.行数是动态的.过去我在做......
for (String element : array) {
myStatement.setString(1, element[0]);
myStatement.setString(2, element[1]);
myStatement.executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)
我想优化它以使用MySQL支持的语法:
INSERT INTO table (col1, col2) VALUES ('val1', 'val2'), ('val1', 'val2')[, ...]
Run Code Online (Sandbox Code Playgroud)
但是PreparedStatement我不知道有什么方法可以做到这一点,因为我事先不知道array会包含多少元素.如果a不可能PreparedStatement,我还能怎么做(并且仍然逃避数组中的值)?
JPA/EJB3框架是否提供了批量插入操作的标准方法......?我们使用hibernate作为持久性框架,所以我可以回退到Hibernate Session并使用组合session.save()/ session.flush()实现批量插入.但是想知道EJB3是否支持这个......
在cassandra列族中插入多行的最有效方法是什么.是否可以在一次通话中完成此操作.
现在我的方法是添加多个列然后执行.在一个电话中,我坚持一行.我正在寻找策略,以便我可以进行批量插入.
我有一个查询,我需要"批量"插入行与没有标识的主键的表.
--TableA
--PK int (Primary key, no-identity)
--CustNo int
INSERT INTO TableA (PK,CustNo)
SELECT (SELECT MAX(PK)+1 AS PK FROM TableA), CustNo
FROM Customers
Run Code Online (Sandbox Code Playgroud)
(简化示例 - 请不要评论可能的并发问题:-))
问题是它没有为每个"处理的"行增加PK,并且我得到主键违规.
我知道如何使用游标/ while循环,但我想避免这种情况,并以基于集合的方式解决它,如果这甚至可能的话?
(运行SQL Server 2008 Standard)
我有一些月度天气数据,我想插入到Oracle数据库表中,但我想在批处理中插入相应的记录,以提高效率.任何人都可以建议我如何在Python中执行此操作?
例如,假设我的表有四个字段:工作站ID,日期和两个值字段.记录由站ID和日期字段(组合键)唯一标识.我必须为每个工作站插入的值将保存在具有X个完整年份数据的列表中,因此,例如,如果有两年的值,则值列表将包含24个值.
我假设如果我想一次插入一个记录,下面就是我这样做的方式:
connection_string = "scott/tiger@testdb"
connection = cx_Oracle.Connection(connection_string)
cursor = cx_Oracle.Cursor(connection)
station_id = 'STATION_1'
start_year = 2000
temps = [ 1, 3, 5, 7, 9, 1, 3, 5, 7, 9, 1, 3 ]
precips = [ 2, 4, 6, 8, 2, 4, 6, 8, 2, 4, 6, 8 ]
number_of_years = len(temps) / 12
for i in range(number_of_years):
for j in range(12):
# make a date for the first day of the month
date_value = datetime.date(start_year + i, j …Run Code Online (Sandbox Code Playgroud) Spring的新手,我试图在List<Map<String, Object>>表中插入一个.到目前为止,我一直在使用SqlParameterSourcefor batch update,它在向它们提供java bean时工作正常.像这样的东西:
@Autowired
private NamedParameterJDBCTemplate v2_template;
public int[] bulkInsertIntoSiteTable(List<SiteBean> list){
SqlParameterSource[] batch = SqlParameterSourceUtils
.createBatch(list.toArray());
int[] updateCounts = v2_template
.batchUpdate(
"insert into sitestatus (website, status, createdby) values (:website, :status, :username)",
batch);
return updateCounts;
}
Run Code Online (Sandbox Code Playgroud)
但是,我尝试使用相同的技术代替bean的地图列表,它失败了(正确地说是这样).
public int[] bulkInsertIntoSiteTable(List<Map<String, Object>> list){
SqlParameterSource[] batch = SqlParameterSourceUtils
.createBatch(list.toArray());
int[] updateCounts = v2_template
.batchUpdate(
"insert into sitestatus (website, status, createdby) values (:website, :status, :username)",
batch);
return updateCounts;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码失败,出现以下异常:
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: No value supplied …Run Code Online (Sandbox Code Playgroud) 我试图使用Hibernate(JPA)在5秒内在MYSQL表中插入100,000行.我已经尝试过hibernate提供的每一个技巧,但仍然不能超过35秒.
第一次优化:我开始使用IDENTITY序列生成器,这导致插入60秒.我后来放弃了序列生成器并开始@Id自己通过阅读MAX(id)和使用AtomicInteger.incrementAndGet()自己分配字段来分配字段.这将插入时间减少到35秒.
第二次优化:我通过添加启用了批量插入
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
到配置.我很震惊地发现批量插入绝对没有减少插入时间.现在还有35秒!
现在,我正在考虑尝试使用多个线程插入.任何人有任何指针?我应该选择MongoDB吗?
下面是我的配置:1.Hibernate配置`
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.progresssoft.manishkr" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.username}"></property>
<property name="password" value="${database.password}"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" …Run Code Online (Sandbox Code Playgroud) 我需要使用EJB 3,Hibernate,Spring Data和Oracle进行大量插入.最初,我使用的是Spring Data,代码如下:
talaoAITDAO.save(taloes);
Run Code Online (Sandbox Code Playgroud)
其中talaoAITDAO是Spring Data JpaRepository子类,taloes是TalaoAIT实体的集合.在此实体中,其各自的ID具有以下形式:
@Id
@Column(name = "ID_TALAO_AIT")
@SequenceGenerator(name = "SQ_TALAO_AIT", sequenceName = "SQ_TALAO_AIT", allocationSize = 1000)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_TALAO_AIT")
private Long id;
Run Code Online (Sandbox Code Playgroud)
此实体也没有相关实体进行级联插入.
我的问题是,所有实体都是单独插入的(例如INSERT INTO TABLE(col1, col2) VALUES (val1, val2)).有时,它可能会导致超时,并且所有插入都将被回滚.我想要在批量插入中转换这些单独的插入(例如INSERT INTO TABLE(col1, col2) VALUES (val11, val12), (val21, val22), (val31, val32), ...).
研究替代方案以提高性能,我在hibernate文档中找到了这个页面,超出了 Hibernate批量大小混淆和其他页面.基于它们,我写了这段代码:
Session session = super.getEntityManager().unwrap(Session.class);
int batchSize = 1000;
for (int i = 0; i < taloes.size(); i++) {
TalaoAIT …Run Code Online (Sandbox Code Playgroud) 我正在使用API进行大量的计算,最后有100个数据库字段,最后有一个很大的Foreach循环.
在每次迭代中,我都会在数据库中插入数据.我想在最后插入一次数据(如CodeIgniter中的批量插入).
任何机构都知道如何在迭代结束时插入所有数据.而不是每次迭代它在数据库中插入行.
我想在循环结束时插入数据.任何帮助或想法赞赏.
我使用JDBC批量插入插入许多记录.有没有办法获得每条记录的生成密钥?我可以使用ps.getGeneratedKeys()批量插入吗?
我在用 oracle.jdbc.OracleDriver
final String insert = "Insert into Student(RollNumber, Name, Age) values(StudentSEQ.nextval, ? , ?)";
final int BATCH_SIZE = 998;
int count = 0;
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement(insert);
for (Student s : students) {
ps.setString(1, s.getName());
ps.setInt(2, s.getAge());
ps.addBatch();
count++;
if (count % BATCH_SIZE == 0) {
// Insert records in batches
ps.executeBatch();
}
}
// Insert remaining records
ps.executeBatch();
} finally {
if(ps != …Run Code Online (Sandbox Code Playgroud) batch-insert ×10
java ×6
hibernate ×3
jpa ×3
jdbc ×2
mysql ×2
oracle ×2
cassandra ×1
cx-oracle ×1
insert ×1
laravel ×1
laravel-5.2 ×1
orm ×1
primary-key ×1
python ×1
python-2.7 ×1
spring ×1
sql-server ×1
t-sql ×1