栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从MySQL中的分层数据生成基于深度的树(无CTE)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从MySQL中的分层数据生成基于深度的树(无CTE)

如果使用存储过程,则可以在一次从php到mysql的调用中完成:

呼叫范例

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 |+--------+---------------+---------------+----------------------+-------+4 rows in set (0.00 sec)$sql = sprintf("call category_hier(%d)", $id);

希望这可以帮助 :)

完整脚本

测试表结构:

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;

测试数据:

insert into categories (name, parent_cat_id) values('Location',null),   ('USA',1),       ('Illinois',2),       ('Chicago',2),  ('Color',null),    ('Black',3),    ('Red',3);

程序:

drop procedure if exists category_hier;delimiter #create procedure category_hier(in p_cat_id smallint unsigned)begindeclare 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.depthfrom  hierinner join categories p on hier.cat_id = p.cat_idleft outer join categories b on hier.parent_cat_id = b.cat_idorder by hier.depth, hier.cat_id;drop temporary table if exists hier;drop temporary table if exists tmp;end #

测试运行:

delimiter ;call category_hier(1);call category_hier(2);

使用Yahoo Geoplanet放置数据的一些性能测试

drop table if exists geoplanet_places;create table geoplanet_places(woe_id int unsigned not null,iso_pre  varchar(3) not null,name varchar(255) not null,lang varchar(8) not null,place_type varchar(32) not null,parent_woe_id int unsigned not null,primary key (woe_id),key (parent_woe_id))engine=innodb;mysql> select count(*) from geoplanet_places;+----------+| count(*) |+----------+|  5653967 |+----------+

所以表中有560万行(位置),让我们看看从php调用的邻接表实现/存储过程是如何处理的。

     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   918 records fetched with max depth 1 in 0.009689 secs  1346 records fetched with max depth 1 in 0.040453 secs  5901 records fetched with max depth 2 in 0.219246 secs  6817 records fetched with max depth 1 in 0.152841 secs  8621 records fetched with max depth 3 in 0.096665 secs 18098 records fetched with max depth 3 in 0.580223 secs238007 records fetched with max depth 4 in 2.003213 secs

总的来说,我对那些寒冷的运行时感到非常满意,因为我什至不会开始考虑将数万行数据返回到我的前端,而是宁愿动态地构建树,每次调用只获取几个级别。哦,以防万一您以为innodb比myisam慢-
我测试的myisam实现在所有方面都慢了一倍。

此处有更多内容:http :
//pastie.org/1672733

希望这可以帮助 :)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号