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

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -