进击的大菜鸟 2021-03-18 16:02 采纳率: 83.3%
浏览 36
已结题

mysql存储过程优化

小白,写了个存储过程,麻烦各位大佬该看一下,能不能优化下,1000条数据,查询了两秒

CREATE PROCEDURE `getAllChildren`(pid bigint(20),typeCode varchar(20))
begin
declare last_count int;
declare last_level int;
set last_level = 0;
drop table if exists temp;
drop table if exists result;
create temporary table temp (id bigint(20) primary key, parent_id bigint(20),type varchar(20),name varchar(50), level int);
create temporary table result (id bigint(20) primary key, parent_id bigint(20), type varchar(20),name varchar(50), level int);
insert into temp(id, parent_id, type,name, level) select id, parent_res_id, res_type,res_name, last_level from pue_res_manage where parent_res_id = pid and delete_state = '0' ;
while exists (select * from temp) do
		insert into result select * from temp;
    delete from temp;
    insert into temp(id, parent_id,type,name, level) 
    select id, parent_res_id,res_type,res_name, last_level + 1
    from pue_res_manage 
    where parent_res_id in (select id from result where level = last_level) and delete_state = '0';
    set last_level = last_level + 1;
end while;
if typeCode is null then
	select id from result;
else
	select id from result where type  = typeCode;
end if;
end
  • 写回答

2条回答 默认 最新

  • 哆啦A梦丶幻想 2021-03-18 18:04
    关注

    看看字段能不能使用枚举?

    然后创建表如果只是临时用的话,看能不能创建临时表

     

    存储过程如果只是临时用的话可以。业务代码中不能用的呦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月9日
  • 已采纳回答 2月9日

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开