sql 优化 -- sql中的自定函数
Long run sql:
MERGE INTO INTITMRTNPARAM D
USING (
SELECT A.INRFILENM,A.INRSTAT,A.INRDEPCD,A.INRITMCD,A.INRSUBCD,A.INRSUPCD,A.INRSTOCD
FROM INTITMRTNPARAM A,INTITMRTNPARAM B
WHERE 1=1
AND A.ROWID <> B.ROWID
AND A.INRFILENM = B.INRFILENM
AND A.INRSTAT = 0
AND B.INRSTAT = 0
AND A.INRFILENM = p_filenm
AND B.INRFILENM = p_filenm
AND A.INRDEPCD = B.INRDEPCD
AND A.INRITMCD = B.INRITMCD
AND PKCRS.CHECKINCLUDECUSTTYPE(NVL(A.INRSUBCD,'ALL'),NVL(B.INRSUBCD,'ALL')) = 1
AND PKCRS.CHECKINCLUDECUSTTYPE(NVL(A.INRSUPCD,'ALL'),NVL(B.INRSUPCD,'ALL')) = 1
AND PKCRS.CHECKINCLUDECUSTTYPE(NVL(A.INRSTOCD,'ALL'),NVL(B.INRSTOCD,'ALL')) = 1
GROUP BY A.INRFILENM,A.INRSTAT,A.INRDEPCD,A.INRITMCD,A.INRSUBCD,A.INRSUPCD,A.INRSTOCD
) E
ON ( D.INRFILENM = E.INRFILENM
AND D.INRSTAT = E.INRSTAT
AND D.INRDEPCD = E.INRDEPCD
AND D.INRITMCD = E.INRITMCD
AND DECODE(TRIM(NVL(D.INRSUBCD,'')),'','NULL',TRIM(D.INRSUBCD)) = DECODE(TRIM(NVL(E.INRSUBCD,'')),'','NULL',TRIM(E.INRSUBCD))
AND DECODE(TRIM(NVL(D.INRSUPCD,'')),'','NULL',TRIM(D.INRSUPCD)) = DECODE(TRIM(NVL(E.INRSUPCD,'')),'','NULL',TRIM(E.INRSUPCD))
AND DECODE(TRIM(NVL(D.INRSTOCD,'')),'','NULL',TRIM(D.INRSTOCD)) = DECODE(TRIM(NVL(E.INRSTOCD,'')),'','NULL',TRIM(E.INRSTOCD)) )
WHEN MATCHED THEN
UPDATE SET D.INRERRNO = 2,
D.INRERRMES = D.INRERRMES || ', Dupplicate item',
D.INRUPD = p_date,
D.INRUSR = p_user;
Long sql 中调用的function(checkIncludeCustType):
FUNCTION checkIncludeCustType(p_custType1 PRITSPHDR.TSPHCUSTYPE%TYPE,p_custType2 PRITSPHDR.TSPHCUSTYPE%TYPE) RETURN NUMBER IS
v_check NUMBER(1);
BEGIN
IF p_custType2 IS NULL OR p_custType1 IS NULL THEN
RETURN 1;
END IF;
BEGIN
SELECT 1
INTO v_check
FROM (SELECT LEVEL,
SUBSTR (
STRING_TO_TOKENIZE,
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1),
INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) - DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1)
) AS Token
FROM ( SELECT p_custType1||',' AS STRING_TO_TOKENIZE, ',' AS DELIMITER FROM DUAL)
CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL)>0
ORDER BY LEVEL ASC
) custtype1,
(SELECT LEVEL,
SUBSTR ( STRING_TO_TOKENIZE,
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1),
INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) - DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL-1)+1)
) AS Token
FROM ( SELECT p_custType2||',' AS STRING_TO_TOKENIZE, ',' AS DELIMITER FROM DUAL)
CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL)>0
ORDER BY LEVEL ASC
) custtype2
WHERE custtype1.token = custtype2.token
AND rownum = 1;
EXCEPTION WHEN NO_DATA_FOUND THEN
v_check := 0;
END;
RETURN v_check;
END checkIncludeCustType;
优化后:
UPDATE INTITMRTNCON D
SET D.INRERRNO = 2, D.INRERRMES = D.INRERRMES || ', Dupplicate item' , D.INRUPD = p_date, D.INRUSR = p_user
WHERE INRFILENM = p_filenm AND INRSTAT = 0
AND (INRFILENM, INRSTAT, INRDPCD, INRITMCD, NVL(INRSUBCD, 'ALL'),
NVL(INRSUPCD, 'ALL')) IN
(SELECT INRFILENM, INRSTAT, INRDPCD, INRITMCD, NVL(INRSUBCD, 'ALL'), NVL(INRSUPCD, 'ALL')
FROM (SELECT T.INRFILENM, T.INRSTAT, T.INRDPCD, T.INRITMCD, T.INRSUBCD, T.INRSUPCD
FROM INTITMRTNCON T
WHERE INSTR(nvl(trim(INRSUBCD), 'ALL'), ',') = 0
and INSTR(nvl(trim(INRSUPCD), 'ALL'), ',') = 0
AND INRFILENM = p_filenm AND INRSTAT = 0
UNION ALL
select a.INRFILENM, a.INRSTAT, a.INRDPCD, a.INRITMCD, a.INRSUBCD, b.INRSUPCD
from (SELECT SUBSTR(STRING_TO_TOKENIZE,
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER,
1, LEVEL - 1) + 1),
INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) -
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER,
1, LEVEL - 1) + 1))
AS INRSUBCD,
V_ROWID, INRFILENM, INRSTAT, INRDPCD, INRITMCD
FROM (SELECT T.INRSUBCD || ',' AS STRING_TO_TOKENIZE,
',' AS DELIMITER, T.ROWID V_ROWID, INRFILENM,
INRSTAT, INRDPCD, INRITMCD
FROM INTITMRTNCON T
WHERE INRFILENM = p_filenm AND INRSTAT = 0 AND (INSTR(INRSUBCD, ',') >= 1
or INSTR(INRSUPCD, ',') >= 1))
CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) > 0) a,
(SELECT SUBSTR(STRING_TO_TOKENIZE,
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL - 1) + 1),
INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) -
DECODE(LEVEL, 1, 1, INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL - 1) + 1)) AS INRSUPCD,
V_ROWID, INRFILENM, INRSTAT, INRDPCD, INRITMCD
FROM (SELECT T.INRSUPCD || ',' AS STRING_TO_TOKENIZE, ',' AS DELIMITER,
T.ROWID V_ROWID, INRFILENM, INRSTAT, INRDPCD, INRITMCD
FROM INTITMRTNCON T
WHERE INRFILENM = p_filenm AND INRSTAT = 0 AND (INSTR(INRSUBCD, ',') >= 1
or INSTR(INRSUPCD, ',') >= 1 ))
CONNECT BY INSTR(STRING_TO_TOKENIZE, DELIMITER, 1, LEVEL) > 0) b
where a.v_rowid = b.v_rowid)
GROUP BY INRFILENM, INRSTAT, INRDPCD, INRITMCD, INRSUBCD, INRSUPCD
HAVING COUNT(*) > 1);
sql 优化 -- sql中的自定函数的更多相关文章
- SQL优化过程中常见Oracle HINT
在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...
- 面试题: 数据库 sql优化 sql练习题 有用 学生表,课程表,成绩表,教师表 练习
什么是存储过程?有哪些优缺点? 什么是存储过程?有哪些优缺点? 存储过程就像我们编程语言中的函数一样,封装了我们的代码(PLSQL.T-SQL). 存储过程的优点: 能够将代码封装起来 保存在数据库之 ...
- SQL优化 | sql执行过长的时间,如何优化?
1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化) 2.涉及链表的查询,是否能进行分表查询,单表查询之后的结果进行字段整合 3. ...
- SQL 优化SQL查询
摘至于:http://www.cnblogs.com/ATree/archive/2011/02/13/sql_optimize_1.html 1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据 ...
- SQL优化SQL tuning
1. 索引不合适,走主键进行了key lookup查找 说明索引没有覆盖到where条件 或者 orderby 或者 group by的列
- 深入了解 TiDB SQL 优化器
分享嘉宾:张建 PingCAP TiDB优化器与执行引擎技术负责人 编辑整理:Druid中国用户组第6次大数据MeetUp 出品平台:DataFunTalk 导读: 本次报告张老师主要从原理上带大家深 ...
- ORACLE常用SQL优化hint语句
在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...
- MySQL中优化sql语句查询常用的30种方法
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使 ...
- MySQL中优化sql语句查询常用的种方法
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...
随机推荐
- 代码实现分析mpeg-2文件
1.概述 把上一篇文章中讲到的mpeg-2文件结构分析用代码实现,结合mpeg-2文件分析.才easy看懂. 2.代码 /* *本程序主要分析MPEG-2文件 *作者:缪国凯(MK) *8214860 ...
- Python文本爬虫实战
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7019950.html 一:流程 目标:爬取目标网页的图片 1:获取网页源码 2:用Python读取源码 3: ...
- 关于Git HEAD^与HEAD~的关系
关于Git HEAD^与HEAD~的关系 请参考下图,来自stackoverflow http://stackoverflow.com/questions/2221658/whats-the-diff ...
- Laravel中pluck的使用——返回指定的字段值信息列表
$model = self::where(['is_delete' => 0, 'is_on_sale' => 1]) ->whereIn('goods.cat_id', Goods ...
- Java ConcurrentHashMap (Java代码实战-005)
package Threads; import com.google.common.collect.Maps; import java.util.concurrent.ConcurrentMap; i ...
- log4j的详细配置(最省心完美配置)
先说下我的需求 1,可以记录日记在我们的java开发项目周期中: 2,很简单即可输出日志: 3,每天按照时间将不同的日志输出到不同的文件中,每天输出日志到一个带有当前时间戳的文件中: 4,可以修改当前 ...
- N皇后问题【递归求解】
n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在n*n的棋盘上,互相不能攻击,输出全部方案. 输入一个正整数N,则程序输出N皇后问题的全部摆法.输出结果里的每一行都代表一种摆法.行里的第i个数字 ...
- >/dev/null 2>&1 这句话的含义及使用的意义
一般在shell包括dos下都可能会这样使用. null是一个名叫null小桶的东西,将输出重定向到它的好处是不会因为输出的内容过多而导致文件大小不断的增加.其实,你就认为null就是什么都没有,也就 ...
- 时间选择器(js,css,html)
忘了从哪下载的了, 整理电脑, 做个记录, 效果图: 下载链接: 链接: https://pan.baidu.com/s/1qfYkcfn8dtLFg0oniB2GHA 提取码: 2amm
- 十五分钟介绍 Redis数据结构
下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...