`

按照一定顺序提取数据

阅读更多
按照一定顺序提取数据研究

create table xxx (n number);

insert into xxx values(1);
insert into xxx values(2);
insert into xxx values(3);
insert into xxx values(4);
insert into xxx values(5);

commit;

select * from xxx

N
1
2
3
4
5

如果我们希望按照(2, 4, 1, 3, 5) 提取数据可以
select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)

N
2
4
1
3
5

我们不能用
select * from xxx where n in (2, 4, 1, 3, 5)

N
1
2
3
4
5

效率
explain plan set statement_id='T_TEST' for
select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)

Plan hash value: 2336544415
 
------------------------------------------------------------------------------------------------------
| Id  | Operation                                 | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                          |          |     1 |    26 |    33   (4)| 00:00:01 |
|*  1 |  HASH JOIN SEMI                           |          |     1 |    26 |    33   (4)| 00:00:01 |
|   2 |   TABLE ACCESS FULL                       | XXX      |     5 |    65 |     3   (0)| 00:00:01 |
|   3 |   VIEW                                    | VW_NSO_1 |     1 |    13 |    29   (0)| 00:00:01 |
|   4 |    COUNT                                  |          |       |       |            |          |
|*  5 |     FILTER                                |          |       |       |            |          |
|   6 |      COLLECTION ITERATOR CONSTRUCTOR FETCH|          |       |       |            |          |
------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - access("N"="TO_NUMBER(COLUMN_VALUE)")
   5 - filter(ROWNUM>0)
 
Note
-----
-	dynamic sampling used for this statement	

create index idx_xxx on xxx(n)

Plan hash value: 4112344697
 
-------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |          |     1 |    26 |    30   (4)| 00:00:01 |
|   1 |  NESTED LOOPS                              |          |     1 |    26 |    30   (4)| 00:00:01 |
|   2 |   VIEW                                     | VW_NSO_1 |     1 |    13 |    29   (0)| 00:00:01 |
|   3 |    HASH UNIQUE                             |          |     1 |     2 |            |          |
|   4 |     COUNT                                  |          |       |       |            |          |
|*  5 |      FILTER                                |          |       |       |            |          |
|   6 |       COLLECTION ITERATOR CONSTRUCTOR FETCH|          |       |       |            |          |
|*  7 |   INDEX RANGE SCAN                         | IDX_XXX  |     1 |    13 |     0   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   5 - filter(ROWNUM>0)
   7 - access("N"="TO_NUMBER(COLUMN_VALUE)")
 
Note
-----
-	dynamic sampling used for this statement			
analyze table xxx compute statistics for table for all indexes for all columns 
explain plan set statement_id='T_TEST' for
select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)
select * from table(dbms_xplan.display);

Plan hash value: 4112344697
 
-------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |          |     1 |    15 |    30   (4)| 00:00:01 |
|   1 |  NESTED LOOPS                              |          |     1 |    15 |    30   (4)| 00:00:01 |
|   2 |   VIEW                                     | VW_NSO_1 |     1 |    13 |    29   (0)| 00:00:01 |
|   3 |    HASH UNIQUE                             |          |     1 |     2 |            |          |
|   4 |     COUNT                                  |          |       |       |            |          |
|*  5 |      FILTER                                |          |       |       |            |          |
|   6 |       COLLECTION ITERATOR CONSTRUCTOR FETCH|          |       |       |            |          |
|*  7 |   INDEX RANGE SCAN                         | IDX_XXX  |     1 |     2 |     0   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   5 - filter(ROWNUM>0)
   7 - access("N"="TO_NUMBER(COLUMN_VALUE)"

这个语句也可以这样写
select x.* from xxx x,
((select /*+Cardinality (t,0)*/ to_number(column_value) s from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)) m
where x.n=m.s
N
2
4
1
3
5

Plan hash value: 2981154701
 
-----------------------------------------------------------------------------------------------------
| Id  | Operation                                 | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                          |         |     1 |    15 |    29   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                             |         |     1 |    15 |    29   (0)| 00:00:01 |
|   2 |   VIEW                                    |         |     1 |    13 |    29   (0)| 00:00:01 |
|   3 |    COUNT                                  |         |       |       |            |          |
|*  4 |     FILTER                                |         |       |       |            |          |
|   5 |      COLLECTION ITERATOR CONSTRUCTOR FETCH|         |       |       |            |          |
|*  6 |   INDEX RANGE SCAN                        | IDX_XXX |     1 |     2 |     0   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   4 - filter(ROWNUM>0)
   6 - access("X"."N"="M"."S")

去掉提示
explain plan set statement_id='T_TEST' for
select x.* from xxx x,
((select  to_number(column_value) s from table(mytable(2, 4, 1, 3, 5)) t  )) m
where x.n=m.s

select * from table(dbms_xplan.display);

Plan hash value: 4014781130
 
--------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |         |  8168 | 32672 |    29   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                          |         |  8168 | 32672 |    29   (0)| 00:00:01 |
|   2 |   COLLECTION ITERATOR CONSTRUCTOR FETCH|         |       |       |            |          |
|*  3 |   INDEX RANGE SCAN                     | IDX_XXX |     1 |     2 |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   3 - access("X"."N"=TO_NUMBER(VALUE(KOKBF$)))

增加数据

insert into xxx
select r from
(
select rownum r  from dual  connect by level <= 100
) 
where r>5
order by dbms_random.value(1,20)

explain plan set statement_id='T_TEST' for
select x.* from xxx x,
((select  to_number(column_value) s from table(mytable(2, 4, 1, 3, 5)) t  )) m
where x.n=m.s

select * from table(dbms_xplan.display);

Plan hash value: 4014781130
 
--------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |         |  8168 | 32672 |    29   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                          |         |  8168 | 32672 |    29   (0)| 00:00:01 |
|   2 |   COLLECTION ITERATOR CONSTRUCTOR FETCH|         |       |       |            |          |
|*  3 |   INDEX RANGE SCAN                     | IDX_XXX |     1 |     2 |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   3 - access("X"."N"=TO_NUMBER(VALUE(KOKBF$)))

增加提示

select x.* from xxx x,
((select /*+Cardinality (t,0)*/ to_number(column_value) s from table(mytable(2, 4, 1, 3, 5)) t  )) m
where x.n=m.s
N
2
4
1
3
5
explain plan set statement_id='T_TEST' for
select x.* from xxx x,
((select /*+Cardinality (t,0)*/ to_number(column_value) s from table(mytable(2, 4, 1, 3, 5)) t  )) m
where x.n=m.s

select * from table(dbms_xplan.display);

Plan hash value: 4014781130
--------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                       |         |     1 |     4 |    29   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                          |         |     1 |     4 |    29   (0)| 00:00:01 |
|   2 |   COLLECTION ITERATOR CONSTRUCTOR FETCH|         |       |       |            |          |
|*  3 |   INDEX RANGE SCAN                     | IDX_XXX |     1 |     2 |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
 
   3 - access("X"."N"=TO_NUMBER(VALUE(KOKBF$)))

再来看看

select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)

N
1
2
4
5
3

发现这不是我们需要的顺序

explain plan set statement_id='T_TEST' for
select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)

select * from table(dbms_xplan.display);

Plan hash value: 4112344697
 
-------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |          |     1 |    15 |    30   (4)| 00:00:01 |
|   1 |  NESTED LOOPS                              |          |     1 |    15 |    30   (4)| 00:00:01 |
|   2 |   VIEW                                     | VW_NSO_1 |     1 |    13 |    29   (0)| 00:00:01 |
|   3 |    HASH UNIQUE                             |          |     1 |     2 |            |          |
|   4 |     COUNT                                  |          |       |       |            |          |
|*  5 |      FILTER                                |          |       |       |            |          |
|   6 |       COLLECTION ITERATOR CONSTRUCTOR FETCH|          |       |       |            |          |
|*  7 |   INDEX RANGE SCAN                         | IDX_XXX  |     1 |     2 |     0   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   5 - filter(ROWNUM>0)
   7 - access("N"="TO_NUMBER(COLUMN_VALUE)")

继续增加数据
----------------------------------------------
select * from table(dbms_xplan.display);
insert into xxx
select r from
(
select rownum r  from dual  connect by level <= 1000000
) 
where r>1000
order by dbms_random.value(1,20)

explain plan set statement_id='T_TEST' for
select * from xxx where n member of in_list2('2, 4, 1, 3, 5')

Plan hash value: 1759293582
 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| XXX  |     1 |     2 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("N"MEMBER OF"IN_LIST2"('2, 4, 1, 3, 5'))

analyze table xxx compute statistics for table for all indexes for all columns

explain plan set statement_id='T_TEST' for
select * from xxx where n member of in_list2('2, 4, 1, 3, 5')

47s

select * from table(dbms_xplan.display);

Plan hash value: 1759293582
 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 50000 |   195K|   597  (26)| 00:00:08 |
|*  1 |  TABLE ACCESS FULL| XXX  | 50000 |   195K|   597  (26)| 00:00:08 |
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("N"MEMBER OF"IN_LIST2"('2, 4, 1, 3, 5'))

explain plan set statement_id='T_TEST' for
select * from xxx where n 
in 
(select /*+Cardinality (t,0)*/ to_number(column_value) from table(mytable(2, 4, 1, 3, 5)) t where rownum>0)

select * from table(dbms_xplan.display);

Plan hash value: 4112344697
 
-------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |          |     1 |    17 |    32   (4)| 00:00:01 |
|   1 |  NESTED LOOPS                              |          |     1 |    17 |    32   (4)| 00:00:01 |
|   2 |   VIEW                                     | VW_NSO_1 |     1 |    13 |    29   (0)| 00:00:01 |
|   3 |    HASH UNIQUE                             |          |     1 |     2 |            |          |
|   4 |     COUNT                                  |          |       |       |            |          |
|*  5 |      FILTER                                |          |       |       |            |          |
|   6 |       COLLECTION ITERATOR CONSTRUCTOR FETCH|          |       |       |            |          |
|*  7 |   INDEX RANGE SCAN                         | IDX_XXX  |     1 |     4 |     2   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   5 - filter(ROWNUM>0)
   7 - access("N"="TO_NUMBER(COLUMN_VALUE)")

delete from xxx where n>100

explain plan set statement_id='T_TEST' for
select * from xxx where n member of in_list2('2, 4, 1, 3, 5')

select * from table(dbms_xplan.display);

Plan hash value: 1759293582
 
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 50000 |   195K|   597  (26)| 00:00:08 |
|*  1 |  TABLE ACCESS FULL| XXX  | 50000 |   195K|   597  (26)| 00:00:08 |
--------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - filter("N"MEMBER OF"IN_LIST2"('2, 4, 1, 3, 5'))


通过这个例子,我们明白
1、	当数据量变化很大后,分析变得非常很重要;
2、	不同的sql写法,执行计划不同,不经影响效率,还影响其功能;
3、	不能表面理解,需要仔细测试;
4、	执行计划……

数据存放存放机制与高水位

 

分享到:
评论

相关推荐

    002_DAT提取_提取数据_

    在`t=0.txt`这个文件名中,"t=0"可能是时间戳或者其他标识符,这提示我们在实际数据集中可能存在多个时间点的数据,可能需要按照某种规则(如时间顺序)来处理这些文件。 总结来说,从"002_DAT提取_提取数据_"这个...

    基于CAD软件道路横断面数据快速提取.pdf

    CAD软件在道路设计领域的应用越来越广泛,其中一个重要方面是道路横断面数据的提取。本文详细介绍了如何利用CAD平台下的CASS软件与VBA编程快速提取道路横断面数据,以提高数据处理的效率和精度。 首先,文章针对...

    顺序提取在分析土壤中金属钒形态的应用

    本文的研究还指出了顺序提取法存在的一个问题,即在不同实验室间的标准参考物质的提取数据差异较大,尤其是在提取过程的第二步。因此,为了提高方法的准确性和可重复性,有研究者提出了改进的BCR顺序提取方案,优化...

    数据挖掘技术与应用:提取时间序列数据信息.docx

    时间序列数据是指按照时间顺序排列的数据集,常见于用户活动记录、股票市场走势、天气预报等领域。在本案例中,我们将深入探讨如何利用数据挖掘技术来提取和分析时间序列数据的信息。 首先,提取时间序列数据信息的...

    EXCEL 中提取数据 TFSoftExcel.rar

    TFSoftExcel可能支持按照一个或多个列进行升序或降序排序,使数据呈现出逻辑顺序。 3. **查找与替换**:该软件可能包含查找和替换功能,帮助用户快速定位并修改特定数据,这对于数据清洗尤其有用。 4. **数据合并*...

    从有规律的多个sheets表里提取数据到一个新sheet里.docx

    标题中的“从有规律的多个sheets表里提取数据到一个新sheet里.docx”指的是在Excel工作簿中,有多张按照一定规律排列的表格(sheets),用户希望将这些表格中的特定数据整合到一个新的单独的表格中。这个过程可以...

    extract_三维矩阵提取_matalb_

    若想从任意位置提取数据并改变顺序,可以使用线性索引。线性索引是一种将多维索引转换为单一索引的方法,特别适合在不连续的位置提取数据。假设我们有一个特定的线性索引向量idx,可以通过以下方式提取数据: ```...

    数据结构提取器软件资料

    2. **哈希函数**:在哈希表中,提取器利用哈希函数将数据映射到特定位置,快速查找和提取数据。 3. **排序算法**:如果数据是数组或链表形式,提取器可能会用到快速排序、归并排序、插入排序或冒泡排序等算法来对...

    利用编程实现提取CAD图纸中的料表数据自动生成电子表格的功能

    然而,在传统的管理流程中,各部门如工艺编制、成本预算、物料消耗、物流采购计划等,需依赖人工从CAD图纸或纸质明细表中提取数据,再手动录入Word或Excel,形成各自的明细表。这一过程不仅耗时费力,还常伴随着数据...

    cobol 提取系统时间,按照一定格式输出

    通过上述分析可以看出,此COBOL程序是一个典型的日期时间处理程序,它利用COBOL提供的强大数据处理能力,实现了从系统中获取当前日期时间并按照指定格式输出的功能。这种类型的程序在银行、保险等需要处理大量历史...

    CAD坐标导excel插件.zip_CAD插件_cad_cad excel_cad坐标提取_提取坐标

    CAD坐标导Excel插件是一种高效实用的工具,它专为CAD(计算机辅助设计)用户设计,目的是简化从CAD图纸中提取坐标数据并将其导入到Excel电子表格中的过程。这款插件提高了工作效率,使得设计师和工程师能够快速分析...

    bmp图像数据提取工具:bmp2raw

    由于BMP文件的存储顺序,你可能需要考虑行填充字节,以确保正确地提取数据。 4. **保存为RAW文件**:将提取的像素数据写入一个新的RAW文件,每个像素的RGB值连续存储,不包含任何文件头信息。 5. **错误处理**:...

    从三维数组中提取出任意二维的数据,并保存在新的二维矩阵中

    程序功能:从三维数组中提取出任意二维的数据,并保存在新的二维矩阵中,且能所以变换顺序。

    如何在EXCEL表格中随机提取数据

    在Excel中进行随机数据提取是一项常见的任务,尤其在数据分析、测试样本选取或抽查工作中非常有用。以下是如何在Excel中按照指定条件随机抽取数据的详细步骤,以满足在每个城市抽取10位客户的需求: 1. **插入新列*...

    DATA_Extract_BIL_envi_bil_高光谱_数据提取_

    BIL格式是一种常见的光谱数据存储方式,它按照行(Line)的顺序交错存放各个波段(Band)。这种格式的优点在于,同一行的各个波段数据相邻存储,便于逐行处理,有利于提高数据读取和分析的效率。转换至BIL格式,可以...

    提取txt文件中的部分数据

    提取txt中一定规律的数据,存储的是三角形的点顺序,接着是点坐标,然后提取点坐标

    ArcGIS教程:多边形提取 (空间分析)

    在地理信息系统(GIS)领域,特别是利用ArcGIS软件进行空间数据分析时,多边形提取是一项常用的操作。这项技术允许用户基于特定的多边形边界来选择或提取栅格数据中的像元。本文将详细介绍如何在ArcGIS中执行多边形...

    各类格式视频的音、视频数据提取

    用户可以一次性选择多个视频文件,工具会自动按顺序进行提取,而无需逐个手动操作。 5. **进度查看**:实时查看处理进度是用户体验的重要组成部分。通过进度条或者百分比显示,用户可以了解当前任务的状态,判断还...

Global site tag (gtag.js) - Google Analytics