php - 以结构化方式显示连接的 mysql 数据,且不会造成性能损失

标签 php mysql performance database-performance

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

所以理论上,我可以做到这一点(伪);

$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";

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

enter image description here

所以我所做的是:

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";
        }
    }
}

这给出了输出:

ex2

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

我想简而言之;这感觉像是一个肮脏的解决方法,我相信这是一个正确的解决方案。

有更好的解决方案吗?

最佳答案

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

看看 PHP dibi library->fetchAssoc() 方法 ( doc ) 可以用简洁的语法完成您需要的所有操作。

关于php - 以结构化方式显示连接的 mysql 数据,且不会造成性能损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10947952/

相关文章:

php - 根据插入请求自动从 SQL 表中删除旧记录

PHP 5.0 : Call to undefined function mysql_connect()

php - 我如何创建一个系统来计算上次看到的时间

c++ - 如何比较 C++ 中 log() 和 fp 除法的性能?

PHP header 存在但无法检索

php - 将带有数字键的数组转换为对象

mysql - 如何避免在此代码中在数据库中存储重复项?

php - MYSQL关注者系统: Indexing the Followers Table

php - 计算剩余天数

c# - 如果我在 for 循环 c# 中运行几次,为什么我运行一次方法几乎同时完成工作