在这篇文章中:SQL Query to get the data。
第一个答案是:
SELECT students.student_id,student_name,father_name,mother_name,
COUNT(student_addresses.student_id) AS total_addresses,
COUNT(student_phones.student_id) AS total_phones
FROM students,student_phones,student_addresses
WHERE students.student_id = student_phones.student_id AND
students.student_id = student_addresses.student_id AND
students.student_id = 7
GROUP BY BY students.student_id,student_name,father_name,mother_name;
而第二个是:
SELECT s.student_id,
max(s.student_name) student_name,
max(s.father_name) father_name,
max(s.mother_name) mother_name,
COUNT(distinct a.student_address_id) total_addresses,
COUNT(distinct p.student_phone_id) total_phones
FROM students s
LEFT JOIN student_phones p ON s.student_id = p.student_id
LEFT JOIN student_addresses a ON s.student_id = a.student_id
WHERE s.student_id = 7
GROUP BY s.student_id
现在,问题是:在性能方面,这两个查询之间是否存在显着差异?使用 是否MAX()
会影响第二个查询的执行时间?
我尝试用谷歌搜索答案,但没有运气。我想要一个清晰而具体的解释。