Home > database > Through a function extraction efficiency too slow, when all the data for improvement program
Through a function extraction efficiency too slow, when all the data for improvement program
Time:10-03
The query is:
select XWCS The from (select get_user_xwcs (b.i d, '2016-11-01', '2016-11-30') as XWCS The from t_user b Where b.a rea_id=201 And b.com pany_id is not null)
The query statement from t_detail_review extract each user in the table from November 1 to November 30 data, execute the statement, when all the data in PLSQL particularly slow, affect the efficiency, seeking improvements! Get_user_xwcs function:
The CREATE OR REPLACE FUNCTION get_user_xwcs (user_id number, startDate varchar, endDate varchar) The RETURN number IS Icount number; The BEGIN Select sum (case when cs> 40 then 40 else cs=end) into icount the from ( The select userid, to_char (create_date '- dd yyyy - mm), count (distinct dyn_detail_id) cs The from t_detail_review Where userid user_id= And (startDate is null or create_date & gt;=to_date (startDate, 'yyyy - mm - dd)) And (endDate is null or create_date & lt;=to_date (endDate | | '23:59:59', '- dd yyyy - mm hh24: mi: ss')) Group by userid, to_char (create_date '- dd yyyy - mm)); RETURN NVL (icount, 0); The END;
CodePudding user response:
Index and structure of the two tables below: T_user:
T_detail_review:
CodePudding user response:
Take the startDate, endDate is null record of handling the Your current statement would lead to a full table scan
In addition, you as if there is a problem returns multiple records
CodePudding user response:
The CREATE OR REPLACE FUNCTION get_user_xwcs (user_id number, startDate varchar, endDate varchar) The RETURN number IS Icount number; The BEGIN Select sum (case when cs> 40 then 40 else cs=end) into icount the from ( The select userid, count (distinct dyn_detail_id) cs The from t_detail_review Where userid user_id= And create_date & gt;=to_date (startDate, 'yyyy - mm - dd) And create_date & lt;=to_date (endDate | | '23:59:59', '- dd yyyy - mm hh24: mi: ss') Group by userid); RETURN NVL (icount, 0); The END;