直接贴代码比较清楚。这种方法既可以利用 ActiveRecord(Ruby)的开发效率,又可以达到很高的性能。
records = ActiveRecordClass.select(...).where(...).order(...).paginate(...)
# before
records.to_json
# after - with 1000% performance improvement
records.klass.connection.execute(records.to_sql).to_a.map { |record| [records.select_values, record].transpose.to_h } }.to_json