简体   繁体   中英

To response the query result with the equivalent performance as `psycopg2` does

I try to explain my case clearly.

I want to use Ruby on Rails to response the query result with the equivalent performance as psycopg2 does.

I use psycopg2 Python package to query my database (PostgreSQL),

No matter how many results in the response, (eg. 2 million)

I can get it much faster than in Ruby on Rails with JSON API.

  self.cur.execute( """ SELECT * FROM products """ for result in self.cur.fetchall(): print(result) 

I wonder how to to make the similar behavior in Rails but not to be too slow.

I want to provider user a WEBAPI, such as http://SERVER/get_users then user can get the response in any format (I don't care what the format is. The faster the better)

Ruby on Rails uses multi_json gem by default to render JSON, and multi_json uses several engines, being Oj one of the fastest.

If you want to render a set of records, you can do it like this in a controller:

render json: Product.all

But 2 million records is a huge amount of data, normally you would use pagination o some criteria to filter the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM