Do not use a cursor unless you have.

Source: Internet
Author: User

Both SQL Server and Oracle provide cursors. The general purpose is to traverse data from the query structure. Cursors are easy to use.ProgramUsers like to use them. However, misuse of cursors will have a great impact on program performance. In my opinion, do not use a cursor unless you have. The following is my optimization experience.
Problem:
In our system, we need to manage product models. People familiar with PDM know that product models can be processed as special components, therefore, when using a data table, the product model and parts use the same data table to store data, except that a flag is used to identify the component or product model. The product model has a version. The requirement is to find all the latest parts and allow users to customize the query conditions during query.
User-Defined query conditions are constructed through various options on the interface. Therefore, dynamic SQL statements are passed. To improve the efficiency, a stored procedure is created on the database to query the product model. The following is a stored procedure written by Oracle programmers who are new to Oracle:

1 Procedure Getprodmodelbysql (p_ SQL In   Varchar2 ,
2 Cur_mi_prodmodel out sys_base.basecursor) As
3 Sqlstr Varchar2 ( 1000 );
4 Idstr Varchar2 ( 1000 );
5
6
7 Dis_id Number ;
8 Maxver Number ;
9 Cursor Disid_curs Is
10 Select   Distinct Pt_id
11 From Mi_part
12 Where Pt_flag =   2 ;
13 Begin
14 Open Disid_curs;
15 Loop
16 Fetch Disid_curs
17 Into Dis_id;
18 Exit   When Disid_curs % Notfound;
19
20 Select   Max (Pv_id)
21 Into Maxver
22 From Mi_partver
23 Where Pv_partid = Dis_id;
24
25 If Maxver Is   Null   Then
26 Idstr: = Idstr;
27 Else
28 Idstr: = Idstr | Maxver |   ' , ' ;
29 End   If ;
30 End Loop;
31
32 Idstr: =   Rtrim (Idstr, ' , ' );
33 Idstr: =   Ltrim (Idstr, ' , ' );
34 If Idstr Is   Null   Then
35 Sqlstr: = P_ SQL |   ' Order by pv_productid, pt_partnumber ' ;
36 Else
37 Sqlstr: = P_ SQL |   ' And mi_partver.pv_id in ( '   | Idstr |   ' ) Order by pv_productid, pt_partnumber ' ;
38 End   If ;
39
40 Open Cur_mi_prodmodel For Sqlstr;
41 End Getprodmodelbysql;

This storage process can work normally, but its speed is very slow. If there is a lot of data in the data table, its speed may be decreased in a straight line, the reason is that the cursor is used to traverse and construct a query statement. Optimize the stored procedure: 1 Procedure Getprodmodelbysql (p_ SQL In   Varchar2 ,
2 Cur_mi_prodmodel out sys_base.basecursor) As
3 S Varchar2 ( 300 );
4 Sqlstr Varchar2 ( 1000 );
5 /**/ /* Idstr varchar2 (1000 );
6
7
8 Dis_id number;
9 Maxver number;
10 Cursor disid_curs is
11 Select distinct pt_id
12 From mi_part
13 Where pt_flag = 2; */

14 Begin
15 /**/ /* Open disid_curs;
16 Loop
17 Fetch disid_curs
18 Into dis_id;
19 Exit when disid_curs % notfound;
20
21 Select max (pv_id)
22 Into maxver
23 From mi_partver
24 Where pv_partid = dis_id;
25
26 If maxver is null then
27 Idstr: = idstr;
28 Else
29 Idstr: = idstr | maxver | ',';
30 End if;
31 End loop; */

32 S: = ' And mi_partver.pv_id in (select a. pv_id from mi_partver A, mi_part c Where a. pv_partid = c.pt _ id and c.pt _ flag = 2 and
33 A. pv_id in (select e. pv_id from mi_partver E
34 Where E. pv_id in (select max (B. pv_id) from mi_partver B where E. pv_partid = B. pv_partid ))) ' ;
35 /**/ /* Idstr: = rtrim (idstr ,',');
36 Idstr: = ltrim (idstr ,',');
37 If idstr is null then
38 Sqlstr: = p_ SQL | 'order by pv_productid, pt_partnumber ';
39 Else
40 Sqlstr: = p_ SQL | 'and mi_partver.pv_id in (' | idstr | ') order by pv_productid, pt_partnumber ';
41 End if; */

42 Sqlstr: = P_ SQL | S | ' Order by pv_productid, pt_partnumber ' ;
43 Open Cur_mi_prodmodel For Sqlstr;
44 End Getprodmodelbysql;

The valid statement here is to use a query statement to construct a query. The result is that the query speed is improved by an order of magnitude, and a hidden danger is that the buffer overflow, because several varchar2 (1000) variables are defined in the original stored procedure, the upper bound of this variable is not predictable, buffer overflow is also a problem that many Oracle beginners often ignore or even think.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.