Finding blocking/locking queries in MS SQL (mssql) - sql-server

Using sys.dm_os_wait_stats I have identified what I believe is a locking problem
wait type waittime pct running ptc
LCK_M_RS_S 2238.54 22.14 22.14
LCK_M_S 1980.59 19.59 41.73
Is there a way I can find the top blocking/locking queries? I've tried querying sys.dm_db_index_operational_stats without much luck.

You may find this query useful:
SELECT *
FROM sys.dm_exec_requests
WHERE DB_NAME(database_id) = 'YourDBName'
AND blocking_session_id <> 0
To get the query itself use this one:
SELECT text,*
FROM sys.dm_exec_requests
CROSS APPLY sys.dm_exec_sql_text(sql_handle)
WHERE DB_NAME(database_id) = 'YourDBName'
AND blocking_session_id <> 0

I found this query which helped me find my locked table and query causing the issue.
SELECT L.request_session_id AS SPID,
DB_NAME(L.resource_database_id) AS DatabaseName,
O.Name AS LockedObjectName,
P.object_id AS LockedObjectId,
L.resource_type AS LockedResource,
L.request_mode AS LockType,
ST.text AS SqlStatementText,
ES.login_name AS LoginName,
ES.host_name AS HostName,
TST.is_user_transaction as IsUserTransaction,
AT.name as TransactionName,
CN.auth_scheme as AuthenticationMethod
FROM sys.dm_tran_locks L
JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
JOIN sys.objects O ON O.object_id = P.object_id
JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE resource_database_id = db_id()
ORDER BY L.request_session_id

Use the script: sp_blocker_pss08 or SQL Trace/Profiler and the Blocked Process Report event class.

Related

How to delete partitions?

I run the query
SELECT so.name, p.*
FROM sys.partitions p
INNER JOIN sys.sysobjects so ON p.object_id = so.id
WHERE partition_number > 1
OPTION (RECOMPILE);
and I get the 170 rows as result, like
ifts_comp_fragment_1336391830_19755845 ....
As I understand that are FTS internal partitions.
How can I delete all of FTS partitions?

Which DMV to get database id for sys.dm_os_waiting_tasks

Which DMV do I have to join with sys.dm_os_waiting_tasks to get the database ID?
You can try something as follows:
SELECT TOP 100 w.session_id, e.database_id, D.name
FROM sys.dm_os_waiting_tasks w
LEFT JOIN sys.dm_exec_sessions e ON w.session_id = e.session_id
LEFT JOIN sys.databases d ON e.database_id = d.database_id

How to determine when SQL Server index statistics were last updated

I want to check which index stats that hasn't been updated on tables that has page count of 500 mb and more.
I have this script:
SELECT OBJECT_NAME(object_id) AS [ObjectName]
,[name] AS [StatisticName]
,STATS_DATE([object_id], [stats_id]) AS [StatisticUpdateDate]
FROM sys.stats
order by StatisticUpdateDate desc
Its not giving me all the info I want. Can someone be able to modify my script or show me how to get where I want to be?
Thanks in advance.
Here is a solution for 500 pages even if I think 500Mb is more reasonable finter:
with cte as
(
select p.object_id,
object_name(p.object_id) as obj,
sum(total_pages) as tot_pages
from sys.partitions p join sys.allocation_units au
on au.container_id = p.hobt_id
group by p.object_id
)
SELECT c.* ,
OBJECT_NAME(st.object_id) AS [ObjectName]
,[name] AS [StatisticName]
,STATS_DATE(st.[object_id],
[stats_id]) AS [StatisticUpdateDate]
FROM sys.stats st join cte c
on st.object_id = c.object_id
where tot_pages >= 500;

What is the SQL Server system table that contains information about stored procedure parameters?

What is the SQL Server system table that contains information about stored procedure parameters with it's information like datatype, name, lenght, null or not?
thanks
You can query sys.procedures and sys.parameters...
select pr.name, p.*
from sys.procedures pr
inner join sys.parameters p on pr.object_id = p.object_id
And join to types too...
select pr.name, p.*, t.name, t.max_length
from sys.procedures pr
inner join sys.parameters p on pr.object_id = p.object_id
inner join sys.types t on p.system_type_id = t.system_type_id
You can also use
select * from INFORMATION_SCHEMA.PARAMETERS
You can also use
select * from INFORMATION_SCHEMA.ROUTINES \G;
SELECT
p.name AS [SP Name], qs.execution_count,
ISNULL(qs.execution_count/DATEDIFF(Second, qs.cached_time, GETDATE()), 0) AS [Calls/Second],
qs.total_worker_time/qs.execution_count AS [AvgWorkerTime], qs.total_worker_time AS [TotalWorkerTime],
qs.total_elapsed_time, qs.total_elapsed_time/qs.execution_count AS [avg_elapsed_time],
qs.cached_time,
isr.ROUTINE_DEFINITION
FROM sys.procedures AS p
INNER JOIN sys.dm_exec_procedure_stats AS qs
ON p.[object_id] = qs.[object_id]
Inner Join INFORMATION_SCHEMA.ROUTINES as ISR
on p.name = isr.SPECIFIC_NAME
WHERE qs.database_id = DB_ID()
and
isr.ROUTINE_DEFINITION like '%XXXX%' -- name of something you are looking for in the stored procedure
ORDER BY qs.execution_count DESC OPTION (RECOMPILE);
For those using SQL 2000
SELECT * FROM [dbName]..sysobjects WHERE xtype = 'P'
and
SELECT * FROM [dbName]..sysComments
Join them by id
This shows information about stored procedures and user-defined functions. It will include procedures/scalar functions without parameters, but will include parameter information if available.
SELECT
O.Name ProcedureName,
P.parameter_id Position,
P.Name ParameterName,
P.max_length MaxLen,
P.is_output IsOutput,
P.has_default_value HasDefaultValue,
P.default_value DefaultValue,
P.is_nullable IsNullable,
T.name TypeName,
T.max_length TypeMaxLen,
O.type_desc TypeDescription
FROM SYS.OBJECTS O
LEFT JOIN SYS.PARAMETERS P ON O.object_id = P.object_id
LEFT JOIN SYS.TYPES T ON P.system_type_id = T.system_type_id
WHERE type_desc LIKE 'SQL_%'
ORDER BY O.Name, P.parameter_id, P.Name

user permission list

Can anybody help me on this?
How to get the database level permission users list for a particular database in SQL Server 2005?
Simple Google search, with query taken from the first result:
select dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc
from sys.database_permissions p
left OUTER JOIN sys.all_objects o
on p.major_id = o.OBJECT_ID
inner JOIN sys.database_principals dp
on p.grantee_principal_id = dp.principal_id
Modified slightly to include the database name as well:
select dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc,
d.[name] as database_name
from sys.database_permissions p
left OUTER JOIN sys.all_objects o on p.major_id = o.OBJECT_ID
inner JOIN sys.database_principals dp on p.grantee_principal_id = dp.principal_id
left outer JOIN sys.sysdatabases d on d.sid = dp.sid

Resources