【Rails学习笔记】includes 和 joins

includes

includes采用使用left outer join
A.includes(:B) 就是将B model的attribute一次性加载到A中,使得A也可以访问B的attribute,如

Plan.includes(:schedules).where(schedules: { day: 1 })
"SELECT `plans`.`id` AS t0_r0, `plans`.`manager_id` AS t0_r1, `plans`.`planner_id` AS t0_r2, `plans`.`creator_id` AS t0_r3, `plans`.`cost` AS t0_r4, `plans`.`fee` AS t0_r5, `plans`.`old_destination` AS t0_r6, `plans`.`budget` AS t0_r7, `plans`.`travel_time` AS t0_r8, `plans`.`departure_date` AS t0_r9, `plans`.`created_at` AS t0_r10, `plans`.`updated_at` AS t0_r11, `plans`.`return_date` AS t0_r12, `plans`.`travel_days` AS t0_r13, `plans`.`grades` AS t0_r14, `plans`.`cover_pic` AS t0_r15, `plans`.`route_pic` AS t0_r16, `plans`.`dedicate` AS t0_r17, `plans`.`title` AS t0_r18, `plans`.`subtitle` AS t0_r19, `plans`.`requirements` AS t0_r20, `plans`.`uuid` AS t0_r21, `plans`.`orderer_id` AS t0_r22, `plans`.`cancel_reason` AS t0_r23, `plans`.`country_ids` AS t0_r24, `plans`.`destination_id` AS t0_r25, `plans`.`service` AS t0_r26, `plans`.`state` AS t0_r27, `plans`.`deposit_contract_amount` AS t0_r28, `plans`.`formal_contract_amount` AS t0_r29, `plans`.`signed_deposit_contract_at` AS t0_r30, `plans`.`signed_formal_contract_at` AS t0_r31, `plans`.`deleted_at` AS t0_r32, `plans`.`estimated_departure_date` AS t0_r33, `plans`.`idea` AS t0_r34, `plans`.`user_id` AS t0_r35, `plans`.`label` AS t0_r36, `plans`.`explicit_signed_deposit_contract` AS t0_r37, `plans`.`total_average_user_rate` AS t0_r38, `plans`.`guidebook_planner_id` AS t0_r39, `plans`.`is_send_notify` AS t0_r40, `plans`.`next_contact_at_serialized` AS t0_r41, `plans`.`imported_at` AS t0_r42, `plans`.`contact_priority` AS t0_r43, `plans`.`planner_comment` AS t0_r44, `plans`.`manager_comment` AS t0_r45, `plans`.`is_performance_cancel` AS t0_r46, `plans`.`advisor_id` AS t0_r47, `plans`.`category` AS t0_r48, `plans`.`from_plan_id` AS t0_r49, `plans`.`advisor_comment` AS t0_r50, `plans`.`deposit_contract_lock_days` AS t0_r51, `plans`.`from_plan_type` AS t0_r52, `plans`.`type` AS t0_r53, `plans`.`prepaid_idea` AS t0_r54, `plans`.`is_free_customize` AS t0_r55, `plans`.`initial_discount` AS t0_r56, `plans`.`initial_discount_reason` AS t0_r57, `plans`.`guidebook_title` AS t0_r58, `plans`.`en_dedicate` AS t0_r59, `plans`.`next_destination_id` AS t0_r60, `plans`.`place_ids` AS t0_r61, `plans`.`booking_count` AS t0_r62, `plans`.`ticket_id` AS t0_r63, `plans`.`actual_tailor_days` AS t0_r64, `plans`.`is_dist` AS t0_r65, `plans`.`is_audited` AS t0_r66, `plans`.`final_balance` AS t0_r67, `plans`.`distributor_id` AS t0_r68, `plans`.`is_followed` AS t0_r69, `plans`.`verifier_id` AS t0_r70, `plans`.`verified_at` AS t0_r71, `plans`.`deprecater_id` AS t0_r72, `plans`.`deprecated_at` AS t0_r73, `plans`.`departure_place_id` AS t0_r74, `plans`.`cloned_at` AS t0_r75, `plans`.`is_audit_printed` AS t0_r76, `plans`.`sub_type` AS t0_r77, `plans`.`plus_package_price` AS t0_r78, `plans`.`deposit_payment_countdown` AS t0_r79, `plans`.`formal_payment_countdown` AS t0_r80, `plans`.`ordering_countdown` AS t0_r81, `plans`.`suspend_reason` AS t0_r82, `plans`.`deposit_received_at` AS t0_r83, `plans`.`full_received_at` AS t0_r84, `plans`.`deleter_id` AS t0_r85, `plans`.`full_profit_rate` AS t0_r86, `schedules`.`id` AS t1_r0, `schedules`.`plan_id` AS t1_r1, `schedules`.`day` AS t1_r2, `schedules`.`date` AS t1_r3, `schedules`.`node` AS t1_r4, `schedules`.`created_at` AS t1_r5, `schedules`.`updated_at` AS t1_r6, `schedules`.`remark` AS t1_r7, `schedules`.`images` AS t1_r8, `schedules`.`pic` AS t1_r9, `schedules`.`completeness` AS t1_r10, `schedules`.`daily_images` AS t1_r11, `schedules`.`deleted_at` AS t1_r12, `schedules`.`reference_count` AS t1_r13, `schedules`.`error_message` AS t1_r14, `schedules`.`deleter_id` AS t1_r15 FROM `plans` LEFT OUTER JOIN `schedules` ON `schedules`.`plan_id` = `plans`.`id` AND `schedules`.`deleted_at` IS NULL WHERE `plans`.`deleted_at` IS NULL AND `schedules`.`day` = 1"

将整个plan表和schedule表所有字段全部加载,这样如果后面需要多次调用,则不用再进行查询。

joins

但如果我不想将整个table载入,而只是想载入相关字段,即可使用joins,即inner join
joins采用lazy load,通过table之间关系只load 两个表非null的字段,但是后面每次使用我都要重新query。

Plan.joins(:schedules).where(schedules: { day: 1 })
"SELECT `plans`.* FROM `plans` INNER JOIN `schedules` ON `schedules`.`plan_id` = `plans`.`id` AND `schedules`.`deleted_at` IS NULL WHERE `plans`.`deleted_at` IS NULL AND `schedules`.`day` = 1"

总结一下

如果需要多次夸表调用,includes是一种先进的语法,否则joins更加高效一些

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容