我试图使插入到 mysql 的速度更快,所以我写了一个带有批量插入的代码。问题是,当我运行以下代码时,两种方法的结果相同(耗时:2 分钟或 173 秒)。欢迎就此事发表任何评论。
MySQLConn mysqlconn = new MySQLConn(db,username,pass);
Connection conn =mysqlconn.getConnection();
PreparedStatement ps = null;
String query = "insert into table (column) values (?)";
System.out.println("bulk insert started with "+10000 );
long startTime = System.currentTimeMillis();
try {
ps = conn.prepareStatement(query);
long start = System.currentTimeMillis();
for(int i =0; i<10000;i++){
ps.setString(1, "Name"+i);
ps.addBatch();
if(i%1000 == 0) ps.executeBatch();
}
ps.executeBatch();
System.out.println("Time Taken="+(System.currentTimeMillis()-start));
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("End inserting data...");
long stopTime = System.currentTimeMillis();
long elapsedTime = (stopTime - startTime);
long days = TimeUnit.MILLISECONDS.toDays(elapsedTime);
long hours = TimeUnit.MILLISECONDS.toHours(elapsedTime);
long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTime);
long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTime);
System.out.println("Time elapsed: "+minutes+"min, or "+seconds+"sec");
System.out.println("normal insert started with "+10000 );
long startTimeNormal = System.currentTimeMillis();
for(int i=0;i<10000;i++){
String ins="Data"+i;
Insert insert = new Insert(conn,ins,"db.table", "column" );
}
System.out.println("End inserting data...");
long stopTimeNormal = System.currentTimeMillis();
long elapsedTimeNormal = (stopTimeNormal - startTimeNormal);
long daysN = TimeUnit.MILLISECONDS.toDays(elapsedTime);
long hoursN = TimeUnit.MILLISECONDS.toHours(elapsedTime);
long minutesN = TimeUnit.MILLISECONDS.toMinutes(elapsedTime);
long secondsN = TimeUnit.MILLISECONDS.toSeconds(elapsedTime);
System.out.println("Time elapsed: "+minutesN+"min, or "+secondsN+"sec");
最佳答案
您使用 addBatch 和 executeBatch,这很好,但您还需要将连接上的自动提交设置为 false 以实现更快的执行时间。
沿线的东西:
Connection conn = mysqlconn.getConnection();
conn.setAutoCommit(false); // here
PreparedStatement ps = null;
String query = "insert into table (column) values (?)";
System.out.println("bulk insert started with "+10000 );
long startTime = System.currentTimeMillis();
try {
ps = conn.prepareStatement(query);
long start = System.currentTimeMillis();
for(int i =0; i<10000;i++){
ps.setString(1, "Name"+i);
ps.addBatch();
if(i%1000 == 0) {
ps.executeBatch();
conn.commit(); // here
}
}
ps.executeBatch();
conn.commit(); // here
System.out.println("Time Taken="+(System.currentTimeMillis()-start));
} catch (SQLException e) {
e.printStackTrace();
}
关于Java MySQL 批量插入与正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939558/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'
我正在尝试创建一个带有项目符号字符的Ruby1.9.3字符串。str="•"+"helloworld"但是,当我输入它时,我收到有关非ASCII字符的语法错误。我该怎么做? 最佳答案 你可以把Unicode字符放在那里。str="\u2022"+"helloworld" 关于ruby-如何在Ruby字符串中插入项目符号字符?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1195
在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa
我想知道是否可以通过自动创建数组来插入数组,如果数组不存在的话,就像在PHP中一样:$toto[]='titi';如果尚未定义$toto,它将创建数组并将“titi”压入。如果已经存在,它只会推送。在Ruby中我必须这样做:toto||=[]toto.push('titi')可以一行完成吗?因为如果我有一个循环,它会测试“||=”,除了第一次:Person.all.eachdo|person|toto||=[]#with1billionofperson,thislineisuseless999999999times...toto.push(person.name)你有更好的解决方案吗?
在我的用户模型中,我有一堆属性,例如is_foos_admin和is_bars_admin,它们决定允许用户编辑哪些类型的记录。我想干掉我的编辑链接,目前看起来像这样:'edit'ifcurrent_user.is_foos_admin?%>...'edit'ifcurrent_user.is_bars_admin?%>我想做一个帮助程序,让我传入一个foo或bar并返回一个链接来编辑它,就像这样:助手可能看起来像这样(这不起作用):defedit_link_for(thing)ifcurrent_user.is_things_admin?link_to'Edit',edit_poly
我在这方面尝试了很多URL,在我遇到这个特定的之前,它们似乎都很好:require'rubygems'require'nokogiri'require'open-uri'doc=Nokogiri::HTML(open("http://www.moxyst.com/fashion/men-clothing/underwear.html"))putsdoc这是结果:/Users/macbookair/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/open-uri.rb:353:in`open_http':404NotFound(OpenURI::HT
我有以下现有的Dog对象数组,它们按age属性排序:classDogattr_accessor:agedefinitialize(age)@age=ageendenddogs=[Dog.new(1),Dog.new(4),Dog.new(10)]我现在想插入一条新的狗记录,并将它放在数组中的正确位置。假设我想插入这个对象:another_dog=Dog.new(8)我想把它插入到数组中,让它成为数组中的第三项。这是一个人为的示例,旨在演示我特别想如何将一个项目插入到现有的有序数组中。我意识到我可以创建一个全新的数组并重新对所有对象进行排序,但这不是我的目标。谢谢!
在字符串连接中,是否可以直接在语句中包含条件?在下面的示例中,我希望仅当dear列表不为空时才连接"mydear"。dear=""string="hello"+"mydear"unlessdear.empty?+",goodmorning!"但是结果报错:undefinedmethod'+'fortrue我知道另一种方法是在这条语句之前定义一个额外的变量,但我想避免这种情况。 最佳答案 使用插值而不是连接更容易和更具可读性:dear=""string="hello#{'mydear'unlessdear.empty?},goodmo
我想使用Ruby检查数千对文件中的每对文件是否包含相同的信息。有人能指出我正确的方向吗? 最佳答案 require'fileutils'FileUtils.compare_file('file1','file2')当且仅当文件file1和file2相同时返回true。 关于ruby-如何批量检查文件内容是否相同,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/33769865/