Du kan göra det i ett enda anrop från PHP till MySQL om du använder en lagrad procedur:
Exempelanrop
mysql> call category_hier(1);
+--------+---------------+---------------+----------------------+-------+
| cat_id | category_name | parent_cat_id | parent_category_name | depth |
+--------+---------------+---------------+----------------------+-------+
| 1 | Location | NULL | NULL | 0 |
| 3 | USA | 1 | Location | 1 |
| 4 | Illinois | 3 | USA | 2 |
| 5 | Chicago | 3 | USA | 2 |
+--------+---------------+---------------+----------------------+-------+
$sql = sprintf("call category_hier(%d)", $id);Hoppas detta hjälper!
Komplett skript
Testtabellstruktur:
drop table if exists categories;
create table categories (
cat_id smallint unsigned not null auto_increment primary key,
name varchar(255) not null,
parent_cat_id smallint unsigned null,
key (parent_cat_id)
) engine = innodb;Testdata:
insert into categories (name, parent_cat_id) values
('Location',null), ('USA',1), ('Illinois',2), ('Chicago',2),
('Color',null), ('Black',3), ('Red',3);Procedur:
drop procedure if exists category_hier;
delimiter #
create procedure category_hier (in p_cat_id smallint unsigned)
begin
declare v_done tinyint unsigned default 0;
declare v_depth smallint unsigned default 0;
create temporary table hier(
parent_cat_id smallint unsigned,
cat_id smallint unsigned,
depth smallint unsigned default 0
) engine = memory;
insert into hier select parent_cat_id, cat_id, v_depth from categories where cat_id = p_cat_id;
create temporary table tmp engine=memory select * from hier;
while not v_done do
if exists(select 1 from categories p
inner join hier on p.parent_cat_id = hier.cat_id and hier.depth = v_depth) then
insert into hier
select p.parent_cat_id, p.cat_id, v_depth + 1
from categories p
inner join tmp on p.parent_cat_id = tmp.cat_id and tmp.depth = v_depth;
set v_depth = v_depth + 1;
truncate table tmp;
insert into tmp select * from hier where depth = v_depth;
else
set v_done = 1;
end if;
end while;
select p.cat_id, p.name as category_name, b.cat_id as parent_cat_id,
b.name as parent_category_name, hier.depth
from hier
inner join categories p on hier.cat_id = p.cat_id
left outer join categories b on hier.parent_cat_id = b.cat_id
order by hier.depth, hier.cat_id;
drop temporary table if exists hier;
drop temporary table if exists tmp;
end #
delimiter ;Testkörningar:
delimiter ;
call category_hier(1);
call category_hier(2);Prestandatest med Yahoo GeoPlanet-data
mysql> select count(*) from geoplanet_places;
+----------+
| count(*) |
+----------+
| 5653967 |Det är alltså 5,6 miljoner rader (platser) i tabellen. Låt oss se hur adjacency list-implementationen lagrad procedur, anropad från PHP, hanterar det.
1 records fetched with max depth 0 in 0.001921 secs 250 records fetched with max depth 1 in 0.004883 secs 515 records fetched with max depth 1 in 0.006552 secs 822 records fetched with max depth 1 in 0.009568 secs 5901 records fetched with max depth 2 in 0.219246 secs 18098 records fetched with max depth 3 in 0.580223 secs 238007 records fetched with max depth 4 in 2.003213 secs
Sammantaget är jag nöjd med dessa körtider – jag skulle aldrig överväga att returnera tiotusentals rader till mitt gränssnitt, utan bygger hellre trädet dynamiskt och hämtar bara några nivåer per anrop. Och nej, InnoDB är långsammare än MyISAM – min MyISAM-implementation var dubbelt så långsam i samtliga mätningar.
Mer finns här: https://pastie.org/1672733
Hoppas detta hjälper!