我有两种模式:
class Transaction < ActiveRecord::Base
self.table_name = 'DEPOSIT_TRANSACTIONS'
belongs_to :deposit_account
scope :not_test, -> { includes(:deposit_account).where("deposit_accounts.is_test_account is null or deposit_accounts.is_test_account != 1").references(:deposit_account) }
...
end
class DepositAccount < ActiveRecord::Base
has_many :transactions, -> { order(:creation_date => :desc) }
default_scope { where(:purpose => 'COUNTERFEIT') }
scope :not_test, -> { where("is_test_account != 1") }
...
end
我试图用以下方式加载这两个表:
irb(main):008:0> request_transactions = Transaction.eager_load(:deposit_account)
**I polish the following output**
SQL (6390.0ms) SELECT "DEPOSIT_TRANSACTIONS"."ID" AS t0_r0,
"DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID" AS t0_r1,
"DEPOSIT_TRANSACTIONS"."TRANSACTION_TYPE" AS t0_r2,
"DEPOSIT_TRANSACTIONS"."TRACKING_ID" AS t0_r3,
"DEPOSIT_TRANSACTIONS"."AMOUNT" AS t0_r4,
"DEPOSIT_TRANSACTIONS"."STATUS" AS t0_r5,
"DEPOSIT_TRANSACTIONS"."TRANSACTION_COMMENT" AS t0_r6,
"DEPOSIT_TRANSACTIONS"."NOTIFICATION_EMAILS" AS t0_r7,
"DEPOSIT_TRANSACTIONS"."CREATED_BY" AS t0_r8,
"DEPOSIT_TRANSACTIONS"."CREATION_DATE" AS t0_r9,
"DEPOSIT_TRANSACTIONS"."LAST_UPDATED_BY" AS t0_r10,
"DEPOSIT_TRANSACTIONS"."LAST_UPDATED_DATE" AS t0_r11,
"DEPOSIT_TRANSACTIONS"."FINISH_DATE" AS t0_r12,
"DEPOSIT_TRANSACTIONS"."PAYMENT_DATE" AS t0_r13,
"DEPOSIT_TRANSACTIONS"."MATCH_TYPE" AS t0_r14,
"DEPOSIT_TRANSACTIONS"."ADDITIONAL_INFO" AS t0_r15,
"DEPOSIT_ACCOUNTS"."ID" AS t1_r0,
"DEPOSIT_ACCOUNTS"."SELLER_ID" AS t1_r1,
"DEPOSIT_ACCOUNTS"."MARKETPLACE_ID" AS t1_r2,
"DEPOSIT_ACCOUNTS"."PURPOSE" AS t1_r3,
"DEPOSIT_ACCOUNTS"."TOTAL_DEPOSIT_BALANCE" AS t1_r4,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_DEPOSIT_REQUEST" AS t1_r5,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_REFUND_REQUEST" AS t1_r6,
"DEPOSIT_ACCOUNTS"."CREATED_BY" AS t1_r7,
"DEPOSIT_ACCOUNTS"."CREATION_DATE" AS t1_r8,
"DEPOSIT_ACCOUNTS"."LAST_UPDATED_BY" AS t1_r9,
"DEPOSIT_ACCOUNTS"."LAST_UPDATED_DATE" AS t1_r10,
"DEPOSIT_ACCOUNTS"."IS_TEST_ACCOUNT" AS t1_r11,
"DEPOSIT_ACCOUNTS"."RECORD_VERSION_NUMBER" AS t1_r12,
"DEPOSIT_ACCOUNTS"."OUTSTANDING_CONFISCATE_REQUEST" AS t1_r13
FROM "DEPOSIT_TRANSACTIONS"
LEFT OUTER JOIN "DEPOSIT_ACCOUNTS"
ON "DEPOSIT_ACCOUNTS"."ID" = "DEPOSIT_TRANSACTIONS"."DEPOSIT_ACCOUNT_ID"
AND "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT'
DepositAccount Load (235.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
DepositAccount Load (535.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 143 AND ROWNUM <= 1
DepositAccount Load (471.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 147 AND ROWNUM <= 1
DepositAccount Load (237.0ms) SELECT "DEPOSIT_ACCOUNTS".* FROM "DEPOSIT_ACCOUNTS" WHERE "DEPOSIT_ACCOUNTS"."PURPOSE" = 'COUNTERFEIT' AND "DEPOSIT_ACCOUNTS"."ID" = 138 AND ROWNUM <= 1
...
但它产生了如此多的SQL,这意味着我认为rails并不会急切地加载。我预计第一个条款就足够了。请帮助急切加载表格。感谢。
我的环境:
jruby 1.7
rails 4.0
EDIT1:
我试图在ruby 1.9 rails 4.0上运行相同的代码,热切的负载工作正常。所以我猜这是由JRuby引起的。还有其他人有这个问题吗?
不知道是否重要,我在ruby 1.9 env中使用activerecord-oracle_enhanced-adapter,但是在JDBC驱动程序中 JRuby的。数据库是oracle。
答案 0 :(得分:0)
通过将activerecord从activerecord-4.0.9升级到activerecord-4.1.5来解决问题。顺便说一句,我使用activerecord-jdbc-adapter-1.3.7作为适配器。