PHP SQL多表分层foreach内foreach内foreach内foreach,reffence仅高于1级
我需要在下面两种不同的模式下运行,因此if second稍后将构建一个大型csv 下面是单个实例的正常运行时间,但第二个实例的加载时间超过了第一个7k行的加载时间 我想避免恐惧PHP SQL多表分层foreach内foreach内foreach内foreach,reffence仅高于1级,php,mysql,sql,datatable,Php,Mysql,Sql,Datatable,我需要在下面两种不同的模式下运行,因此if second稍后将构建一个大型csv 下面是单个实例的正常运行时间,但第二个实例的加载时间超过了第一个7k行的加载时间 我想避免恐惧 set_time_limit(0); 我还在每个阶段挑选一些片段,因为这些片段稍后也会用到 所以问题是,下面最快的方法是什么 可以是一个SQL语句和定义值的提取,如中所示 table_c.tree , $row['apple'] 表结构 表A有7k行,其中$row['Code']可以重复10次 表B有10k行,其
set_time_limit(0);
我还在每个阶段挑选一些片段,因为这些片段稍后也会用到
所以问题是,下面最快的方法是什么
可以是一个SQL语句和定义值的提取,如中所示
table_c.tree , $row['apple']
表结构
表A有7k行,其中$row['Code']可以重复10次
表B有10k行,其中$row['Group']可以重复5次
表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']
}
第一排的桌子不是我的,所以我无法控制它们
另一种方法是将一些新字段加载到表_a中,但这只是将时间分配给其他函数
以下是@yoshiwaan
我错过了一个速度技巧吗?如果我创建一次我的\u结果\u链,因为所有数据都可以在开始时以很好的速度转储到中,那么只需调用foreach,它运行得非常快。但由于有些表不包含上一级表的所有引用,因此它尽可能返回null而不是trail。这可以通过使用LEFT JOIN来纠正,但显然会创建多个行,其中filter key是相同的等等。有没有办法选择哪一行/字段,DISTINCT只获取顶层,并且每个表没有唯一的key,因为我需要从不同的表中获取不同的数据?如果创建一次,则参考13秒v.03秒。如果我在foreach的每一行中添加一个键号,这算是个不错的解决方案。简短提示:不要选择*如果不需要所有内容,只需选择所需的列即可 冗长版本:不要在代码中这样做,而是将逻辑卸载到数据库中的视图中 下面是一个很好的示例,您需要向下滚动才能看到所有内容:
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;
现在,在您的代码中,只需选择所需的using there filter value,您将在results列中获得所有结果。谢谢-这就是我需要的SQL。不幸的是,在某个时刻,所有表的大多数行中都需要*号,因此它被保留在中。