为了将大量数据加载到MySQL中,LOAD DATA INFILE是迄今为止最快的选择.不幸的是,虽然这可以以INSERT IGNORE或REPLACE的方式使用,但目前不支持ON DUPLICATE KEY UPDATE.
但是,ON DUPLICATE KEY UPDATE有优势REPLACE.当存在重复时,后者执行删除和插入.这为密钥管理带来了开销.此外,自动增量ID在替换时不会保持不变.
ON DUPLICATE KEY UPDATE使用LOAD DATA INFILE时如何模拟?
我用rake渲染一个非常庞大的站点地图HTML文件.不幸的是,当我迁移到rails 3时代码中断.我当前的代码如下所示:
@controller = ActionController::Base.new
@controller.request = ActionController::TestRequest.new
@controller.instance_eval do
@url = ActionController::UrlRewriter.new(request, {})
end
# collect data, open output file file
template = ERB.new(IO.read("#{RAILS_ROOT}/app/views/sitemap/index.html.erb"))
f.puts(template.result(binding))
Run Code Online (Sandbox Code Playgroud)
这段代码工作在2.3,但在Rails 3中断,因为url_for不再访问@controller,而是控制器.(我想这就是原因.)
undefined local variable or method `controller' for #<Object:0x3794c>
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in `url_for'
(erb):5
/Users/me/Documents/Projects/zvg2/lib/tasks/zvg.rake:452
Run Code Online (Sandbox Code Playgroud)
我也尝试创建一个ActionView来做到这一点:
av = ActionView::Base.new(Rails::Application::Configuration.new(Rails.root).view_path, {
# my assigns
}, @controller)
av.class_eval do
include ApplicationHelper
end
f.puts(av.render(:template => "sitemap/index.html"))
Run Code Online (Sandbox Code Playgroud)
但问题似乎是一样的,虽然ActionView :: Base.new需要我的控制器.
undefined local variable or method `controller' for nil:NilClass
/opt/local/lib/ruby/gems/1.8/gems/activesupport-3.0.1/lib/active_support/whiny_nil.rb:48:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:31:in `url_options'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.1/lib/action_view/helpers/url_helper.rb:99:in …Run Code Online (Sandbox Code Playgroud) 我有一个rake文件,它通过HTTP读取内容,我想使用Paperclip将加载的内容存储在Amazon S3上.当我提供本地文件时,它工作正常,但我想将内容设置为字符串并手动设置内容类型.
以下不起作用.未发出错误,数据库条目已更新,但在S3中未创建任何文件:
p.attachment = "Test"
p.attachment_file_name = "test.txt"
p.attachment_content_type = "text/plain"
p.attachment_file_size = "Test".size
p.attachment_updated_at = Time.now
p.save
Run Code Online (Sandbox Code Playgroud)
我想我可以用我的内容写一个临时文件,但这将是一个非常低效的解决方案.
我有一个模型优惠和另一个历史_offers,一个提供has_many historical_offers.
现在,我想急切地将一天的历史记录加载到一组优惠中(如果存在的话).为此,我认为我需要将这一天传递给ON子句,而不是WHERE子句,这样我就可以得到所有的优惠,也就是当给定日期没有历史_天赋时.
使用Offer.where(few_complex_conditions).includes(:historical_offers).where("historical_offers.day =?",Date.today)
我会的
SELECT * FROM offers
LEFT OUTER JOIN historical_offers
ON offers.id = historical_offers.offer_id
WHERE day = '2012-11-09' AND ...
Run Code Online (Sandbox Code Playgroud)
但是我希望在ON子句中有条件,而不是在WHERE子句中:
SELECT * FROM offers
LEFT OUTER JOIN historical_offers
ON offers.id = historical_offers.offer_id AND day = '2012-11-09'
WHERE ...
Run Code Online (Sandbox Code Playgroud)
我想我可以用特定日期的lambda条件来改变has_many定义,但是我如何传递日期呢?
或者我可以像这样编写连接mysqlf:
Offer.where(several_complex_conditions)
.joins(["historical_offers ON offers.id = historical_offers.offer_id AND day = ?", Date.today])
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能将其连接起来以便进行急切加载?
让活动记录在开发环境中的所有查询中使用 SQL_NO_CACHE 的简单方法是什么?
我想针对最坏情况的加载时间优化语句。希望这样做是有意义的,但是我在第一页点击时得到的查询非常慢,而下一次所有查询都非常快,因为服务器应该缓存它们。
我正在使用 mysql2 gem 0.3.11 和 Rails 3.2.3。
我正在尝试跟踪用户的当前,之前和所选(期末更改)订阅状态.
在每月结束时,如果chosen_subscription != current_subscription,current_subscription改变了.
class User
include Mongoid::Document
embeds_one :previous_subscription, class_name: "Subscription"
embeds_one :current_subscription, class_name: "Subscription"
embeds_one :chosen_subscription, class_name: "Subscription"
end
class Subscription
include Mongoid::Document
embedded_in :user
field :plan, type: String
field :credits, type: Integer
field :price_per_credit, type: BigDecimal
field :start, type: Date
field :end, type: Date
end
Run Code Online (Sandbox Code Playgroud)
Mongoid希望我以一种对我没有意义的方式更多地指定它:
Mongoid::Errors::AmbiguousRelationship:
Problem:
Ambiguous relations :previous_subscription, :current_subscription, :chosen_subscription defined on User.
Summary:
When Mongoid attempts to set an inverse document of a relation in memory, it needs to …Run Code Online (Sandbox Code Playgroud) 我想用扫地机过期碎片.执行sweeper回调,但对expire_fragment的调用什么都不做,因为(我假设)cache_configured?返回零.配置缓存并在我的模板中创建和使用片段(在日志中验证它).我究竟做错了什么?
application.rb中
config.cache_store = :mem_cache_store, "XXX.XXX.XXX.XXX", { # I use a real IP
:compress => true,
:namespace => "#{Rails.env}_r3"
}
config.active_record.observers = [:auction_sweeper, :address_sweeper]
Run Code Online (Sandbox Code Playgroud)
production.rb
config.action_controller.perform_caching = true
Run Code Online (Sandbox Code Playgroud)
auction_sweeper.rb
class AuctionSweeper < ActionController::Caching::Sweeper
observe Auction
def after_create(auction)
Rails.logger.info "AuctionSweeper.expire_details #{auction.id} #{cache_configured?.inspect}=#{perform_caching.inspect}&&#{cache_store.inspect}"
expire_fragment("auction/#{auction.reference_sid}")
end
end
Run Code Online (Sandbox Code Playgroud)
在日志文件中,cache_configured?是nil,perform_caching和cache_store也是如此.
AuctionSweeper.expire_details 12732 nil=nil&&nil
Run Code Online (Sandbox Code Playgroud)
所以我假设,我的片段没有过期,因为expire_fragment的代码读取:
文件actionpack/lib/action_controller/caching/fragments.rb,第87行
87: def expire_fragment(key, options = nil)
88: return unless cache_configured?
Run Code Online (Sandbox Code Playgroud) configuration memcached caching fragment-caching ruby-on-rails-3