Php 性能、sql重连接与多个小请求

Php 性能、sql重连接与多个小请求,php,mysql,Php,Mysql,我有以下Mysql数据库结构 [Table - Category1] [Table Category1 -> Category2 ] (One to N relation) [Table - Category2] [Table Category2 -> Item ] (One to N relation) [Table - Item] $arr[$i]['name'] = 'name of something in category1'; $arr[$i]['

我有以下Mysql数据库结构

[Table - Category1]

  [Table Category1 -> Category2 ] (One to N relation)  

[Table - Category2]

  [Table Category2 -> Item ] (One to N relation) 

[Table - Item]
$arr[$i]['name'] = 'name of something in category1';
$arr[$i]['data'][$j]['name'] = 'name of something in category2';
$arr[$i]['data'][$j]['data'][$k]['name'] = 'name of something in item';
我想在PHP中用以下结构将所有内容都放入一个数组中

[Table - Category1]

  [Table Category1 -> Category2 ] (One to N relation)  

[Table - Category2]

  [Table Category2 -> Item ] (One to N relation) 

[Table - Item]
$arr[$i]['name'] = 'name of something in category1';
$arr[$i]['data'][$j]['name'] = 'name of something in category2';
$arr[$i]['data'][$j]['data'][$k]['name'] = 'name of something in item';
因此,基本上我不知道我是应该像下面这样使用一个“重”sql请求和连接,还是使用迭代方法

$sql = 'SELECT id, name FROM category1';
$result = $mysqli->query($sql);
$arr = array();
$i = 0;
while ($arr[$i] = $result->fetch_assoc()) {
    $join = $mysqli->query('SELECT c2.id, c2.name FROM category2 c2 LEFT JOIN category1_to_category2 c1tc2 ON c2.id = c1tc2.id_category 2 WHERE c1tc2.id_category1 = '.$arr[$i]['id']);
    $j = 0;
    while ($arr[$i]['data'][$j] = $join->fetch_assoc())
      /* same request as above but with items */
    $i++;
}
加入请求

SELECT c1.name as c1name, c2.name as c2name, i.name 
FROM category1 c1 
LEFT JOIN category1_to_category2 c1tc2 ON c1.id = c1tc2.id_category1 
LEFT JOIN category2 c2 ON c1tc2.id_category2 = c2.id
LEFT JOIN category2_to_item c2ti ON c2.id = c2ti.id_category2
LEFT JOIN item i ON c2ti.id_item  = i.id
迭代法

$sql = 'SELECT id, name FROM category1';
$result = $mysqli->query($sql);
$arr = array();
$i = 0;
while ($arr[$i] = $result->fetch_assoc()) {
    $join = $mysqli->query('SELECT c2.id, c2.name FROM category2 c2 LEFT JOIN category1_to_category2 c1tc2 ON c2.id = c1tc2.id_category 2 WHERE c1tc2.id_category1 = '.$arr[$i]['id']);
    $j = 0;
    while ($arr[$i]['data'][$j] = $join->fetch_assoc())
      /* same request as above but with items */
    $i++;
}
迭代解决方案将产生大约10*20的请求,这对我来说似乎很重要,这就是为什么我会选择第一个解决方案(4个连接单个请求)。 但是,使用单请求解决方案时,我的阵列将是这样的

$arr[0]['c1name'];
$arr[0]['c2name'];
$arr[0]['iname'];
它需要一些PHP训练来获得我需要在HTML页面的选项卡中显示的所需数组。所以我的问题是,有一个大的SQL请求和一些PHP数组操作,还是有多个小的请求而没有PHP数组操作更好?我知道在大多数情况下,从SQL获取所有数据是一个更好的解决方案,但在这种情况下,我不确定。顺便说一下,我唯一要考虑的是网页的加载时间


提前感谢您的帮助=)。

通常更好,您的示例也不例外,让SQL server尽可能多地进行数据格式化和迭代,因为SQL server通常比普通编程语言更高效

此外,您正在减少服务器的查询负载,并且使用复杂联接有很好的理由


唯一的缺点是,复杂的SQL查询可能很难格式化和调试,如果还没有使用第三方SQL工具,我建议您使用一个。

要用摇摆回答(我同意),我建议您只执行一个查询,但要为c1name、c2name和iname中的每一个存储最后一个键。当这些更改发生时,您会增加相关的数组下标,并再次初始化较低级别的下标以构建数组

大概是这样的:-

<?php

$sql = "SELECT c1.name AS c1name, c2.name AS c2name, i.name  AS iname
        FROM category1 c1 
        LEFT JOIN category1_to_category2 c1tc2 ON c1.id = c1tc2.id_category1 
        LEFT JOIN category2 c2 ON c1tc2.id_category2 = c2.id
        LEFT JOIN category2_to_item c2ti ON c2.id = c2ti.id_category2
        LEFT JOIN item i ON c2ti.id_item  = i.id"

$result = $mysqli->query($sql);
$arr = array();
$i = 0;
$j = 0;
$k = 0;
$c1name = '';
$c2name = '';
$iname = '';
while ($row = $result->fetch_assoc()) 
{
    switch(true)
    {
        case $row['c1name'] != $c1name :
            $i++;
            $j = 0;
            $k = 0;
            $arr[$i]['name'] = $row['c1name'];
            $arr[$i]['data'][$j]['name'] = $row['c2name'];
            $arr[$i]['data'][$j]['data'][$k]['name'] = $row['iname'];
            break;
        case $row['c2name'] != $c2name :
            $j++;
            $k = 0;
            $arr[$i]['data'][$j]['name'] = $row['c2name'];
            $arr[$i]['data'][$j]['data'][$k]['name'] = $row['iname'];
            break;
        default :
            $k++;
            $arr[$i]['data'][$j]['data'][$k]['name'] = $row['iname'];
            break;
    }
    $c1name = $row['c1name'];
    $c2name = $row['c2name'];
    $iname = $row['iname'];
}