Three equivalent relational algebra queries corresponding to this SQL statement are:
(1) σ(position=‘Manager’) ∧ (city=‘London’) ∧ (Staff.branchNo=Branch.branchNo)(Staff × Branch)
(2) σ(position=‘Manager’) ∧ (city=‘London’)(Staff 1Staff.branchNo=Branch.branchNo Branch)
(3) (σposition=‘Manager’(Staff)) 1Staff.branchNo=Branch.branchNo (σcity=‘London’(Branch))
For the purposes of this example, we assume that there are 1000 tuples in Staff, 50 tuples
in Branch, 50 Managers (one for each branch), and 5 London branches. We compare these
three queries based on the number of disk accesses required. For simplicity, we assume
that there are no indexes or sort keys on either relation, and that the results of any intermediate
operations are stored on disk. The cost of the final write is ignored, as it is the
same in each case. We further assume that tuples are accessed one at a time (although in
practice disk accesses would be based on blocks, which would typically contain several
tuples), and main memory is large enough to process entire relations for each relational
algebra operation.
The first query calculates the Cartesian product of Staff and Branch, which requires
(1000 + 50) disk accesses to read the relations, and creates a relation with (1000 * 50)
tuples. We then have to read each of these tuples again to test them against the selection
predicate at a cost of another (1000 * 50) disk accesses, giving a total cost of:
(1000 + 50) + 2*(1000 * 50) = 101 050 disk accesses
The second query joins Staff and Branch on the branch number branchNo, which again
requires (1000 + 50) disk accesses to read each of the relations. We know that the join of
the two relations has 1000 tuples, one for each member of staff (a member of staff can only
work at one branch). Consequently, the Selection operation requires 1000 disk accesses to
read the result of the join, giving a total cost of:
2*1000 + (1000 + 50) = 3050 disk accesses
The final query first reads each Staff tuple to determine the Manager tuples, which
requires 1000 disk accesses and produces a relation with 50 tuples. The second Selection
operation reads each Branch tuple to determine the London branches, which requires 50
disk accesses and produces a relation with 5 tuples. The final operation is the join of the
reduced Staff and Branch relations, which requires (50 + 5) disk accesses, giving a total
cost of:
1000 + 2*50 + 5 + (50 + 5) = 1160 disk accesses
Clearly the third option is the best in this case, by a factor of 87:1. If we increased the
number of tuples in Staff to 10 000 and the number of branches to 500, the improvement
would be by a factor of approximately 870:1. Intuitively, we may have expected this as the
Cartesian product and Join operations are much more expensive than the Selection operation,
and the third option significantly reduces the size of the relations that are being joined
together. We will see shortly that one of the fundamental strategies in query processing is
to perform the unary operations, Selection and Projection, as early as possible, thereby
reducing the operands of any subsequent binary operations.
สามคำสั่งเทียบเท่าพีชคณิตเชิงสัมพันธ์สอดคล้องกับคำสั่ง SQL นี้:
(1) σ (ตำแหน่ง = 'จัดการ') ∧ (เมือง = 'ลอนดอน') ∧ (Staff.branchNo = Branch.branchNo) (พนักงาน×สาขา)
(2) σ (ตำแหน่ง = 'จัดการ') ∧ (เมือง = 'ลอนดอน') (พนักงาน 1Staff.branchNo = Branch.branchNo สาขา)
(3) (σposition = 'จัดการ' (พนักงาน)) 1Staff.branchNo = Branch.branchNo (σcity = ' ลอนดอน (Branch))
สำหรับจุดประสงค์ของตัวอย่างนี้เราสมมติว่ามี 1,000 สิ่งอันดับในพนักงาน 50 สิ่งอันดับ
ในสาขา 50 ผู้จัดการ (สำหรับแต่ละสาขา) และ 5 สาขาลอนดอน เราเปรียบเทียบทั้ง
สามคำสั่งขึ้นอยู่กับจำนวนของดิสก์ที่ต้องการเข้าถึง สำหรับความเรียบง่ายเราคิด
ว่ามีดัชนีหรือค้นหากุญแจในความสัมพันธ์ทั้งสองและว่าผลของกลางใด ๆ
การดำเนินงานจะถูกเก็บไว้บนดิสก์ ค่าใช้จ่ายในการเขียนขั้นสุดท้ายจะถูกละเว้นมันเป็น
เหมือนกันในแต่ละกรณี เรายังคิดว่าสิ่งอันดับมีการเข้าถึงในเวลาหนึ่ง (แม้ว่าใน
ทางปฏิบัติเข้าถึงดิสก์จะขึ้นอยู่กับบล็อกซึ่งโดยทั่วไปแล้วจะมีหลาย
สิ่งอันดับ) และหน่วยความจำมีขนาดใหญ่พอที่จะดำเนินการความสัมพันธ์ทั้งหมดสำหรับแต่ละเชิง
พีชคณิตดำเนิน
การสอบถามครั้งแรก คำนวณผลิตภัณฑ์ Cartesian ของพนักงานและสาขาที่ต้องใช้
(1000 + 50) ดิสก์เข้าถึงการอ่านความสัมพันธ์และสร้างความสัมพันธ์กับ (1000 * 50)
สิ่งอันดับ แล้วเราจะต้องอ่านแต่ละสิ่งอันดับเหล่านี้อีกครั้งเพื่อทดสอบพวกเขากับการเลือก
คำกริยาที่ค่าใช้จ่ายของคนอื่น (1000 * 50) ดิสก์เข้าถึงให้ต้นทุนรวมของ:
(1000 + 50) + 2 * (1000 * 50) = 101 050 ดิสก์เข้าถึง
แบบสอบถามที่สองร่วมกับพนักงานและสาขาในจำนวนสาขา branchNo อีกซึ่ง
ต้องใช้ (1000 + 50) ดิสก์เข้าถึงการอ่านแต่ละความสัมพันธ์ เรารู้ว่าการเข้าร่วมของ
ทั้งสองความสัมพันธ์ที่มี 1,000 สิ่งอันดับหนึ่งสำหรับสมาชิกของพนักงานแต่ละคน (สมาชิกของทีมเท่านั้นที่สามารถ
ทำงานที่สาขาหนึ่ง) ดังนั้นการดำเนินการคัดเลือกต้องมี 1,000 ดิสก์เข้าถึง
อ่านผลมาจากการเข้าร่วมให้ต้นทุนรวมของการ:
2 * 1000 + (1000 + 50) = 3050 ดิสก์เข้าถึง
แบบสอบถามสุดท้ายแรกอ่านแต่ละพนักงาน tuple เพื่อตรวจสอบสิ่งอันดับผู้จัดการ ซึ่ง
ต้องมีการเข้าถึงดิสก์ 1000 และสร้างความสัมพันธ์กับ 50 สิ่งอันดับ เลือกที่สอง
การดำเนินการอ่านแต่ละ tuple สาขาเพื่อตรวจสอบสาขาลอนดอนซึ่งต้อง 50
เข้าถึงดิสก์และสร้างความสัมพันธ์กับ 5 สิ่งอันดับ การดำเนินการขั้นสุดท้ายจะเข้าร่วมในการ
ลดพนักงานและสาขาความสัมพันธ์ซึ่งจะต้องมี (50 + 5) การเข้าถึงดิสก์ให้รวม
ค่าใช้จ่ายใน:
1,000 + 2 * 50 + 5 + (50 + 5) = 1,160 ดิสก์เข้าถึง
อย่างชัดเจนที่สาม เป็นตัวเลือกที่ดีที่สุดในกรณีนี้โดยปัจจัยที่ 87: 1 ถ้าเราเพิ่ม
จำนวนของสิ่งอันดับในพนักงาน 10 000 และจำนวนของสาขาที่ 500, การปรับปรุง
จะเป็นโดยปัจจัยที่ประมาณ 870: 1 สังหรณ์ใจเราอาจจะคาดหวังนี้เป็น
ผลิตภัณฑ์ Cartesian และเข้าร่วมการดำเนินงานมีมากมีราคาแพงกว่าการดำเนินการเลือก,
และตัวเลือกที่สามอย่างมีนัยสำคัญช่วยลดขนาดของความสัมพันธ์ที่มีการร่วม
กัน เราจะได้เห็นในไม่ช้าว่าหนึ่งในกลยุทธ์ที่สำคัญในการประมวลผลการสอบถามเป็น
ที่จะดำเนินการการดำเนินงานเอกเลือกและฉายอย่างเร็วที่สุดเท่าที่เป็นไปได้ซึ่งจะช่วย
ลดการถูกดำเนินการของการดำเนินงานไบนารีใดภายหลัง
การแปล กรุณารอสักครู่..
