在这种情况下,您将如何优化 MySQL 性能?

发布于 2024-08-05 04:00:02 字数 538 浏览 9 评论 0原文

$query = "select * from a ...";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)
        $arr[0][] = $row;
}
$query = "select * from b ...";
if($result = mysql_query($query,$con))
{
    while($row = mysql_fetch_array($result);)
        $arr[1][] = $row;
}
$query = "select * from c ...";
if($result = mysql_query($query,$con))
{
    while($row = mysql_fetch_array($result);)
        $arr[2][] = $row;
}

这 3 个查询检索具有不同结构的多行, 3次查询需要3次往返, 10 个查询将需要 10 个往返。

您将如何优化这个案例?

$query = "select * from a ...";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)
        $arr[0][] = $row;
}
$query = "select * from b ...";
if($result = mysql_query($query,$con))
{
    while($row = mysql_fetch_array($result);)
        $arr[1][] = $row;
}
$query = "select * from c ...";
if($result = mysql_query($query,$con))
{
    while($row = mysql_fetch_array($result);)
        $arr[2][] = $row;
}

The 3 queries retrieves multiple rows with different structure,
3 queries need 3 round-trip,
10 queries will need 10 round-trip.

How will you optimize this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-08-12 04:00:02

选择您实际需要的列而不是 select *

除此之外,在不知道您需要什么数据以及它们如何关联的情况下,没什么好说的。

Select the columns you actually need instead of select *

Other than that, not much to say without knowing what data you need and how they relate.

若能看破又如何 2024-08-12 04:00:02

你可以尝试 UNION:

$query = "SELECT 'a' AS tablename, a.* FROM a
UNION
SELECT 'b' AS tablename, b.* FROM b
UNION
SELECT 'c' AS tablename, c.* FROM c";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)

switch ($row['tablename']) {
    case "a":
        $arr[0][] = $row;
        break;
    case "b":
$arr[1][] = $row;
        break;
    case "c":
        $arr[2][] = $row;
        break;
}
}

请原谅我的 php,这不是我的专业领域:)

You could try a UNION:

$query = "SELECT 'a' AS tablename, a.* FROM a
UNION
SELECT 'b' AS tablename, b.* FROM b
UNION
SELECT 'c' AS tablename, c.* FROM c";
if($result = mysql_query($query,$con))
{while($row = mysql_fetch_array($result);)

switch ($row['tablename']) {
    case "a":
        $arr[0][] = $row;
        break;
    case "b":
$arr[1][] = $row;
        break;
    case "c":
        $arr[2][] = $row;
        break;
}
}

pardon my php, it's not my area of expertise :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文