[英]How to properly optimise mysql select statement
I have a web page that is divided into different sections.我有一个分为不同部分的网页。 Each section has to show different results.
每个部分必须显示不同的结果。 These results are gotten from the database.
这些结果是从数据库中得到的。 This is a sample data on SQLfiddle
这是 SQLfiddle 上的示例数据
http://sqlfiddle.com/#!9/ad98b/1 http://sqlfiddle.com/#!9/ad98b/1
The following code is what comes to my mind but I'm afraid that it might somehow overload the server when this page is accessed multiple times by different people下面的代码是我想到的,但我担心当这个页面被不同的人多次访问时,它可能会以某种方式使服务器过载
$sectionA = $connect->query("SELECT * FROM Main_Section WHERE section = `A`
");
while ($row = $sectionA->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_a = $sec_result_a.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionB = $connect->query("SELECT * FROM Main_Section WHERE section = `B` ");
while ($row = $sectionB->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_b = $sec_result_b.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionC = $connect->query("SELECT * FROM Main_Section WHERE section = `C` ");
while ($row = $sectionC->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_c= $sec_result_c.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
UP TO section Z直到 Z 部分
Is there a way I can Optimise this properly?有什么方法可以正确优化吗?
Unless there's more to the picture, why not just query everything, ordered by section, to have the AZ:除非图片有更多内容,否则为什么不按部分排序查询所有内容以获得 AZ:
SELECT * FROM Main_Section ORDER BY section
... and then process the results with one loop, which could look something like this: ...然后用一个循环处理结果,它可能看起来像这样:
$sections = $connect->query("SELECT * FROM Main_Section ORDER BY section");
while ($row = $sections->fetch_array())
{
echo $row['section'] . ' ' . '<p>' . $row['id'] . '</p><h3>' . $row['firstname'] . ' ' . $row['lastname'] . '</h3>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.