Home Database Mysql Tutorial oracel中字符串分割成集合详解

oracel中字符串分割成集合详解

Jun 07, 2016 pm 05:49 PM
String splitting

文章分享一篇关于自己的学习笔记,oracel中字符串分割成集合详解有需要学习的同学可以看看。

首先分别使用两种方式构造两个函数

 代码如下 复制代码

-- use conventional plsql
create or replace function f_str2list_pls
(
  p_str       varchar2,
  p_separator varchar2 default ','
) return my_tk_str_tab_type is
  l_idx  pls_integer := 0;
  l_str  varchar2(32767) := trim(p_str);
  l_elmt varchar2(100) := null;
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
begin
  while l_str is not null loop
    l_idx := instr(l_str, p_separator);
    if l_idx = 0 then
      l_elmt := l_str;
      l_str  := null;
    else
      l_elmt := substr(l_str, 1, l_idx - 1);
      l_str  := substr(l_str, l_idx + 1);
    end if;
 
    l_list.extend;
    l_list(l_list.last) := trim(l_elmt);
  end loop;

  return l_list;
end;
/

-- use single sql
create or replace function f_str2list_sql
(
  p_str       varchar2,
  p_separator varchar2 default ','
) return my_tk_str_tab_type is
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
begin
  substr(a.str,
                instr(p_separator || a.str, p_separator, 1, rn),
                instr(a.str || p_separator, p_separator, 1, rn) -
                instr(p_separator || a.str, p_separator, 1, rn)) q
    bulk collect into l_list
    from (select p_str as str from dual) a,
         (select rownum rn from dual connect by rownum    where instr(p_separator || a.str, p_separator, 1, rn) > 0;

  return l_list;
end;
/

 

 


确认两种方法完成同样的功能

 

----------------------------------------------------

 代码如下 复制代码

-- same result
declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str varchar2(1000) := 'a,b,c';
begin
  l_list := f_str2list_pls(l_str,',');
  for i in 1..l_list.count loop
    dbms_output.put_line(l_list(i));
  end loop;
 
  dbms_output.put_line('');
  l_list := f_str2list_sql(l_str,',');
  for i in 1..l_list.count loop
    dbms_output.put_line(l_list(i));
  end loop;
end;
/

SQL> set serveroutput on
a
b
c
 
a
b
c

 

 

 

 

 

我们知道在PL/SQL和SQL里面,varchar2类型的长度限制是不同的。那么这两种方法是否也存在同样的限制?验证一下

先测试PL/SQL版本

 

-- 这里使用单个字母作为元素,加上必要的逗号分割符,一个元素占用长度2. PL/SQL中上限32767。当取(32767/2 = 16383)个元素的时候,运行成功
----------------------------------------------------

 代码如下 复制代码

-- pls versions tring length limit
declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 16383;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;
  l_str := substr(l_str, 2);

   l_list := f_str2list_pls(l_str, ',');
end;
/

PL/SQL procedure successfully completed

-- 增加一个元素,当取(16384)个元素的时候,超过了varchar2的限制,所以无法运行。(构造字符串就报错了,这个时候其实还没有调用f_str2list_pls)
 代码如下 复制代码

declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 16384;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;

  l_list := f_str2list_pls(l_str, ',');
end;
/

ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 9

-- 修改一下代码,实际调用这个函数。从结果可以看到,f_str2list_pls()内部报超长错误
declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 16383;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;

  l_list := f_str2list_pls(l_str||',a', ',');
end;
 
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "T2.F_STR2LIST_PLS", line 7
ORA-06512: at line 12

 

 

 


下面测试SQL版本

 

 代码如下 复制代码

-- 因为SQL中varchar2上限是4000,所以使用2000个元素。一切正常。
----------------------------------------------------
-- sql versions tring length limit
declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 2000;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;
  l_str := substr(l_str, 2);

   l_list := f_str2list_sql(l_str, ',');
end;
/

PL/SQL procedure successfully completed

-- 直接增加到2001个元素,发现f_str2list_sql()内部报错,说明构造字符串的成功传入,但是函数不能处理
 代码如下 复制代码

declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 2001;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;
  l_str := substr(l_str, 2);

   l_list := f_str2list_sql(l_str, ',');
end;
 
ORA-01460: unimplemented or unreasonable conversion ed
ORA-06512: at "T2.F_STR2LIST_SQL", line 8
ORA-06512: at line 13

备注:ORA-01460的描述很不清楚,其实这里就是varchar2超长

 

 

 

 

 

测试比较两种方式在不同数据量下的性能

 

 代码如下 复制代码

----------------------------------------------------
-- performance test
declare
  l_list my_tk_str_tab_type := my_tk_str_tab_type();
  l_str  varchar2(32767) := '';
  l_max  pls_integer := 2000;
begin
  -- construct string
  for i in 1 .. l_max loop
    l_str := l_str || ',' || 'a';
  end loop;
  l_str := substr(l_str, 2);

  -- warm up before actually calculation
  l_list := f_str2list_sql(l_str, ',');
  l_list := f_str2list_pls(l_str, ',');
 
  -- begin calc and diff
  my_rs.rs_start;
  -- 1. pls version
  l_list := f_str2list_pls(l_str, ',');
  my_rs.rs_middle;
  -- 2. sql version
  l_list := f_str2list_sql(l_str, ',');
  my_rs.rs_stop();
end;
/

 

 

 


我们分别测试当元素个数为100, 200, 500, 1000, 2000的情况。每种情况进行3-5次然后取平均

元素个数

PL/SQL 运行时间 (1/100 second)

SQL 运行时间 (1/100 second)

PCT

100

1

1

100%

200

1

1 - 2

50% - 100%

500

1 - 2

2 - 3

30% - 50%

1000

1

4

25%

2000

2

10

20%

总结:

SQL版本的书写简便,并且可以脱离PL/SQL环境直接使用(单条SQL进行行列转换)。

SQL版本只能处理长度小于4000的字符串,实际最大只能包含2000个元素。PL/SQL版本可以处理长度小于32767的字符串。

小数据量情况下,两者性能相当。随着数据量增大,PL/SQL版本性能明显占优

 

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the split() function in Python 3.x to split a string according to the specified delimiter How to use the split() function in Python 3.x to split a string according to the specified delimiter Jul 31, 2023 pm 08:33 PM

Python is a popular programming language that provides many built-in functions to handle strings. One of the commonly used functions is the split() function, which can split a string into multiple substrings according to the specified delimiter. This article will introduce how to use the split() function in Python3.x. In Python, the split() function is a built-in function of the string class. Its basic syntax is as follows: string.split(separator,maxsplit)

How to use explode function to split string in PHP How to use explode function to split string in PHP Jun 26, 2023 pm 12:03 PM

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

The maximum split length of a string such that each character in the string appears in a substring The maximum split length of a string such that each character in the string appears in a substring Aug 25, 2023 pm 02:41 PM

In this article, we will explore the problem of how to find the length of the maximizing partition of a string with unique characters. We first understand the problem statement and then study naive and efficient methods to solve this problem, including their respective algorithms and time complexities. Finally, we will implement the solution in C++. Problem Statement Given a string, split the string into as many substrings as possible so that each character in the string appears in only one substring. Returns the length of these maximizing splits. Naive approach The naive approach is to iterate through the string, recording the last occurrence of each character. Then, iterate over the string again and create a partition when the last occurrence of the current character is found. Algorithm (naive) to initialize an array to store strings in

How to split string into array in PHP How to split string into array in PHP Jul 08, 2023 pm 01:49 PM

How to Split String into Array in PHP In PHP, we often need to process strings and split them into multiple parts. Splitting a string into an array is a common operation that helps us better handle the various parts of the string. In this article, we will learn how to split a string into an array using functions in PHP and provide some code examples. Use the explode function to split a string into an array. PHP provides a function called explode, which can split the string according to the specified delimiter.

Count the number of ways to split a string into K substrings starting with an even number and having a minimum length of M Count the number of ways to split a string into K substrings starting with an even number and having a minimum length of M Sep 09, 2023 pm 02:01 PM

In this problem, we will calculate the way to divide the given string into K substrings such that it satisfies the conditions given in the problem statement. We will use recursion to solve this problem. Additionally, we will use tabular dynamic programming methods to efficiently solve this problem. Problem Statement − We have a string of specific length called bin_Str. The string contains only numeric characters from '0' to '9'. We need to calculate the number of ways to split the string into K substrings such that it satisfies the following conditions. Substring should contain at least 2 characters. The first character of each substring should be even and the last character should be odd. ExampleExample inputM=2,K=2;bin_str="255687&q

PHP string function example: string splitting PHP string function example: string splitting Jun 20, 2023 pm 01:58 PM

There are many string functions in PHP, among which the string splitting function is very commonly used. The string split function can split a string according to the specified delimiter and return an array. Below we will introduce several commonly used string splitting functions. explode function The explode function can split a string according to the specified delimiter and return an array. The syntax is as follows: explode(string$separator,string$string

How to solve the error reported by the explode function in PHP How to solve the error reported by the explode function in PHP Mar 11, 2024 am 11:45 AM

The method to solve the error reported by the explode function in PHP requires specific code examples. In PHP, the explode function is a function used to split a string into an array according to the specified delimiter. However, sometimes an error occurs when using the explode function, mainly because the parameters passed in do not meet the requirements of the function. Below we will discuss possible problems and solutions in detail, and provide specific code examples. Error caused by wrong number of parameters when using explode function

Checks whether a string can be split into three substrings, where one substring is a substring of the other two substrings Checks whether a string can be split into three substrings, where one substring is a substring of the other two substrings Sep 22, 2023 am 11:53 AM

In this problem, we need to split the given string such that the third substring can be a substring of the first two substrings. Let's think of a solution. The third string can be a substring of the first two strings only if the first two strings contain all the characters of the third string. So, we need to find at least one character with frequency greater than 3 in the given string, and we can take the third substring of that single character. Problem Statement - We are given a string str containing N lowercase alphabetic characters. We need to check if we can split the string into three substrings a, b and c such that substring c is a substring of a and b. Depending on whether 3 substrings can be found, print "yes" or "no"

See all articles