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

时间:2009-09-21 17:08:48

标签: php mysql multiple-select

$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次往返。

您将如何优化此案例?

2 个答案:

答案 0 :(得分:1)

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

除此之外,如果不知道您需要什么数据以及它们之间的关系,那就不用说了。

答案 1 :(得分:0)

你可以试试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,这不是我的专业领域:)