Table of Contents
oracle查询要求." >借助内存表处理复杂的oracle查询要求.
, 创建测试用表." >, 创建测试用表.
, 方法1 , 使用动态语句拼凑实现." >, 方法1 , 使用动态语句拼凑实现.
, 方法2 使用like查询" >, 方法2 使用like查询
, 方法3 使用instr函数处理" >, 方法3 使用instr函数处理
, 方法4 使用内存表处理" >, 方法4 使用内存表处理
Home Database Mysql Tutorial 借助内存表处理复杂的oracle查询要求

借助内存表处理复杂的oracle查询要求

Jun 07, 2016 pm 03:50 PM
oracle Memory deal with complex Inquire Require

借助内存表处理复杂的 oracle 查询要求 . 在日常业务处理过程中 , 我们经常会碰到一些非常规的查询需求 , 这些需求我们或者可以借助动态语句 , 或者其他现有的 oracle 函数完成查询结果 , 但效率往往差强人意 . 假设我们有一个客户订单业务表 { 订单号 , 订

借助内存表处理复杂的oracle查询要求.

在日常业务处理过程中,我们经常会碰到一些非常规的查询需求, 这些需求我们或者可以借助动态语句,或者其他现有的oracle函数完成查询结果, 但效率往往差强人意.

假设我们有一个客户订单业务表{订单号, 订单客户, 订单日期, 数量, 金额}存储了订单的往来明细数据,订单表中保存最近3个月的往来明细共1000w条记录, 其中客户总量约500000. 并假定在订单表上有针对日期和客户的单独索引.

现在要求提供对任意集合的多个客户的某段时间的订单明细数据.

Select 订单号, 订单客户, 订单日期, 数量, 金额

From 订单业务表

Where 订单日期 between 开始日期 and 结束日期

  And 订单客户 in (客户1, 客户2, 客户3…)

面对这种需求, 我们可以要求前台程序传回三个参数, 开始日期, 结束日期, 客户列表(类似于客户1, 客户2, 客户3, 客户4…)

, 创建测试用表.

Create table t_order_cust(

O_id varchar2(20),

O_customer varchar2(20),

O_date date,

O_qty numeric(18,2),

O_amount numeric(18,2)

);

 

Create index ind_t_order_cust_01 on t_order_cust(o_customer);

Create index ind_t_order_cust_02 on t_order_cust(o_date);

 

, 方法1 , 使用动态语句拼凑实现.

针对上述查询, 我们可以拼凑动态语句实现, 如下代码所示.

Declare

  V_beg_date date := trunc(sysdate,’month’);

  V_end_date date := trunk(sysdate);

  V_cust_str varchar2(1000) := ‘’’客户1’’, ’’客户2’’, ’’客户3’’…’;

  V_sql_str varchar2(2000) ;

Begin

  V_sql_str := ‘select * from t_order_cust

Where o_date between ’ || v_beg_date || ‘ and ’ || v_end_date ||’

And o_customer in (’||v_cust_str||’)’;

  Execute immediate v_sql_str;

End;

根据表明细数据的特点我们知道, 客户索引的选择性为 1000w/50w= 20, 而日期索引的选择性为 1000w/(3*30) = 10w, 明显使用日期索引效率极差, 我们只能选择使用客户上的索引,使用这种处理方法的优势是可以用到客户上的索引, in使用索引的效率相对较差, 并且这种处理方式下, oracle每次执行查询都需要重新建立查询执行树, 也是需要一定的额外开销.

, 方法2 使用like查询

除了上面的拼凑动态执行语句的方法之外, 我们可以设想的到的第二种方法就是借助于oracle提供的like功能. 如下代码所示.这种处理方式下对客户列表字符串的要求跟方法一少有区别.

Declare

  V_beg_date date := trunc(sysdate,’month’);

  V_end_date date := trunk(sysdate);

  V_cust_str varchar2(1000) := ‘客户1, 客户2, 客户3…’;

Begin

  select * from t_order_cust

Where o_date between v_beg_date  and  v_end_date

And v_cust_str like ‘%’||o_customer||’%’;

End;

这种处理方式的优点在于代码书写简单, 但由于对客户索引所在字段o_customer做了拼接处理||, 所以将导致客户索引ind_t_order_cust_01无效, 而只能使用效率较差的日期索引. 在数据量较小, 对效率无法造成影响时这种方法可以接受, 但数据量较大时, 这种方法的缺点将是致命的.

, 方法3 使用instr函数处理

Declare

  V_beg_date date := trunc(sysdate,’month’);

  V_end_date date := trunk(sysdate);

  V_cust_str varchar2(1000) := ‘客户1, 客户2, 客户3…’;

Begin

  select * from t_order_cust

Where o_date between v_beg_date  and  v_end_date

And instr(v_cust_str like ,o_custome) >0

End;

这种处理方式的优缺点跟使用like相似, 同样由于对o_customer使用了函数, 导致该索引不可用, 函数索引同样也不适用于这种情况.

, 方法4 使用内存表处理

我们知道, oracle, sqlserver等关系数据库最善于处理的数据类型是集合, 而不是单独的记录. 同样的100条记录, 如果逐条循环处理和批量处理其效率的差别将是几何单位的.

所以, 为了提高查询效率, 我们这里考虑将给定的客户字符串转变为一个集合或者临时表来处理. Oracle使用全局临时表和复杂数据类型集合来支持这一点.

这里我们介绍一下使用复杂数据类型集合来处理的方式.

首先我们定义一个复杂类型.

create or replace type ctl.type_jax_varc2tab is table of varchar2(2000);

然后定义一个函数实现将给定的字符串转换为嵌套内存表.

CREATEORREPLACEFUNCTION f_jax_str2tab(p_str INVARCHAR2,
p_sep
varchar2default','
)
RETURN ctl.type_jax_varc2tab IS
 
/******************************************************************
  Ver1.0 Created by jaxzhang on 2009-06-08
 
把字符串(1*2*3*4*5)转换为内存表形式
  create or replace type type_jax_varc2tab is table of varchar2(2000);
 
测试用例:SELECT * FROM TABLE(f_jax_str2tab('1*2*3*4*5','*'));
  ******************************************************************/

  v_str
varchar2(2000);
  v_cnt
NUMBER ;
  v_numtab type_jax_varc2tab := type_jax_varc2tab();
--返回内存表
BEGIN
 
select decode(substr(p_str,-1),p_sep,p_str,p_str || p_sep) into v_str from dual;
 
select  length(v_str) - length(REPLACE(v_str, p_sep)) into v_cnt from dual;

 
FOR i IN1 .. v_cnt LOOP
    v_numtab.
EXTEND;
    v_numtab(i) := substr(v_str,
1, instr(v_str, p_sep) - 1);
    v_str := substr(v_str, instr(v_str,p_sep) +
1);
 
ENDLOOP;

 
RETURN v_numtab;
EXCEPTION
 
WHENOTHERSTHEN
    v_numtab.
DELETE;
END;
上述函数的功能就是要将类似于客户1,客户2,客户3’的字符串转换为如下形式.

SELECT * FROMTABLE(f_jax_str2tab('客户1,客户2,客户3',','));

COLUMN_VALUE

客户1

客户2

客户3

得到上述的内存表之后, 我们就可以使用类似于一个表或者视图的方式来与正式表t_order_cust关联得到我们需要的查询结果.

Select /*+ ordered use_nl(a b)*/

  From TABLE(f_jax_str2tab('客户1,客户2,客户3',',')) a,

        T_order_cust b

   Where b.o_customer = a.column_value

 

 

 

 

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Large memory optimization, what should I do if the computer upgrades to 16g/32g memory speed and there is no change? Large memory optimization, what should I do if the computer upgrades to 16g/32g memory speed and there is no change? Jun 18, 2024 pm 06:51 PM

For mechanical hard drives or SATA solid-state drives, you will feel the increase in software running speed. If it is an NVME hard drive, you may not feel it. 1. Import the registry into the desktop and create a new text document, copy and paste the following content, save it as 1.reg, then right-click to merge and restart the computer. WindowsRegistryEditorVersion5.00[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SessionManager\MemoryManagement]"DisablePagingExecutive"=d

How long will Oracle database logs be kept? How long will Oracle database logs be kept? May 10, 2024 am 03:27 AM

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The order of the oracle database startup steps is The order of the oracle database startup steps is May 10, 2024 am 01:48 AM

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

Sources say Samsung Electronics and SK Hynix will commercialize stacked mobile memory after 2026 Sources say Samsung Electronics and SK Hynix will commercialize stacked mobile memory after 2026 Sep 03, 2024 pm 02:15 PM

According to news from this website on September 3, Korean media etnews reported yesterday (local time) that Samsung Electronics and SK Hynix’s “HBM-like” stacked structure mobile memory products will be commercialized after 2026. Sources said that the two Korean memory giants regard stacked mobile memory as an important source of future revenue and plan to expand "HBM-like memory" to smartphones, tablets and laptops to provide power for end-side AI. According to previous reports on this site, Samsung Electronics’ product is called LPWide I/O memory, and SK Hynix calls this technology VFO. The two companies have used roughly the same technical route, which is to combine fan-out packaging and vertical channels. Samsung Electronics’ LPWide I/O memory has a bit width of 512

Kingbang launches new DDR5 8600 memory, offering CAMM2, LPCAMM2 and regular models to choose from Kingbang launches new DDR5 8600 memory, offering CAMM2, LPCAMM2 and regular models to choose from Jun 08, 2024 pm 01:35 PM

According to news from this site on June 7, GEIL launched its latest DDR5 solution at the 2024 Taipei International Computer Show, and provided SO-DIMM, CUDIMM, CSODIMM, CAMM2 and LPCAMM2 versions to choose from. ▲Picture source: Wccftech As shown in the picture, the CAMM2/LPCAMM2 memory exhibited by Jinbang adopts a very compact design, can provide a maximum capacity of 128GB, and a speed of up to 8533MT/s. Some of these products can even be stable on the AMDAM5 platform Overclocked to 9000MT/s without any auxiliary cooling. According to reports, Jinbang’s 2024 Polaris RGBDDR5 series memory can provide up to 8400

Oracle database server hardware configuration requirements Oracle database server hardware configuration requirements May 10, 2024 am 04:00 AM

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

How much memory does oracle require? How much memory does oracle require? May 10, 2024 am 04:12 AM

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

Lexar God of War Wings ARES RGB DDR5 8000 Memory Picture Gallery: Colorful White Wings supports RGB Lexar God of War Wings ARES RGB DDR5 8000 Memory Picture Gallery: Colorful White Wings supports RGB Jun 25, 2024 pm 01:51 PM

When the prices of ultra-high-frequency flagship memories such as 7600MT/s and 8000MT/s are generally high, Lexar has taken action. They have launched a new memory series called Ares Wings ARES RGB DDR5, with 7600 C36 and 8000 C38 is available in two specifications. The 16GB*2 sets are priced at 1,299 yuan and 1,499 yuan respectively, which is very cost-effective. This site has obtained the 8000 C38 version of Wings of War, and will bring you its unboxing pictures. The packaging of Lexar Wings ARES RGB DDR5 memory is well designed, using eye-catching black and red color schemes with colorful printing. There is an exclusive &quo in the upper left corner of the packaging.

See all articles