ruby on rails - How to reduce hitting db multiple -


lets assume have hundred thousand users

simple example,

user = user.where(id: 1..10000)  user load (30.8ms)  select `users`.* `users`  (`users`.`id` between 1 , 10000) 

in here, want slice more this,

user.where(id: 100..1000) user load (2.9ms)  select `users`.* `users`  (`users`.`id` between 1 , 10000) , (`users`.`id` between 100 , 1000) 

why active record hitting db twice? has result has bigger data. why have hit db, not reuse , slice activerecord::relation?

is there solution this?

activerecord keeps track of queries , able cache duplicate requests, in case it's not immediate library understand second 1 subset of first.

moreover, there several reasons why generic library such activerecord may not want implement caching logic one. caching large data set in large application may result several mb of memory, , processes may reach memory limit of machine because garbage collector not able recollect memory.

long story short, it's bad idea implement such feature in generic orm library.

if want implement in own code, free it.


Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

php - Using str_replace to translate a MySQL Table in html -

SQL Server - MyCTE query based on 24 hour period (next day) -