鉴于以下查询,如何对其进行优化以使子查询不依赖?
SELECT DISTINCT
inst.id, inst.name, inst.state, inst.farm_status,
(SELECT COUNT(inst_note.id)
FROM project_institution_note AS inst_note
WHERE inst_note.institution_id = inst.id) AS inst_note_count,
(SELECT COUNT(c.id) FROM project_catalog AS c
WHERE c.institution_id = inst.id
AND c.status = 0
AND c.catalog_type BETWEEN 0 AND 1) AS ug_count,
(SELECT COUNT(c.id) FROM project_catalog AS c
WHERE c.institution_id = inst.id
AND c.status = 0
AND c.catalog_type BETWEEN 1 AND 2) AS grad_count,
(SELECT COUNT(c.id) FROM project_catalog AS c
WHERE c.institution_id = inst.id
AND c.status = 0 AND c.catalog_type >= 3) AS alt_count,
(SELECT COUNT(c.id) FROM project_catalog_note AS cn
INNER JOIN farmtool_catalog AS c
ON c.id = cn.catalog_id
WHERE c.institution_id = inst.id) AS catalog_note_count,
(SELECT inst_note.text FROM project_institution_note AS inst_note
LEFT JOIN project_institution AS inst
ON inst_note.institution_id = inst.id
WHERE inst_note.institution_id = inst.id
ORDER BY inst_note.date DESC
LIMIT 1) AS latest_note
FROM project_institution AS inst
LEFT JOIN project_institution_note AS inst_note
ON inst.id = inst_note.institution_id
LEFT JOIN project_catalog AS c
ON inst.id = c.institution_id
WHERE LOWER(inst.state) = "me";
我已尝试将第一个子查询重构为INNER JOIN
,如此:
INNER JOIN (SELECT COUNT(inst_note.id)
FROM project_institution_note AS inst_note
GROUP BY inst_note.institution_id) inst_note_count
ON inst_note.institution_id = inst.id
并在最后LEFT JOIN
次操作后包含它,但返回空结果。
对我来说特别感兴趣的是优化计算ug_count
和grad_count
的第二和第三子查询。两者之间的唯一区别是第一个取决于(0,1)之间的字段值,以及(1,2)之间的第二个字段值。
现在,此查询运行正常,并且处于低使用率方案。不过,它显然效率很低,所以我希望尽可能优化。
答案 0 :(得分:0)
这应该可以帮助你解决问题。
<div class="twelve wide stretched column" >
<div class="ui tab segment active" data-tab="a">
<div class="ui form">
<div class="field">
<label>Please Select Classroom</label>
<select class="ui search dropdown" id="classroom">
<option value="">Select Classroom</option>
';
$faculty_id = $_SESSION["faculty_id"];
$class = "select * from class where faculty=$faculty_id";
$result = $conn->query($class);
if ($result->num_rows > 0) {
while ($fill = $result->fetch_assoc()) {
$class_id = $fill['class_no'];
$name = $fill['cls_name'];
echo "<option value=$class_id>$name</option>";
}
}
echo '
</select>
</div>
';
echo'
<div class="ui basic segment" style="display: none;" id="segment_course">
<div class="field">
<label>Please Select Course</label>
<select class="ui search dropdown" id="course">
<option value="">Select Course</option>
';
$faculty_id = $_SESSION["faculty_id"];
$class = "select * from course where faculty=$faculty_id";
$result = $conn->query($class);
if ($result->num_rows > 0) {
while ($fill = $result->fetch_assoc()) {
$course_id = $fill['code_section'];
$course_name = $fill['name'];
echo "<option value=$course_id>$course_id - $course_name</option>";
}
}
echo '
</select>
</div>
</div>
<div class="ui basic segment" style="display: none;" id="segment_time">
<div class="field">
<label>Please Select Course Start Time</label>
<select class="ui search dropdown" id="time">
<option value="">Select Start Time</option>
<option value="1">9:00-10:00</option>
<option value="2">10:00-11:00</option>
<option value="3">11:00-12:00</option>
<option value="4">12:00-13:00</option>
<option value="5">13:00-14:00</option>
<option value="6">14:00-15:00</option>
<option value="7">15:00-16:00</option>
<option value="8">16:00-17:00</option>
<option value="9">17:00-18:00</option>
<option value="10">18:00-19:00</option>
<option value="11">19:00-20:00</option>
<option value="12">20:00-21:00</option>
</select>
</div>
</div>
<div class="ui basic segment" style="display: none;" id="segment_duration">
<div class="ui field">
<label>Please Enter Course Duration</label>
<div class="ui fluid input">
<input type="text" placeholder="Duration of Course" id="duration">
</div>
</div>
</div>
</div>
</div>
不确定您是否可以采取任何有关获取最新笔记的信息。在Sql-Server中,我会使用cte和window函数,但这些东西在MySql中不可用。我希望无论如何这都很有用。