【问题标题】:Showing joined mysql data in a structured way without performance loss以结构化方式显示连接的 mysql 数据而不会损失性能
【发布时间】:2012-06-08 11:27:41
【问题描述】:

在数据库中,有项目。每个项目都有文件。每个文件都有多个版本。

所以理论上,我可以这样做(伪);

$res = mysql_query("SELECT * FROM projects");
while($row=mysql_fetch_assoc($res))
{
    echo "Project ".$row["id"]."<br/>";
    $res2 = mysql_query("SELECT * FROM projectfiles");
    while($row2=mysql_fetch_assoc($res2))
    {
        echo "FileID ".$row2["id"]."<br/>";
        $res3 = mysql_query("SELECT * FROM fileversions");
        while($row3=mysql_fetch_assoc($res3))
        {
            echo $row3["name"]."<br/>";
        }
        echo "<br/>";
    }
}

样本输出:

Project 1
    FileID 1
        test_file_1.txt.1

    FileID 2
        test_file_2.txt.1

    FileID 3
        test_file_3.txt.1
        test_file_3.txt.2

但这意味着加载和加载只需要一个的 mysql 查询。

所以我加入了查询:

$sql = "SELECT projectfiles.*, fileversions.*, 
    projects.id as projects_id
   FROM projects";

$sql .= " LEFT JOIN".
    " (SELECT 
        projectfiles.id as projectfiles_id,
        projectfiles.fileID as projectfiles_fileID,
        projectfiles.projectID as projectfiles_projectID
       FROM projectfiles
       ) AS projectfiles".
    " ON projects.id = projectfiles_projectID";


$sql .= " LEFT JOIN".
    " (SELECT 
        fileversions.id as fileversions_id,
        fileversions.name as fileversions_name,
        fileversions.location as fileversions_location,
        fileversions.fileID as fileversions_fileID
       FROM fileversions
       ) AS fileversions".
    " ON projectfiles.projectfiles_fileID = fileversions_fileID";

当然,现在这给我留下了非结构化数据:

所以我做的是:

while($row = mysql_fetch_assoc($res))
{
    $projectID = $row["projects_id"];
    $fileID = $row["projectfiles_fileID"];
    $fileversionID = $row["fileversions_id"];

    $fileversionsArray[$fileversionID] = array($row["fileversions_name"],$row["fileversions_location"]);
    $fileArray[$fileID][$fileversionID] = $fileversionID;
    $projectArray[$projectID][$fileID] = $fileID;
}

所以我可以这样表示:

foreach($projectArray as $projectID => $projectDatas)
{
    echo "Project ID: ".$projectID."\n";
    foreach($projectDatas as $fileID)
    {
        echo "\tFile ID: ".$fileID."\n";
        foreach($fileArray[$fileID] as $fileversionID)
        {
            echo "\t\tFile version name: ";
            echo $fileversionsArray[$fileversionID][0];
            echo "\n";
            echo "\t\tFile location: ";
            echo $fileversionsArray[$fileversionID][2];
            echo "\n";
        }
    }
}

它给出了输出:

但我不太确定这样做是否能获得任何性能,因为连接行中有很多重复数据,而且更新代码一次/如果数据库中的内容确实需要做很多工作变化。

我想简而言之;对于我认为存在的正确解决方案,这感觉就像是一种肮脏的解决方法。

有没有更好的解决方案?

【问题讨论】:

  • 我会说你的性能将取决于查询的生成方式和表结构(这更重要!),如果你需要一些关于性能方面的帮助,你忘了显示结构表,你能显示什么'show create table tablename'和'explain all_your_query_with_joins'吗??

标签: php mysql performance database-performance


【解决方案1】:

无法直接从 MySQL 数据库返回结构化数据。您将这些面向表格的数据转换为数组的努力是正确的。

看看 PHP dibi library-&gt;fetchAssoc() 方法 (doc) 可以用简洁的语法完成您需要的一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2014-11-30
    相关资源
    最近更新 更多