sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Exploring FETCH API CURSOR and sp_cursorfetch

Antonios Chatzipavlis
Wednesday 16 March 2022

Overview

Δεν είναι σπάνιες οι φορές όταν κάνουμε performance tuning investigation χρησιμοποιώντας διάφορα εργαλεία όπως Profiler, sp_whoisactive, dbcc inputbuffer να συναντάμε σαν query text είτε FETCH API CURSOR είτε exec sp_cursorfetch με κάποιες παραμέτρους. Φυσικά με αυτά δεν βγάζουμε άκρη. Για αυτό στο άρθρο αυτό θα σας εξηγήσω πως μπορείτε να βγάλετε άκρη και κυρίως να δείτε το πραγματικό query που εκτελείτε πίσω από αυτά.

Explain by sample


Sample
image

Όπως βλέπουμε στην παραπάνω εικόνα έχω εκτελέσει την δημοφιλή sp_whoisactive του Adam Machanic και στο sql_text βλέπω το FETCH API CURSOR.

Αυτό όμως δεν μου δείχνει την πραγματικά εκτελείται και για να το βρω θα πρέπει να κάνω query στο sys.dm_exec_cursors DMF με παράμετρο το session id.

Το DMF αυτό μου δίνει αρκετές πληροφορίες για το cursor και μπορείτε να δείτε περισσότερα στα BOL.

Από αυτό μας ενδιαφέρει το sql_handle το οποίο χρησιμοποιώ σας παράμετρο στο αρκετά γνωστό DMV sys.dm_exec_sql_text στου οποίου τα αποτελέσματα το text field περιέχει το πραγματικό statement που εκτελείται.

All in one query

Αν τώρα όλα αυτά τα ενώσουμε μεταξύ τους τότε έχουμε το παρακάτω query που μας δίνει άμεσα τις πληροφορίες που χρειαζόμαστε.

SQL Script

select 
    creation_time
,    cursor_id
,    c.session_id
,    c.properties
,    c.creation_time
,    c.is_open
,    substring(st.text, 
              (c.statement_start_offset/2)+1, 
              ((case c.statement_end_offset 
                when -1 then datalength(st.text) 
                else c.statement_end_offset
                end - c.statement_start_offset) / 2) + 1) as statement_text
from   
    sys.dm_exec_cursors(0) as c
inner join 
    sys.dm_exec_sessions as s on c.session_id = s.session_id
cross apply 
    sys.dm_exec_sql_text(c.sql_handle) as st
go

Query Results
image

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.