Skip to content

EarlyZhao/selectable_column_sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

preload动态选择select的字段

背景

在执行类似

User.where(id: 1).includes([:introduction,{:organizations=> :products}, wife: [:father, :mother] ])

的过程中, 对应的Model的字段会被全部提取出来。 这个在数据量比较大的情况下是非常浪费资源的。

这个简单的demo通过一种方式实现只提取关联Model指定的字段。在某些场景下可用作性能优化。 具体的情景分析请看这篇文章

使用方法

  1. 将 preload_patch.rb 在 config/initializers 文件夹中, 文件可在本demo的config/initializers 找到.

  2. 具体示例见 app/services/selectable_columns/test.rb

Have a try

cd this_demo_path
bundle install
rake db:migrate
rake data_creation:create_test_data
rails c

excute:

# prelod all associations data at once
SelectableColumn::Test.user_selectable

# preload in batchs
SelectableColumn::Test.user_selectable_batchs

And, you can see the results from the logs, if the development mode on.

局限

  • 不能再进行懒加载了
  • 在提取通过through定义的关联关系时,该机制不起作用,仍然会提取全部字段

About

加载关联关系时,只提取指定的字段

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published