简体   繁体   English

ActiveRecord :: Relation转换为哈希缓慢

[英]ActiveRecord::Relation convert to hash slow

i query 5000 records from the database, then converted into Hash, 'marker_id' as key, if 'marker_id' is the same, put together into Array. 我从数据库中查询5000条记录,然后将其转换为Hash,将'marker_id'作为键,如果'marker_id'是相同的,则放到Array中。 but it very slow when convert to Hash. 但是转换为Hash时非常慢。

      total_attrs=MarkerAttr.where(marker_layer_id: layer.id)
      temp_attrs={}
      t=Time.now

      for ta in total_attrs
        if !temp_attrs.has_key?(ta.marker_id)
          temp_attrs["#{ta.marker_id}"]=[]
        end
        temp_attrs["#{ta.marker_id}"].push(ta)
      end
      p "to_hash_time::#{Time.now-t}"
      # TODO:: to_hash_time::1.563641124 

thanks 谢谢

Hmm. 嗯。 I'm wondering if Enumerable#group_by will be any faster. 我想知道Enumerable#group_by是否会更快。 Try this: 尝试这个:

 total_attrs = MarkerAttr.where(marker_layer_id: layer.id).all
 temp_attrs = total_attrs.group_by(&:marker_id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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