<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>liangbin661831</title>
    <link>https://ruby-china.org/liangbin661831</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>Sublime Text 3 Ctrl+Alt+P not working</title>
      <description>&lt;p&gt;大神们，sublime_text3 中，ctrl+alt+p 没有反应，是怎么回事啊，google 后实在找不到解决办法，
st 的控制台（Ctrl+~) 上也没有内容显示，感觉不是快捷键冲突，我用的是 ubuntu 14.我把所有的插件都卸载了，也不行。&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Thu, 05 Feb 2015 10:33:08 +0800</pubDate>
      <link>https://ruby-china.org/topics/24128</link>
      <guid>https://ruby-china.org/topics/24128</guid>
    </item>
    <item>
      <title>【已解决】mongoid 实现 group by 问题</title>
      <description>&lt;p&gt;我参考这篇文章：&lt;a href="http://www.dixis.com/?p=531" rel="nofollow" target="_blank"&gt;http://www.dixis.com/?p=531&lt;/a&gt;
clone 的 rubychina 的源码，打开 console,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Reply.collection.group(:key =&amp;gt; 'user_id', :initial =&amp;gt; { :count =&amp;gt; 0 }, :reduce =&amp;gt; "function(doc, prev){prev.count += 1;}")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;没有反应啊，大神们这是怎么回事啊？
mongodb 时启动的，rubychina 可以在本地跑起来。
或者大神门赐教 mongodb 怎么做 group by 比较好。&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Thu, 22 Jan 2015 11:26:48 +0800</pubDate>
      <link>https://ruby-china.org/topics/23875</link>
      <guid>https://ruby-china.org/topics/23875</guid>
    </item>
    <item>
      <title>ElasticSearch 安装 IK 详细步骤</title>
      <description>&lt;p&gt;编译 elasticsearch-analysis-ik&lt;/p&gt;

&lt;p&gt;#clone 最新的 ik 项目并编译
        git clone &lt;a href="https://github.com/medcl/elasticsearch-analysis-ik.git" rel="nofollow" target="_blank"&gt;https://github.com/medcl/elasticsearch-analysis-ik.git&lt;/a&gt; --depth=1
        cd elasticsearch-analysis-ik&lt;/p&gt;

&lt;p&gt;#如果没有安装 maven，执行 sudo apt-get install maven
        maven package&lt;/p&gt;

&lt;p&gt;配置 ES，三步就搞定了；&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;拷贝 jar.maven 执行完毕后会在当前目录下生成 target/releases 目录，将其中的 elasticsearch-analysis-ik-1.2.9.zip 拷贝到 ES 目录下的 plugins/analysis-ik，并解压，没有这个目录可以自己建。&lt;/li&gt;
&lt;li&gt;拷贝辞典。将 elasticsearch-analysis-ik 目录中的 config/ik 拷贝到 ES 的 config 目录。&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;修改 ES 的配置文件，指定 IK 为分词工具，到 ES 目录下，打开 config/elasticsearch.yml，在最后添加&lt;/p&gt;

&lt;p&gt;index.analysis.analyzer.ik.type : "ik"&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Sun, 18 Jan 2015 11:14:03 +0800</pubDate>
      <link>https://ruby-china.org/topics/23794</link>
      <guid>https://ruby-china.org/topics/23794</guid>
    </item>
    <item>
      <title>求教 ElasticSearch 添加 IK 分词</title>
      <description>&lt;p&gt;git clone &lt;a href="https://github.com/medcl/elasticsearch-analysis-ik.git" rel="nofollow" target="_blank"&gt;https://github.com/medcl/elasticsearch-analysis-ik.git&lt;/a&gt;
cd elasticsearch-analysis-ik/
mvn package&lt;/p&gt;

&lt;p&gt;[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -&amp;gt; [Help 1]
[ERROR]&lt;br&gt;
[ERROR]   The project org.elasticsearch:elasticsearch-analysis-ik:1.2.9 (/home/lb/Downloads/elasticsearch-analysis-ik/pom.xml) has 1 error
[ERROR]     Non-parseable POM /home/lb/.m2/repository/org/sonatype/oss/oss-parent/7/oss-parent-7.pom: Expected root element 'project' but found 'html' (position: START_TAG seen ... &lt;a href="/1" class="user-mention" title="@1"&gt;&lt;i&gt;@&lt;/i&gt;1&lt;/a&gt;:6)  @ /home/lb/.m2/repository/org/sonatype/oss/oss-parent/7/oss-parent-7.pom, line 1, column 6 -&amp;gt; [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] &lt;a href="http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException" rel="nofollow" target="_blank"&gt;http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException&lt;/a&gt;
[ERROR] [Help 2] &lt;a href="http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException" rel="nofollow" target="_blank"&gt;http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;结果就但疼了，求大神指点&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Sat, 17 Jan 2015 23:22:56 +0800</pubDate>
      <link>https://ruby-china.org/topics/23790</link>
      <guid>https://ruby-china.org/topics/23790</guid>
    </item>
    <item>
      <title>Rails4.2 console 打不开解决办法</title>
      <description>&lt;p&gt;这个找了好长时间，才发现时 rails4.2 默认添加的 spring gem 包的问题，具体原因，小弟能力不行，也搞不明白，解决办法：关闭 spring 在打开&lt;/p&gt;

&lt;p&gt;bin/spring stop &lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Fri, 16 Jan 2015 13:22:50 +0800</pubDate>
      <link>https://ruby-china.org/topics/23776</link>
      <guid>https://ruby-china.org/topics/23776</guid>
    </item>
    <item>
      <title>Rails4.2 assets 目录中.keep 文件是什么意思啊，有什么用呢？</title>
      <description>&lt;p&gt;Rails4.2 assets 目录中.keep 文件是什么意思啊，有什么用呢？&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Fri, 16 Jan 2015 09:22:13 +0800</pubDate>
      <link>https://ruby-china.org/topics/23769</link>
      <guid>https://ruby-china.org/topics/23769</guid>
    </item>
    <item>
      <title>Capistrano 中的一些知识</title>
      <description>&lt;p&gt;原文地址： &lt;a href="http://benediktdeicke.com/2013/02/what-you-did-not-know-about-capistrano-yet/" rel="nofollow" target="_blank"&gt;http://benediktdeicke.com/2013/02/what-you-did-not-know-about-capistrano-yet/&lt;/a&gt;
学到了
capistrano 中变量定义方法？
fetch 这个到处都是的方法到底时什么意思？
cap deploy -s branch=??????, -s 什么意思？&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Tue, 06 Jan 2015 14:56:29 +0800</pubDate>
      <link>https://ruby-china.org/topics/23575</link>
      <guid>https://ruby-china.org/topics/23575</guid>
    </item>
    <item>
      <title>【转载】向数据库插入多条数据的效率问题</title>
      <description>&lt;p&gt;Mass inserting data in Rails without killing your performance
Mass inserting is one of those operations that isn't really well-supported by ActiveRecord, but which has to be done nonethless. You might say, "Well hey, I'll just run a loop and create a bunch of AR objects, no sweat".&lt;/p&gt;

&lt;p&gt;That'll work, but if speed is a factor, it might not be your best option.&lt;/p&gt;

&lt;p&gt;ActiveRecord makes interface to the DB very easy, but it doesn't necessarily make it fast. Instantiating an ActiveRecord object is costly, and if you do a lot of 'em, that's going to cause you to bump up against the garbage collector, which will significantly hinder performance. There are several options, though, depending on how much speed you need.&lt;/p&gt;

&lt;p&gt;There are benchmarks at the bottom of the post, so if you're just interested in those, scroll down.&lt;/p&gt;

&lt;p&gt;Option 1: Use transactions
This is definitely the easiest method, and while you'll realize gains from it, you aren't going to be breaking any speed records using only this method. However, it's well worth it if you are doing mass inserts via ActiveRecord.&lt;/p&gt;

&lt;p&gt;Instead of&lt;/p&gt;

&lt;p&gt;1000.times { Model.create(options) }
You want:&lt;/p&gt;

&lt;p&gt;ActiveRecord::Base.transaction do
  1000.times { Model.create(options) }
end
The net effect is that the database performs all of your inserts in a single transaction, rather than starting and committing a new transaction for every request.&lt;/p&gt;

&lt;p&gt;Options 2: Get down and dirty with the raw SQL
If you know that your data is valid and can afford to skip validations, you can save a lot of time by just jumping directly to raw SQL.&lt;/p&gt;

&lt;p&gt;Imagine, for example, that you're running the following:&lt;/p&gt;

&lt;p&gt;1000.times {|i| Foo.create(:counter =&amp;gt; i) }
That's going to create 1000 ActiveRecord objects, run validations, generate the insert SQL, and dump it into the database. You can realize large performance gains by just jumping directly to the generated SQL.&lt;/p&gt;

&lt;p&gt;1000.times do |i|
  Foo.connection.execute "INSERT INTO foos (counter) values (#{i})"
end
You should use sanitize_sql and such as necessary to sanitize input values if they are not already sanitized, but with this technique you can realize extremely large performance gains. Of course, wrapping all those inserts in a single transaction, as in Option 1 gets you even more performance.&lt;/p&gt;

&lt;p&gt;Foo.transaction do
  1000.times do |i|
    Foo.connection.execute "INSERT INTO foos (counter) values (#{i})"
  end
end
Option 3: A single mass insert
Many databases support mass inserts of data in a single insert statement. They are able to significantly optimize this operation under the hood, and if you're comfortable using it, will be your fastest option by far.&lt;/p&gt;

&lt;p&gt;inserts = []
TIMES.times do
  inserts.push "(3.0, '2009-01-23 20:21:13', 2, 1)"
end
sql = "INSERT INTO user_node_scores (&lt;code&gt;score&lt;/code&gt;, &lt;code&gt;updated_at&lt;/code&gt;, &lt;code&gt;node_id&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;) VALUES #{inserts.join(", ")}"
No transaction block is necessary here, since it's just a single statement, and the DB will wrap it in a transaction. We build an array of value sets to include, then just join them into the INSERT statement. We don't use string concatenation, since it will result in significantly more string garbage generated, which could potentially get us into the GC, which we're trying to avoid (and hey, memory savings are always good).&lt;/p&gt;

&lt;p&gt;Option 4: ActiveRecord::Extensions
njero in #rubyonrails pointed me at this nifty little gem and I decided to include it. It seems to try to intelligently do mass inserts of data. I wasn't able to get it to emulate the single mass insert for a MySQL database, but it does provide a significant speed increase without much additional work, and can preserve your validations and such.&lt;/p&gt;

&lt;p&gt;There's the obvious added benefit that you stay in pure Ruby, and don't have to get into the raw SQL.&lt;/p&gt;

&lt;p&gt;columns = [:score, :node_id, :user_id]
values = []
TIMES.times do
    values.push [3, 2, 1]
end&lt;/p&gt;

&lt;p&gt;UserNodeScore.import columns, values
Benchmarks
I used a simple script to test each of the methods described here.&lt;/p&gt;

&lt;p&gt;require "ar-extensions"&lt;/p&gt;

&lt;p&gt;CONN = ActiveRecord::Base.connection
TIMES = 10000&lt;/p&gt;

&lt;p&gt;def do_inserts
    TIMES.times { UserNodeScore.create(:user_id =&amp;gt; 1, :node_id =&amp;gt; 2, :score =&amp;gt; 3) }
end&lt;/p&gt;

&lt;p&gt;def raw_sql
    TIMES.times { CONN.execute "INSERT INTO &lt;code&gt;user_node_scores&lt;/code&gt; (&lt;code&gt;score&lt;/code&gt;, &lt;code&gt;updated_at&lt;/code&gt;, &lt;code&gt;node_id&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;) VALUES(3.0, '2009-01-23 20:21:13', 2, 1)" }
end&lt;/p&gt;

&lt;p&gt;def mass_insert
    inserts = []
    TIMES.times do
        inserts.push "(3.0, '2009-01-23 20:21:13', 2, 1)"
    end
    sql = "INSERT INTO user_node_scores (&lt;code&gt;score&lt;/code&gt;, &lt;code&gt;updated_at&lt;/code&gt;, &lt;code&gt;node_id&lt;/code&gt;, &lt;code&gt;user_id&lt;/code&gt;) VALUES #{inserts.join(", ")}"
    CONN.execute sql
end&lt;/p&gt;

&lt;p&gt;def activerecord_extensions_mass_insert(validate = true)
    columns = [:score, :node_id, :user_id]
    values = []
    TIMES.times do
        values.push [3, 2, 1]
    end&lt;/p&gt;

&lt;p&gt;UserNodeScore.import columns, values, {:validate =&amp;gt; validate}
end&lt;/p&gt;

&lt;p&gt;puts "Testing various insert methods for #{TIMES} inserts\n"
puts "ActiveRecord without transaction:"
puts base = Benchmark.measure { do_inserts }&lt;/p&gt;

&lt;p&gt;puts "ActiveRecord with transaction:"
puts bench = Benchmark.measure { ActiveRecord::Base.transaction { do_inserts } }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)&lt;/p&gt;

&lt;p&gt;puts "Raw SQL without transaction:"
puts bench = Benchmark.measure { raw_sql }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)&lt;/p&gt;

&lt;p&gt;puts "Raw SQL with transaction:"
puts bench = Benchmark.measure { ActiveRecord::Base.transaction { raw_sql } }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)&lt;/p&gt;

&lt;p&gt;puts "Single mass insert:"
puts bench = Benchmark.measure { mass_insert }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)&lt;/p&gt;

&lt;p&gt;puts "ActiveRecord::Extensions mass insert:"
puts bench = Benchmark.measure { activerecord_extensions_mass_insert }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)&lt;/p&gt;

&lt;p&gt;puts "ActiveRecord::Extensions mass insert without validations:"
puts bench = Benchmark.measure { activerecord_extensions_mass_insert(true)  }
puts sprintf("  %2.2fx faster than base", base.real / bench.real)
And the results:&lt;/p&gt;

&lt;p&gt;Testing various insert methods for 10000 inserts
ActiveRecord without transaction:
 14.930000   0.640000  15.570000 ( 18.898352)
ActiveRecord with transaction:
 13.420000   0.310000  13.730000 ( 14.619136)
  1.29x faster than base
Raw SQL without transaction:
  0.920000   0.170000   1.090000 (  3.731032)
  5.07x faster than base
Raw SQL with transaction:
  0.870000   0.150000   1.020000 (  1.648834)
  11.46x faster than base
Single mass insert:
  0.000000   0.000000   0.000000 (  0.268634)
  70.35x faster than base
ActiveRecord::Extensions mass insert:
  6.580000   0.280000   6.860000 (  9.409169)
  2.01x faster than base
ActiveRecord::Extensions mass insert without validations:
  6.550000   0.240000   6.790000 (  9.446273)
  2.00x faster than base
The results are fairly self-explainatory, but of particular note is the specific single INSERT statement. At 70x faster than the non-transactional ActiveRecord insert, if you need speed, it's hard to beat.&lt;/p&gt;

&lt;p&gt;Conclusions
ActiveRecord is great, but sometimes it'll hold you back. Finding the balance between ease of use (full ActiveRecord) and performance (bare metal mass inserts) can have a profound effect on the performance of your app.&lt;/p&gt;

&lt;p&gt;原文地址：&lt;a href="https://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/" rel="nofollow" target="_blank"&gt;https://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/&lt;/a&gt;&lt;/p&gt;</description>
      <author>liangbin661831</author>
      <pubDate>Tue, 06 Jan 2015 09:56:48 +0800</pubDate>
      <link>https://ruby-china.org/topics/23566</link>
      <guid>https://ruby-china.org/topics/23566</guid>
    </item>
  </channel>
</rss>
