Detailed explanation of performance issues of cursor scripts (1)

Source: Internet
Author: User

Instances with performance impact of the cursor type. The following two cursor scripts create and execute two types of cursor: dynamic and fast forward only.

Knowledge supplement:

Operations in a relational database work for the entire row set. The row set returned by the SELECT statement includes all rows that meet the conditions in the WHERE clause of the statement. The complete row set returned by the statement is called the result set. The application does not always take the entire result set as a unit for effective processing. These applications require a mechanism to process one or a small number of rows at a time. A cursor not only provides this mechanism, but also an extension of the result set.

The cursor expands the result set processing by performing the following operations:

Unsatisfactory cursor type: (dynamic Cursor)

 
 
  1. declare @p1 int  set @p1=NULL    
  2. declare @p2 int  set @p2=0    
  3. declare @p5 int  set @p5=4098  
  4. declare @p6 int  set @p6=8193    
  5. declare @p7 int  set @p7=0    
  6.  
  7. exec sp_cursorprepexec @p1 output,@p2 output,N'@P1 varchar(30),@P2 varchar(15)',  
  8. N'  
  9. SELECT       T1.CONFLICT_ID  
  10. FROM         dbo.S_AUDIT_ITEM T1              
  11. LEFT OUTER JOIN dbo.S_USER T2   
  12. ON T1.USER_ID = T2.PAR_ROW_ID      
  13. WHERE  ((T1.BC_BASE_TBL = @P1)    
  14. AND  (T1.RECORD_ID = @P2))      
  15. ORDER BY  T1.OPERATION_DT DESC    
  16. OPTION (FAST 40)  
  17. ',  
  18. @p5 output,@p6 output,@p7 output,'1-10350J','S_PARTY'    
  19.  
  20. print 'fetch' 
  21. exec sp_cursorfetch @p2,2,4,1    
  22.  
  23. exec sp_cursorclose @p2 

Ideal cursor type (fast forward only cursor)

 
 
  1. declare @p1 int  set @p1=NULL    
  2. declare @p2 int  set @p2=0    
  3. declare @p5 int  set @p5=4112  
  4. declare @p6 int  set @p6=8193    
  5. declare @p7 int  set @p7=0    
  6.  
  7. exec sp_cursorprepexec @p1 output,@p2 output,N'@P1 varchar(30),@P2 varchar(15)',  
  8. N'  
  9. SELECT       T1.CONFLICT_ID  
  10. FROM         dbo.S_AUDIT_ITEM T1              
  11. LEFT OUTER JOIN dbo.S_USER T2   
  12. ON T1.USER_ID = T2.PAR_ROW_ID      
  13. WHERE  ((T1.BC_BASE_TBL = @P1)    
  14. AND  (T1.RECORD_ID = @P2))      
  15. ORDER BY  T1.OPERATION_DT DESC    
  16. OPTION (FAST 40)  
  17. ',  
  18. @p5 output,@p6 output,@p7 output,'S_SRV_REQ','1-WUQTM6'    
  19.  
  20. select @p1, @p2, @p5, @p6, @p7  
  21.  
  22. print '2' 
  23. exec sp_cursorfetch @p2,2,1,1    
  24. print '3' 
  25. exec sp_cursorclose @p2 

Note: For cursor-related stored procedures in the script, see: http://jtds.sourceforge.net/apiCursors.html#_sp_cursorprepexec

I. how to interpret the cursor type

 
 
  1. sp_cursorprepexec [@handle =] statement_handle OUTPUT,  
  2.      [@cursor =] cursor_handle OUTPUT,  
  3.      [@paramdef =] N'parameter_name data_type, [,...n]'   
  4.      [@stmt =] N'stmt',  
  5.      [, [@scrollopt =] scroll_options OUTPUT]  
  6.      [, [@ccopt =] concurrency_options OUTPUT]  
  7.      [, [@rowcount =] rowcount OUTPUT]  
  8.  
  9. @scrollopt  

 

[@ Ccopt

 

@ P5 = 4098 convert to hexadecimal 1002 and the corresponding cursor type is Parameterized query + Dynamic cursor.

@ P5 = 4112 convert to hexadecimal 1010 and the corresponding cursor type is Parameterized query + Fast forward-only cursor.

The problem is that in the cursor type on the left, the script execution time is much longer than the cursor type on the right.


Related Article

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.