﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>学习日记 &#187; java</title>
	<atom:link href="https://www.softwareace.cn/?cat=107&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://www.softwareace.cn</link>
	<description>时刻想着为自己的产品多做一些对他好的事情</description>
	<lastBuildDate>Fri, 20 Mar 2026 06:58:28 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>SQLite 批量insert &#8211; 如何加速SQLite的插入操作</title>
		<link>https://www.softwareace.cn/?p=1121</link>
		<comments>https://www.softwareace.cn/?p=1121#comments</comments>
		<pubDate>Fri, 23 Jan 2015 09:21:20 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=1121</guid>
		<description><![CDATA[本人翻译, 原文见: http://tech.vg.no/2011/04/04/speeding-up-sql [&#8230;]]]></description>
				<content:encoded><![CDATA[<p style="color: #000000;">本人翻译, 原文见:</p>
<p style="color: #000000;"><a style="color: #006600;" href="http://tech.vg.no/2011/04/04/speeding-up-sqlite-insert-operations/">http://tech.vg.no/2011/04/04/speeding-up-sqlite-insert-operations/</a></p>
<p style="color: #000000;">
<p style="color: #000000;">我正在开发一个Android程序, 它使用SQLite存储大约6000行的数据, 这些数据会定期从网上更新. 在模拟器上, 从网络获取和解析CSV格式的数据所花的时间大概是20秒, 但是把数据插入的数据库的时间是71秒.</p>
<p style="color: #000000;">
<p style="color: #000000;">因为数据更新的操作差不多一个星期才有一次, 因此我认为1分多钟的操作时间是可以接受的. 但当我把程序在真机上跑的时候, 6000行的插入时间让我吓了一跳 &#8212; 478秒, 差不多8分钟. 很奇怪, 一般来说, 真机要比模拟器快, 何况我用的是Sanmsung Galaxy S &#8211; 当时最快的Android设备之一. 这是我第一次使用SQLite, 我想我下一步得优化插入操作了.</p>
<p style="color: #000000;">
<div id="" class="dp-highlighter" style="color: #000000;">
<div class="bar">
<div class="tools" style="font-weight: bold;">Sql代码  <a style="color: #006600;" title="收藏这段代码"><img class="star" src="http://hchaojie.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div>
</div>
<ol class="dp-sql" style="color: #2b91af;" start="1">
<li><span style="color: black;">String sql = <span class="string" style="color: blue;">&#8220;INSERT INTO table (number, nick) VALUES (?, ?)&#8221;</span>;  </span></li>
<li><span style="color: black;">SQLiteStatement stmt = db.compileStatement(sql);  </span></li>
<li><span style="color: black;"><span class="keyword" style="font-weight: bold; color: #7f0055;">for</span> (<span class="keyword" style="font-weight: bold; color: #7f0055;">int</span> i = 0; i &lt; <span class="keyword" style="font-weight: bold; color: #7f0055;">values</span>.<span class="keyword" style="font-weight: bold; color: #7f0055;">size</span>(); i++) {  </span></li>
<li><span style="color: black;">    stmt.bindString(1, <span class="keyword" style="font-weight: bold; color: #7f0055;">values</span>.get(i).number);  </span></li>
<li><span style="color: black;">    stmt.bindString(2, <span class="keyword" style="font-weight: bold; color: #7f0055;">values</span>.get(i).nick);  </span></li>
<li><span style="color: black;">    stmt.<span class="keyword" style="font-weight: bold; color: #7f0055;">execute</span>();  </span></li>
<li><span style="color: black;">    stmt.clearBindings();  </span></li>
<li><span style="color: black;">}  </span></li>
</ol>
</div>
<p style="color: #000000;">
<p style="color: #000000;">有了上面的改动后, 我在模拟器上测试, 时间从71秒减少到56秒, 但是在真机上测的时候, 时间反而多了几秒, 我反复测了几次, 还是差不多一样的结果.</p>
<p style="color: #000000;">
<p style="color: #000000;">进一步研究, 我从SQLite的一个文档页面看到“PRAGMA synchronous = OFF” 将会告诉SQLite, 当它把数据传入操作系统的时候, 不要立即同步. 这个设置让插入时间从71秒减少的50秒, 在真机上的结果是361秒, 差不多快2分钟.</p>
<p style="color: #000000;">
<p style="color: #000000;">感觉到找到路子之后, 我认为是文件系统减慢了插入操作, 我在网上找到了很多关于Galaxy S I/O 性能问题的参考 &#8211; 所有的都与RFS文件系统相关. 对于SQLite而言, 它每插入一次数据, 都会执行一下fsync, 以保证数据写入了磁盘. 再看SQLite文档, 我发现用transactions能够将数据保存在内存中, 只有在commit时候才写入文件系统. 因此我改动如下:</p>
<p style="color: #000000;">
<div id="" class="dp-highlighter" style="color: #000000;">
<div class="bar">
<div class="tools" style="font-weight: bold;">Java代码  <a style="color: #006600;" title="收藏这段代码"><img class="star" src="http://hchaojie.iteye.com/images/icon_star.png" alt="收藏代码" /></a></div>
</div>
<ol class="dp-j" style="color: #2b91af;" start="1">
<li><span style="color: black;">String sql = <span class="string" style="color: blue;">&#8220;INSERT INTO table (number, nick) VALUES (?, ?)&#8221;</span>;  </span></li>
<li><span style="color: black;">db.beginTransaction();  </span></li>
<li><span style="color: black;">   </span></li>
<li><span style="color: black;">SQLiteStatement stmt = db.compileStatement(sql);  </span></li>
<li><span style="color: black;"><span class="keyword" style="font-weight: bold; color: #7f0055;">for</span> (<span class="keyword" style="font-weight: bold; color: #7f0055;">int</span> i = <span class="number" style="color: #c00000;">0</span>; i &lt; values.size(); i++) {  </span></li>
<li><span style="color: black;">    stmt.bindString(<span class="number" style="color: #c00000;">1</span>, values.get(i).number);  </span></li>
<li><span style="color: black;">    stmt.bindString(<span class="number" style="color: #c00000;">2</span>, values.get(i).nick);  </span></li>
<li><span style="color: black;">    stmt.execute();  </span></li>
<li><span style="color: black;">    stmt.clearBindings();  </span></li>
<li><span style="color: black;">}  </span></li>
<li><span style="color: black;">   </span></li>
<li><span style="color: black;">db.setTransactionSuccessful();  </span></li>
<li><span style="color: black;">db.endTransaction();  </span></li>
</ol>
</div>
<p style="color: #000000;">
<p style="color: #000000;">结合transactions和 compiled statements后, 性能有了巨大的提升: 从71秒到不可置信的5秒! 在Galaxy上的结果更是牛逼: 从478秒到1.5秒!</p>
<p style="color: #000000;">
<p style="color: #000000;">结论:</p>
<p style="color: #000000;">- 除非你只执行单次的insert, 或者你需要数据立即写入文件系统, 不然的话就用transactions</p>
<p style="color: #000000;">- 保证你的程序在真机上测试过, 最好是多台机器上测.</p>
<p style="color: #000000;">- 我上面的性能提升只在Samsung Galaxy S上测过, 不同的机器可能还是会有性能问题.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=1121</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>开源客户管理及进销存财务系统</title>
		<link>https://www.softwareace.cn/?p=1119</link>
		<comments>https://www.softwareace.cn/?p=1119#comments</comments>
		<pubDate>Thu, 22 Jan 2015 01:21:20 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.softwareace.cn/?p=1119</guid>
		<description><![CDATA[ECP  是基于jfinal、avalon、bootstrap、jqGrid、snaker工作流开发的客户关系 [&#8230;]]]></description>
				<content:encoded><![CDATA[<div style="color: #000000;">
<div id="p_fullcontent" class="detail TextContent">
<p>ECP  是基于<a style="color: #4183c4;" href="http://www.oschina.net/p/jfinal">jfinal</a>、<a style="color: #4183c4;" href="http://www.oschina.net/p/avalon">avalon</a>、<a style="color: #4183c4;" href="http://www.oschina.net/p/bootstrap">bootstrap</a>、<a style="color: #4183c4;" href="http://www.oschina.net/p/jqgrid">jqGrid</a>、<a style="color: #4183c4;" href="http://www.oschina.net/p/snaker">snaker</a>工作流开发的客户关系及进销存财务系统。</p>
<p>支持多企业使用。登录信息：企业名是：北京朗天鑫业信息工程技术有限公司，用户名：loyin 密码：123456</p>
<p>登录界面：</p>
<p><img src="http://static.oschina.net/uploads/space/2014/1104/142453_uMRG_105457.png" alt="" /></p>
<p>主界面：</p>
<p><img src="http://static.oschina.net/uploads/space/2014/1104/142716_YfP8_105457.png" alt="" /></p>
<p>功能菜单：</p>
<p><img src="http://static.oschina.net/uploads/space/2014/1104/142840_1SZD_105457.png" alt="" /></p>
</div>
<h1><a id="如何部署？" class="anchor" style="color: #4183c4;" href="http://git.oschina.net/loyin/ECP#如何部署？"></a>如何部署？</h1>
<p>需要将doc/数据库备份中的buck文件在postgresql管理工具里进行恢复到数据库ecp。</p>
</div>
<div style="color: #000000;">项目主页：http://git.oschina.net/loyin/ECP</div>
<div style="color: #000000;">常见问题：http://git.oschina.net/loyin/ECP/issues</div>
<div style="color: #000000;">项目源代码git: https://git.oschina.net/loyin/ECP.git  (附件已打包)</div>
<div style="color: #000000;">项目性质：开源，可随意修改，二次开发</div>
<div style="color: #000000;">编程语言：java</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.softwareace.cn/?feed=rss2&#038;p=1119</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
