我有一个铁路3.2应用程序 追踪订阅订单的邮件
基本模式结构是:
顺序有_ many 订阅有_ many 订阅有_ many 订阅帐号
每个月为每次订阅邮件制作一份记录,并从这些记录中导出一份Csv文件。
邮件地址按顺序存储。
基本上,我选择了所有对于该月邮寄有效的订阅,然后通过它们从订单对象获取邮件地址。然后,我为每个对象创建了新的订阅邮件记录。
现在这个工作正常,因为订阅数量不多, 但它相当缓慢。
我怎样才能加快这个过程?
我有一个铁路3.2应用程序 追踪订阅订单的邮件
基本模式结构是:
顺序有_ many 订阅有_ many 订阅有_ many 订阅帐号
每个月为每次订阅邮件制作一份记录,并从这些记录中导出一份Csv文件。
邮件地址按顺序存储。
基本上,我选择了所有对于该月邮寄有效的订阅,然后通过它们从订单对象获取邮件地址。然后,我为每个对象创建了新的订阅邮件记录。
现在这个工作正常,因为订阅数量不多, 但它相当缓慢。
我怎样才能加快这个过程?
经过一些研究后,我最后在一笔交易中总结了我的代码,而没有做任何其他修改。
它加快了事情的进度
在我添加交易之前,代码正在花费超过1分钟运行,现在降到大约10秒。这足够满足我的需求,所以我不再尝试优化。
ActiveRecord::Base.transaction do
# my db stuff here
end
为了优化, 您需要从 Ruby 级别下调到 SQL 级别 。
而不是做 N+1
选择( 1
获取所有订阅的 和
N
获取每次订阅的所有订单),你可能只能通过联合选择 1
。
SubscriptionMailing.
joins(:subscrtiption).
joins(:order).
where(Order.table_name => { valid: true })
听起来你想要用包括热切的装货
# Subscription.rb
scope :valid_for_month lambda {|month| where(month: month)}
# Elsewhere
valid_subscriptions = Subscription.valid_for_month(Time.now.month).includes(:order)
valid_subscriptions.each do |subscription|
subscription.generate_subscription_mailing
end
包括:http://api.rubyonrails.org/class/Acord/Acsociations/ClassMethods.html
collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...
We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...
As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...
I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...
I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml
guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...
I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...