PHP SQL 多 Table 分层 foreach 内 foreach 内 foreach 仅来自 1 层以上的引用

PHP SQL Multi Table layered foreach within foreach within foreach with reffence from only 1 level above

我需要在下面的两种不同模式下 运行 因此 "if "(第二个稍后构建一个大的 csv)

低于 运行s 单个实例正常,但第二个 (*) 的加载时间下降,因为前 7k 行中的每一行都为 运行s。

我想避免可怕的

 set_time_limit(0);

我也在每个阶段挑选一些内容,因为这些内容稍后也会用到。

所以问题是实现以下最快的方法是什么?

可能是一个 SQL 语句和定义值的提取,如

table_c.tree , $row['apple'] 

Table结构

Table A 有 7k 行,其中 $row['Code'] 可以重复 10 次

Table B 有 10k 行,其中 $row['Group'] 可以重复 5 次

Table c 有 2k 行,其中 $row['Group2'] 可以重复 5 次

 if (isset($_POST["go"])) { $where = 'WHERE xxxxx = 1';} else{$where = 'WHERE xxxxx = "'.$selected_val[1].'"';}

$stmt = $conn->prepare(" SELECT *  FROM table_A $where "); $stmt->execute();

    foreach ($stmt as $row) {
                $Code = $row['Code'];

                $stmt1 = $conn->prepare(" SELECT *  FROM table_B WHERE Code = '$Code' "); $stmt1->execute();

                    foreach ( $stmt1 as $row1 ) {
                        if ($row['apple']) {$apple = $row['apple'];} // used to stop running rest all the time and also set $ for use later
                        else {$group1 = $row1['Group'];

                                $stmt2 = $conn->prepare(" SELECT *  FROM table_c WHERE group2 = '$group1' "); $stmt2->execute();                            

                                foreach ( $stmt2 as $row2 ) {$group2 =  $row2['group2'];}
                                    $stmt3 = $conn->prepare(" SELECT *  FROM table_d WHERE group3 = '$group2' "); $stmt3->execute();                            
                                        foreach ( $stmt3 as $row3 ) {

                                            $result [] = $row3['result'] 
                                        }
                                }   
                    }


        /// do things with 
        $result
        $Code
        $group1
        $row2['group2']
        $row['apple']
    }   

第一个 table 不是我的,所以我无法控制它们。

另一种方法可能是将一些新字段加载到 "Table_a" 中,但这只是将时间分配给其他功能。

来自@yoshiwaan

我是不是漏掉了速度技巧?如果我在开始时创建 "my_result_chain" 一次(因为所有数据都可以以很快的速度转储),那么只需调用 foreach 就可以非常快地 运行 。但是因为有一些 tables 不包括来自 table 上一层的所有引用,所以 returns null 而不是尽可能的踪迹。这可以通过使用 LEFT JOIN 来纠正,但显然会创建多个行,其中过滤键相同等等有没有办法选择哪一行/字段,DISTINCT 只抓取顶层,并且每个 table 没有唯一的键因为我需要从不同的 tables 获取各种数据?对于 ref 13 sec v .03sec 如果创建一次。如果我在 foreach 的每一行中添加一个键号但不是一个好的解决方案,则可以工作。

简短的提示:不要 select * 如果您不需要所有内容,只需 select 您想要的列即可。

冗长的版本:不要在代码中执行此操作,而是将逻辑卸载到数据库中的视图中。

这里有一个重要的例子(你需要向下滚动才能看到全部):

create table table_A
(
    xxxxxx int,
    Code    int
);

create table table_B
(
    Code int,
    `Group` varchar(20),
    apple varchar(20)
);

create table table_C
(
    `Group` varchar(20),
    group2  varchar(20)
);

create table table_D
(
    group3  varchar(20),
    result  varchar(20)
);

insert into table_A
values (1, 1);
insert into table_A
values (2, 2);
insert into table_A
values (3, 3);
insert into table_A
values (4, 4);

insert into table_B
values (1, 'snake', 'yes');
insert into table_B
values (1, 'lizard', '');
insert into table_B
values (2, 'canine', 'yes');
insert into table_B
values (2, 'feline', '');
insert into table_B
values (3, 'smallbird', 'yes');
insert into table_B
values (3, 'bigbird', '');
insert into table_B
values (4, 'bigfish', '');
insert into table_B
values (4, 'smallfish', '');


insert into table_C
values ('snake', 'python');
insert into table_C
values ('lizard', 'adder');
insert into table_C
values ('canine', 'corgi');
insert into table_C
values ('feline', 'lion');
insert into table_C
values ('ursine', 'grizzly');
insert into table_C
values ('smallbird', 'swallow');
insert into table_C
values ('bigbird', 'goose');
insert into table_C
values ('bigfish', 'baraccuda');
insert into table_C
values ('smallfish', 'minnow');
insert into table_C
values ('smallfish', 'herring');

insert into table_D
values ('python', 'big');
insert into table_D
values ('adder', 'scary');
insert into table_D
values ('asp', 'scary');
insert into table_D
values ('corgi', 'funny');
insert into table_D
values ('doberman', 'funny');
insert into table_D
values ('lion', 'evil');
insert into table_D
values ('tabby', 'evil');
insert into table_D
values ('swallow', 'spit');
insert into table_D
values ('goose', 'edible');
insert into table_D
values ('herring', 'red');
insert into table_D
values ('pike', 'weapon');

create view my_result_chain
as
    select a.xxxxxx as filter, a.Code as Code, b.Group as Group1, c.group2 as Group2, d.result as Result
    from
        table_A a
    join (table_B b)
        on (b.Code=a.Code)
    join (table_C c)
        on (c.group=b.group)
    join (table_D d)
        on (d.group3=c.group2)
    where
        b.apple = '';

select * from my_result_chain;

select * from my_result_chain
where filter = 1;

drop view my_result_chain;
drop table table_A;
drop table table_B;
drop table table_C;
drop table table_D;

现在在您的代码中 select 使用您想要的过滤器值,您将在结果列中获得所有结果。