Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以结构化的方式显示连接的mysql数据,而不损失性能_Php_Mysql_Performance_Database Performance - Fatal编程技术网

Php 以结构化的方式显示连接的mysql数据,而不损失性能

Php 以结构化的方式显示连接的mysql数据,而不损失性能,php,mysql,performance,database-performance,Php,Mysql,Performance,Database Performance,在db中,有一些项目。每个项目都有文件。每个文件都有多个版本 所以理论上,我可以这样做(伪) 但这将意味着mysql查询的负载越来越大,其中只需要一个查询 因此,我加入以下问题: $sql = "SELECT projectfiles.*, fileversions.*, projects.id as projects_id FROM projects"; $sql .= " LEFT JOIN". " (SELECT projectfiles.id

在db中,有一些项目。每个项目都有文件。每个文件都有多个版本

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

但这将意味着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";
        }
    }
}
它给出了输出:

但我不太确定这样做是否能获得任何性能,因为在连接的行中有大量重复数据,而且如果数据库中的内容发生变化,那么一次更新代码肯定会有很多工作要做

我想简而言之;这感觉就像是一个肮脏的解决方案,我相信它是存在的


有更好的解决方案吗?

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


看看PHP,它是一种
->fetchAssoc()
方法(),可以用简洁的语法完成所有需要的操作。

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


看看PHP,它是一种
->fetchAssoc()
方法(),可以用简洁的语法完成所有需要的操作。

我想说,您的性能取决于查询的方式和表结构(这更重要!),如果您在性能方面需要一些帮助,您忘记了显示表的结构,请您展示一下“show create table tablename”和“explain all_your_query_with_join”?我想说,您的性能将取决于查询的生成方式和表结构(这更重要!),如果您在性能方面需要一些帮助,您忘记了显示表的结构,您能展示一下什么是“show create table tablename”和“explain all_your_query_with_join”吗??
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";
        }
    }
}