Looker Explore: กำหนดความสัมพันธ์ของ join และตรวจสอบ fanout
ใน Looker Explore การมี `sql_on` อย่างเดียวไม่พอสำหรับกำหนด join อย่างปลอดภัย ให้ระบุ cardinality จาก view หลักของ Explore ไปยัง view ที่นำมา join และทำเครื่องหมายคีย์ที่ไม่ซ้ำจริงของแต่ละ view ด้วย `primary_key: yes` วิธีนี้ช่วยให้ Looker จัดการ measure ได้ถูกต้องเมื่อ join แบบหนึ่งต่อหลายทำให้แถวข้อมูลซ้ำ อย่างไรก็ตาม symmetric aggregates ขึ้นกับการรองรับของ database dialect และ metadata ที่ถูกต้อง จึงต้องตรวจสอบผลลัพธ์เสมอ
Looker Explore: กำหนดความสัมพันธ์ของ join และตรวจสอบ fanout
ใน Looker Explore การมี sql_on อย่างเดียวไม่พอสำหรับกำหนด join อย่างปลอดภัย ให้ระบุ cardinality จาก view หลักของ Explore ไปยัง view ที่นำมา join และทำเครื่องหมายคีย์ที่ไม่ซ้ำจริงของแต่ละ view ด้วย primary_key: yes วิธีนี้ช่วยให้ Looker จัดการ measure ได้ถูกต้องเมื่อ join แบบหนึ่งต่อหลายทำให้แถวข้อมูลซ้ำ อย่างไรก็ตาม symmetric aggregates ขึ้นกับการรองรับของ database dialect และ metadata ที่ถูกต้อง จึงต้องตรวจสอบผลลัพธ์เสมอ
Join ใน Explore กำหนดอะไร
ในไฟล์โมเดล LookML explore ระบุ view หลัก ส่วน join แต่ละรายการกำหนดการเชื่อม view อื่น Looker ใส่เฉพาะ join ที่จำเป็นต่อ field ที่เลือกลงใน SQL พารามิเตอร์หลักคือ sql_on (เงื่อนไข SQL ON), type (left_outer, inner, full_outer, cross; ค่าเริ่มต้น left_outer), relationship (one_to_one, many_to_one, one_to_many, many_to_many) และ primary_key: yes ในแต่ละ view ทิศทางความสัมพันธ์นับจาก view หลักไปยัง view ที่ join หากไม่ระบุ Looker ถือเป็น many_to_one ให้ระบุ cardinality จริงอย่างชัดเจน
ตัวอย่าง: เหตุใดยอดคำสั่งซื้อจึงเพิ่มเป็นสองเท่า
ตัวอย่างสมมติเพื่ออธิบาย ไม่ใช่ผลธุรกิจจริง: คำสั่งซื้อ 1 มียอด 100 และมีรายการสินค้า 2 แถว มูลค่า 40 กับ 60 SQL join แบบทั่วไปจะทำซ้ำแถวคำสั่งซื้อสำหรับแต่ละรายการ
| คำสั่งซื้อ | ยอดคำสั่งซื้อ | รายการ | มูลค่ารายการ |
|---|---|---|---|
| 1 | 100 | 501 | 40 |
| 1 | 100 | 502 | 60 |
หากรวมยอดคำสั่งซื้อที่ซ้ำโดยตรงจะได้ 200 แต่ยอดรายการรวมเป็น 100 นี่คือ fanout: หนึ่งแถวของตารางหนึ่งตรงกับหลายแถวของอีกตาราง Symmetric aggregates ของ Looker อาจป้องกันการนับซ้ำได้เมื่อคีย์และความสัมพันธ์ถูกต้องและ dialect รองรับ แต่แทนการทำโมเดลที่ถูกต้องไม่ได้ หากไม่รองรับหรือตั้งค่าผิด Looker อาจบล็อก measure หรือซ่อนจาก Explore
กำหนดคีย์และ cardinality
หาก orders เป็น Explore หลักและมี order_items หลายรายการต่อคำสั่งซื้อ ความสัมพันธ์คือ one_to_many ปรับ LookML ตัวอย่างให้ตรงกับ schema และ SQL dialect จริง:
view: orders {
dimension: id {
primary_key: yes
sql: ${TABLE}.id ;;
}
measure: total_revenue {
type: sum
sql: ${TABLE}.total ;;
}
}
view: order_items {
dimension: id {
primary_key: yes
sql: ${TABLE}.id ;;
}
dimension: order_id {
sql: ${TABLE}.order_id ;;
}
measure: item_revenue {
type: sum
sql: ${TABLE}.amount ;;
}
}
explore: orders {
join: order_items {
type: left_outer
relationship: one_to_many
sql_on: ${orders.id} = ${order_items.order_id} ;;
}
}orders.id และ order_items.id ต้องไม่ซ้ำและไม่เป็น null จริงในแต่ละ view หากต้องใช้หลาย field จึงจะไม่ซ้ำ ให้กำหนด compound key ที่แท้จริง อย่าทำเครื่องหมาย field ที่ซ้ำเป็น primary key เอกสารระบุว่าการใช้ symmetric aggregates กับ sum และ average ต้องมีคีย์ที่ถูกต้องใน view ที่เกี่ยวข้อง
ตรวจสอบ join
ตรวจ grain ของแต่ละ source table และนับคีย์ที่ซ้ำหรือเป็น null
พิจารณาทิศทางจาก Explore หลัก: orders ไป items มักเป็น `one_to_many`; กลับทิศมักเป็น `many_to_one` ตรวจด้วยข้อมูลจริง
Validate LookML ใน Development Mode แล้วเปิด Explore การเห็น field ไม่ได้ยืนยันว่ายอดถูกต้อง
Query measure จากทั้งสอง view ในตัวอย่างรายได้ orders ควรเป็น 100 และรายได้ items ก็ 100 เพิ่ม order ID เพื่อตรวจค่าต่อคำสั่งซื้อ
เปิดแท็บ SQL ของ Explore ตรวจ `FROM`, เงื่อนไข join, filter และ grouping ใช้ SQL Runner หรือ execution plan หากจำเป็น
หาก measure หาย ให้ตรวจคีย์ การรองรับ dialect และ `symmetric_aggregates`; หากยอดผิด ให้ตรวจทิศทาง ความไม่ซ้ำ เงื่อนไข join และ filter
นี่คือรายการตรวจตามเอกสาร ไม่ใช่การทดสอบกับ Looker instance ใดโดยเฉพาะ ตรวจความไม่ซ้ำของคีย์ในฐานข้อมูล และยืนยัน SQL กับผลลัพธ์ใน connection, dialect และ model ของคุณเอง ตรวจ filter ของ join และกฎสิทธิ์ผู้ใช้ที่มีผลต่อรายงานด้วย
แหล่งข้อมูล
[Working with joins in LookML](https://docs.cloud.google.com/looker/docs/working-with-joins?hl=en)
[Join parameters](https://docs.cloud.google.com/looker/docs/reference/param-explore-join)
[Getting the relationship parameter right](https://docs.cloud.google.com/looker/docs/best-practices/how-to-use-the-relationship-parameter-correctly?hl=en)
[Understanding symmetric aggregates](https://docs.cloud.google.com/looker/docs/best-practices/understanding-symmetric-aggregates)
[The symmetric_aggregates parameter](https://docs.cloud.google.com/looker/docs/reference/param-explore-symmetric-aggregates)
[The primary_key parameter](https://docs.cloud.google.com/looker/docs/reference/param-field-primary-key)
[Troubleshoot measures missing from a join](https://docs.cloud.google.com/looker/docs/best-practices/how-to-troubleshoot-when-measures-dont-come-through-a-join)
[How Looker generates SQL](https://docs.cloud.google.com/looker/docs/how-looker-generates-sql)
[Looker IDE overview](https://docs.cloud.google.com/looker/docs/looker-ide?hl=en) · [Looker glossary](https://docs.cloud.google.com/looker/docs/glossary?hl=en)