当前位置:网站首页>Requirements for SQL server to retrieve SQL and user information
Requirements for SQL server to retrieve SQL and user information
2022-04-23 05:21:00 【bisal(Chen Liu)】
Oracle If you need to know one thing SQL Who did it , Can pass v$sql Of parsing_schema_name Field to get the login schema name , amount to SQL And session login information are bound .
But recently, there was a SQL Server The needs of , Need to know history SQL The executor of the .
as follows SQL, Can find the current SQL Server I ran SQL, But no user information ,
SELECT p.refcounts, p.usecounts, sqltext.text
FROM sys.dm_exec_cached_plans p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) sqltext
WHERE p.objtype IN ('Adhoc', 'Prepared')
AND p.cacheobjtype = 'Compiled Plan'
AND sqltext.text NOT LIKE '%sys%'
AND sqltext.text NOT LIKE '%fn_listextendedproperty%'
AND upper(sqltext.text) LIKE 'SELECT%'
AND upper(db_name(sqltext.dbid)) = 'XXX'
ORDER BY sqltext.text
Find... On the official website DMV There was a sys.dm_exec_sessions, Which field login_name, It should refer to the login user name ,
https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-sessions-transact-sql?view=sql-server-ver15

But I can talk to you sys.dm_exec_sql_text Only database_id, The following result should be a Cartesian product , Not yet SQL and login_name Associate user information , So it still doesn't meet the demand ,
SELECT sessions.login_name, p.refcounts, p.usecounts, sqltext.text
FROM sys.dm_exec_cached_plans p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) sqltext
CROSS APPLY sys.dm_exec_sessions sessions
WHERE p.objtype IN ('Adhoc', 'Prepared')
AND p.cacheobjtype = 'Compiled Plan'
AND sqltext.text NOT LIKE '%sys%'
AND sqltext.text NOT LIKE '%fn_listextendedproperty%'
AND upper(sqltext.text) LIKE 'SELECT%'
AND upper(db_name(sqltext.dbid)) = 'XXX'
AND sessions.database_id = sqltext.dbid
ORDER BY sqltext.text;
In addition, I found this statement ,
SELECT a.[request_session_id] AS [ conversation ID] ,
CASE [b].[status]
WHEN 'dormant' THEN 'dormant( Reset session )'
WHEN 'running' THEN 'running( In execution )'
WHEN 'sleeping' THEN 'sleeping( In sleep )'
WHEN 'background' THEN 'background( The background to perform )'
WHEN 'rollback' THEN 'rollback( Transaction rollback )'
WHEN 'pending' THEN 'pending( The session becomes available )'
WHEN 'runnable' THEN 'runnable( Executable )'
WHEN 'scheduler' THEN 'scheduler( Execute executable queue )'
WHEN 'spinloop' THEN 'spinloop( Wait for the spin lock to become available )'
WHEN 'suspended' THEN 'spinloop( Wait for the event to complete )'
ELSE [b].[status]
END AS [ Process status ] ,
CONVERT(VARCHAR(100), DB_NAME([b].dbid)) AS [ Database name ] ,
[qt].[text] AS [ Executing statement ] ,
CONVERT(VARCHAR(100), [b].hostname) AS [ Host name ] ,
d.client_net_address AS [IP Address ],
[b].loginame AS [ Login name ] ,
[c].start_time AS [ Start execution time ],
CASE a.[request_mode]
WHEN 'Sch-S' THEN 'Sch-S( Architecture stability )'
WHEN 'S' THEN 'S( share )'
WHEN 'U' THEN 'U( to update )'
WHEN 'X' THEN 'X( Exclusive )'
WHEN 'IS' THEN 'IS( Intention to share )'
WHEN 'IU' THEN 'IU( Intention update )'
WHEN 'IX' THEN 'IX( Intending to be exclusive )'
WHEN 'BU' THEN 'BU( High capacity operation )'
WHEN 'RangeS_S' THEN 'RangeS_S( Shared key range and shared resource lock )'
WHEN 'RangeS_U' THEN 'RangeS_U( Share key ranges and update resource locks )'
WHEN 'RangeI_N' THEN 'RangeI_N( Insert key range and empty resource lock )'
WHEN 'RangeI_S' THEN 'RangeI_S(RangeI_N and S Conversion lock )'
WHEN 'RangeI_U' THEN 'RangeI_U(RangeI_N and U Conversion lock )'
WHEN 'RangeI_X' THEN 'RangeI_X(angeI_N and X Conversion lock )'
WHEN 'RangeX_S' THEN 'RangeX_S(RangeI_N and RangeS_S Conversion lock )'
WHEN 'RangeX_U' THEN 'RangeX_U(RangeI_N and RangeS_U Conversion lock )'
WHEN 'RangeX_X' THEN 'RangeX_X( Exclusive key range and exclusive resource lock )'
ELSE a.[request_mode]
END AS [ Request lock mode ] ,
CASE a.[request_status]
WHEN 'GRANTED' THEN 'GRANTED( Has been awarded )'
WHEN 'CONVERT' THEN 'CONVERT( Switching )'
WHEN 'WAIT' THEN 'WAIT( Waiting for the )'
ELSE a.[request_status]
END AS [ Request status ] ,
b.blocked AS [ Blocking a session ID] ,
CONVERT(VARCHAR(100), SUSER_NAME([b].uid)) AS [ Perform user ] ,
CONVERT(VARCHAR(100), [b].program_name) AS [ Application name ] ,
CONVERT(VARCHAR(MAX), [b].cmd) AS [ Executing order ] ,
CASE a.[resource_type]
WHEN 'DATABASE' THEN 'DATABASE( database )'
WHEN 'FILE' THEN 'FILE( file )'
WHEN 'OBJECT' THEN 'OBJECT( object )'
WHEN 'PAGE' THEN 'PAGE( page )'
WHEN 'KEY' THEN 'KEY( Index key )'
WHEN 'EXTENT' THEN 'EXTENT()'
WHEN 'RID' THEN 'RID( Line identification )'
WHEN 'APPLICATION' THEN 'APPLICATION( Applications )'
ELSE a.[resource_type]
END AS [ The resource type ] ,
CASE WHEN a.[resource_database_id] = DB_ID()
AND a.[resource_type] = 'OBJECT'
THEN CONVERT(VARCHAR(200), OBJECT_NAME(a.resource_Associated_Entity_id))
ELSE CONVERT(VARCHAR(200), a.resource_Associated_Entity_id)
END AS [ Associate resource objects ] ,
a.[request_reference_count] AS [ Number of requests ] ,
CASE a.[request_owner_type]
WHEN 'TRANSACTION' THEN 'TRANSACTION( Business )'
WHEN 'CURSOR' THEN 'CURSOR( The cursor )'
WHEN 'SESSION' THEN 'SESSION( The user's session )'
WHEN 'SHARED_TRANSACTION_WORKSPACE'
THEN 'SHARED_TRANSACTION_WORKSPACE( Transaction workspace sharing )'
WHEN 'EXCLUSIVE_TRANSACTION_WORKSPACE'
THEN 'EXCLUSIVE_TRANSACTION_WORKSPACE( The business workspace is exclusive )'
ELSE a.[request_owner_type]
END AS [ Request entity type ] ,
STR([b].cpu, 7) AS [ Cumulative CPU Time ] ,
STR([b].physical_io, 7) AS [ At present IO( byte )]
FROM [sys].[dm_tran_locks] a WITH ( NOLOCK )
INNER JOIN sys.sysprocesses b WITH ( NOLOCK ) ON a.[request_session_id] = [b].[spid]
INNER JOIN sys.dm_exec_requests c WITH ( NOLOCK ) ON [c].[session_id] = [b].[spid]
INNER JOIN sys.dm_exec_connections d WITH ( NOLOCK ) ON d.session_id = a.request_session_id
CROSS APPLY sys.dm_exec_sql_text(c.[sql_handle]) AS qt
ORDER BY request_session_id ,
resource_database_id DESC
He can find what is currently being implemented SQL And conversation information , Just from the content , To meet the requirements , But in scope , It's just the current execution , Without history .
Consulted the master , The reply is ,SQL Server Cannot pass DMV View to query the execution history of a session SQL, You can only collect data that the current session is executing SQl, Keep collecting and then save it .
No matter from the monitoring granularity , Or from the perspective of data statistics ,SQL Associated with user information retrieval is still useful , Can achieve more fine control , It's not clear why Microsoft didn't officially give such a design , Or there are other hidden functions ?
Recently updated articles :
《MySQL A case of implicit conversion 》
《Linux Comparison of several data transmission tools 》
《 Xiaobai studies MySQL - There are some differences between users created by different versions 》
《 Recent problems 》
《Linux Retrieval of process information 》
Article classification and indexing :
版权声明
本文为[bisal(Chen Liu)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230519464866.html
边栏推荐
- MFC实现资源单独Dll实现
- Study notes: unity customsrp-10-point and spot shadows
- Study notes: unity customsrp-11-post processing --- bloom
- 点击添加按钮--出现一个框框(类似于添加学习经历-本科-研究生)
- C test calls the paddlesharp module to recognize pictures and words
- Power consumption parameters of Jinbei household mute box series
- Semi synchronous replication of MariaDB
- MySQL foreign key constraint
- TSlint注释忽略错误和RESTful理解
- 引入精益管理方式,需要提前做到这九点
猜你喜欢

Master-slave replication of MariaDB database

Solution of how to log in with mobile phone verification code in wireless network

Redis persistence

Let the LAN group use the remote device

Basic use of sqlyog

2021 年 25 大 DevOps 工具(下)

MFC实现资源单独Dll实现

Uniapp wechat sharing

了解 DevOps,必读这十本书!

Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
随机推荐
Three 之 three.js (webgl)简单实现根据点绘制线/弧线(基于LineGeometry / Line2 / LineMaterial,绘制两点基于圆心的弧线段)
Power consumption parameters of Jinbei household mute box series
4 most common automated test challenges and Countermeasures
Grpc long connection keepalive
2021-10-25
Data management of basic operation of mairadb database
Laravel database
How to add beautiful code blocks in word | a very complete method to sort out and compare
Asynchronous iterator & asynchronous generator & asynchronous context manager
Low code and no code considerations
Mariadb的半同步复制
4 个最常见的自动化测试挑战及应对措施
The 2021 IT industry project management survey report was released!
云计算与云原生 — OpenShift 的架构设计
学习笔记:Unity CustomSRP-11-Post Processing---Bloom
MFC implementation resources are implemented separately by DLL
TSlint注释忽略错误和RESTful理解
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
My old programmer's perception of the dangers and opportunities of the times?