Home Database Mysql Tutorial 如何高效能拆分一个字段为多行

如何高效能拆分一个字段为多行

Jun 07, 2016 pm 02:53 PM
one how Field Split high efficiency

如何高效能拆分一个字段为多行 我本意是将表中的一个字段拆分成多行,例如aaa,bbb拆分为 aaa bbb 现在测试如下: [sql] with t1 as ( select 3 c1,eee,fff,ggg c2 from dual UNION ALL select 2 c1,ccc,ddd c2 from dual UNION ALL SELECT 1 c1,aaa,bbb c2 F


如何高效能拆分一个字段为多行

 

我本意是将表中的一个字段拆分成多行,例如'aaa,bbb'拆分为

'aaa'

'bbb'

现在测试如下:

 

[sql] 

with t1 as   

(  

     select 3 c1,'eee,fff,ggg' c2 from dual UNION ALL   

     select 2 c1,'ccc,ddd' c2 from dual UNION ALL   

     SELECT 1 c1,'aaa,bbb' c2 FROM dual  

)  

  

select c1,LEVEL,replace(regexp_substr(c2,'[^,]+',1,level),',',' ') c2  

from t1   

connect BY level

order by c1,level  

  www.2cto.com  

返回结果如下:

[sql] 

C1 LEVEL C2  

1 1 aaa  

1 2 bbb  

1 2 bbb  

1 2 bbb  

2 1 ccc  

2 2 ddd  

2 2 ddd  

2 2 ddd  

3 1 eee  

3 2 fff  

3 2 fff  

3 2 fff  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

3 3 ggg  

  www.2cto.com  

--======================================================

看起来connect by之后产生了大量重复数据,于是加入distinct后取得正确数据。

反思:

我构造的测试数据仅仅只有三行,最长的拆分资料仅3段'eee,fff,ggg',却产生了21笔资料。如果测试数据增多,或者需拆分的段

数量增多,那么connect by产生的数据将是海量的。

用此种方法实际处理生产库数据时,问题马上显现出来,仅17笔资料,最长拆分字段为8段,竟然产生了738万笔资料,尽管我使用

了distinct,依然慢的很。

 

解决方案:用Join方式取代connect by方式

 

[sql] 

with t1 as   

(  

     select 3 c1,'eee,fff,ggg' c2 from dual UNION ALL   

     select 2 c1,'ccc,ddd' c2 from dual UNION ALL   

     SELECT 1 c1,'aaa,bbb' c2 FROM dual  

)  

  

SELECT c1,  

       substr(t.ca,  

              instr(t.ca, ',', 1, d.lv) + 1,  

              instr(t.ca, ',', 1, d.lv + 1) -  

              (instr(t.ca, ',', 1, d.lv) + 1)) AS d  

  FROM (SELECT c1,  

               ',' || c2 || ',' AS ca,  

               length(c2 || ',') - nvl(length(REPLACE(c2, ',')), 0) AS cnt  

          FROM t1) t,  

       (select rownum lv from  

        (select max(length(c2 || ',') - nvl(length(REPLACE(c2, ',')), 0)) mlc from t1)   

         connect by level

       )d         

WHERE d.lv

ORDER BY c1  

  www.2cto.com  

结论:

对于表资料只有一笔的时候,用connect by一般不会有什么问题。但如果表中资料是多笔,则connect by会产生海量的重复资料。

 

用join方式可解决此类问题。

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)

Get started quickly: JSON array merging and splitting techniques in Java. Get started quickly: JSON array merging and splitting techniques in Java. Sep 06, 2023 am 10:21 AM

Get started quickly: JSON array merging and splitting techniques in Java In modern software development, data format and transmission have become increasingly important. Among them, JSON (JavaScriptObjectNotation) is a commonly used data format, especially suitable for front-end and back-end interaction and data storage. In Java development, we often need to deal with JSON objects and JSON arrays. This article explains how to merge and split JSON arrays in Java, along with tips and examples for implementing these operations.

How to use PHP ZipArchive to merge and split multiple compressed packages? How to use PHP ZipArchive to merge and split multiple compressed packages? Jul 21, 2023 am 10:17 AM

How to use PHPZipArchive to merge and split multiple compressed packages? Overview: During the development process, sometimes we need to merge multiple compressed packages into one, or split a compressed package into multiple ones. PHP provides the ZipArchive extension to easily complete these operations. This article will introduce how to use PHPZipArchive to merge and split multiple compressed packages. Merging multiple archives First, we need to create a new archive and open it. Then, the loop traversal needs to be

NoSuchFieldError in Java - Solution to field not found NoSuchFieldError in Java - Solution to field not found Jun 25, 2023 am 11:33 AM

NoSuchFieldError in Java - Solution to field not found Java is a high-level programming language that is widely used in enterprise-level applications and large-scale data processing. During the development process of Java, errors such as NoSuchFieldError may occur. This error means that the JVM cannot find the required field at runtime. In this article, we will take a deeper look at NoSuchFieldError and how to resolve it. What is NoSuchFieldE

Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Is there a future for employment in clinical pharmacy at Harbin Medical University? (What are the employment prospects for clinical pharmacy at Harbin Medical University?) Jan 02, 2024 pm 08:54 PM

What are the employment prospects of clinical pharmacy at Harbin Medical University? Although the national employment situation is not optimistic, pharmaceutical graduates still have good employment prospects. Overall, the supply of pharmaceutical graduates is less than the demand. Pharmaceutical companies and pharmaceutical factories are the main channels for absorbing such graduates. The demand for talents in the pharmaceutical industry is also growing steadily. According to reports, in recent years, the supply-demand ratio for graduate students in majors such as pharmaceutical preparations and natural medicinal chemistry has even reached 1:10. Employment direction of clinical pharmacy major: After graduation, students majoring in clinical medicine can engage in medical treatment, prevention, medical research, etc. in medical and health units, medical research and other departments. Employment positions: Medical representative, pharmaceutical sales representative, sales representative, sales manager, regional sales manager, investment manager, product manager, product specialist, nurse

How to determine if a field is empty in PHP? How to determine if a field is empty in PHP? Mar 20, 2024 pm 03:09 PM

PHP is a scripting language widely used in website development. For developers, it is often necessary to determine whether a field is empty. In PHP, determining whether a field is empty can be achieved through some simple methods. This article will introduce how to determine whether a field is empty in PHP, and provide specific code examples for your reference. In PHP, you can usually use the empty() function or isset() function to determine whether a field is empty. Next, we introduce the usage of these two functions respectively. Use the empty() function

How to clean temp folder How to clean temp folder Feb 22, 2024 am 09:15 AM

How to clean the temp folder As we use the computer, temporary files (temp files) will gradually accumulate. These temporary files are generated when we use the computer, such as cache files when browsing the web, temporary files during software installation, etc. Failure to clean the temp folder for a long time may occupy a large amount of disk space and affect the speed of the computer. Therefore, cleaning the temp folder regularly is a necessary step to maintain computer performance. Below, we will introduce some simple ways to clean the temp folder. Method 1: Manually clean t

How to download win10 image quickly How to download win10 image quickly Jan 07, 2024 am 11:33 AM

Recently, some friends reported how to download win10 image files. Because there are so many image files on the market, what should I do if I want to find a regular file to download? Today, the editor has brought you the link to download the image and the detailed solution steps. Let’s take a look at them together. win10 image quick download and installation tutorial download link >>> System Home Ghostwin101909 image 64-bit version v2019.11<<<>>>Win10 image 64-bit v2019.07<<<>>>Win10 image 32-bit v2019.07<< <1. Search through the Internet

What does mysql field mean? What does mysql field mean? Jul 10, 2023 pm 02:14 PM

A mysql field is a column of a specific type and length in a mysql database table that is used to store data. In MySQL, each field must have a specific data type. Common data types include integers, floating point numbers, strings, dates, and times. These data types determine the data that MySQL can store in each field.

See all articles