【发布时间】:2021-12-12 14:04:15
【问题描述】:
如果我有一个多态关联,比如:
create Transmission < ApplicationRecord
belongs_to :transmittable, polymorphic: true
end
create Contact < ApplicationRecord
has_many :transmissions, as: :transmittable
end
create Product < ApplicationRecord
has_many :transmissions, as: :transmittable
end
...那么下面使用transmittable_type的绑定变量...
Transmission.where(transmittable_type: 'Product')
当产品和联系人的传输数量之间存在较大偏差时,这可能是不可取的,因为缺少与 Oracle 的绑定变量窥视等效的方法可能会导致对结果基数的错误估计。
问题:在这种情况下有没有办法避免使用绑定变量,除了:
Transmission.where("transmittable_type = 'Product'")
...我不喜欢,因为需要在复杂的查询中识别 transmissions 的正确表别名?
我已将 plan_cache_mode 视为解决此问题的数据库解决方法,但它似乎没有提供解决方案。
【问题讨论】: