为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:DM8
【操作系统】:WIN11
【CPU】:i710510U
【问题描述】*:
CREATE OR REPLACE PROCEDURE conf_apply_listInfo( "begintime" IN VARCHAR(32),
"endtime" IN VARCHAR(32),
"operatorId" IN int,
"states" IN int,
"beginIndex" IN int,
"endIndex" IN int,
"rowSum" out int)
AUTHID DEFINER
as
as
v_str varchar2(1000);
v_str_sum varchar2(1000);
v_strsql varchar2(1000);
rowSum1 int;
BEGIN
IF beginIndex > 0 THEN
SET beginIndex := beginIndex - 1;
END IF;
-- 会议未审批数
IF states = 1 THEN
SET v_str := CONCAT(SELECT * FROM conf WHERE create_time > '",begintime,"'
AND create_time < '",endtime,"'
AND appointmentState = 1
AND auditState IS NULL
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = '",operatorId,"' ) ));
-- 会议通过数
ELSEIF states = 2 THEN
SET v_str := CONCAT(SELECT * FROM conf WHERE create_time > '",begintime,"'
AND create_time < '",endtime,"'
AND appointmentState = 1
AND auditState = 1
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = '",operatorId,"' ) ));
-- 会议驳回数
ELSEIF states = 3 THEN
SET v_str := CONCAT(SELECT * FROM conf WHERE create_time > '",begintime,"'
AND create_time < '",endtime,"'
AND appointmentState = 1
AND auditState = 2
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = '",operatorId,"' ) ));
END IF;
SET v_str_sum := concat('select count(1) into rowSum1 from (', v_str ,') b');
-- PREPARE stmt1 FROM @v_strsql;
EXECUTE v_str_sum;
-- DEALLOCATE PREPARE v_str_sum;
SET rowSum:=rowsum1;
SET v_strsql := concat('select * from (', v_str ,') c limit ', beginIndex , ',', endIndex-beginIndex);
-- PREPARE stmt1 FROM @v_strsql;
EXECUTE v_strsql;
-- DEALLOCATE PREPARE stmt1;
END;
请问如何优化这个存储过程,问题1是select语句过长,达到可执行的效果
还有第二问题
CREATE OR REPLACE PROCEDURE conf_apply_statistics("begintime" IN VARCHAR(32), "endtime" IN VARCHAR(32), "operatorId" IN int)
as
BEGIN
SELECT
a1.count,
a2.agreecount,
a3.noagreecount,
a1.count - a2.agreecount - a3.noagreecount AS outcount
FROM
(
SELECT
IF( sum( 1 ) IS NULL, 0, sum( 1 ) ) AS count
FROM
conf
WHERE
create_time > begintime
AND create_time < endtime
AND appointmentState = 1
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = operatorId ) )
) AS a1,
(
SELECT
IF( sum( 1 ) IS NULL, 0, sum( 1 ) ) AS agreecount
FROM
conf
WHERE
create_time > begintime
AND create_time < endtime
AND appointmentState = 1
AND auditState = 1
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = operatorId ) )
) AS a2,
(
SELECT
IF( sum( 1 ) IS NULL, 0, sum( 1 ) ) AS noagreecount
FROM
conf
WHERE
create_time > begintime
AND create_time < endtime
AND appointmentState = 1
AND auditState = 2
AND applyPeople_Id IN ( SELECT id FROM operator WHERE dept_id IN ( SELECT dept_id FROM operator WHERE unitId = operatorId ) )
) AS a3 ;
-- PREPARE stmt from sql;
-- EXECUTE sql;
END;
这个IF判断我怎么替换,如果是Null就返回0,如果不是就返回查询出来的值。求教
使用nvl函数可以实现null返回0
nvl(exp,0)
关于存储过程优化是一个基于业务的比较复杂的过程,在这里不便进行分析优化。