How to find out what table a page lock belongs to - sql-server

I'm using the sys.dm_tran_locks view to check what areas of my database have locks when we are having performance problems.
Using this view....
If the resource_type is database I can use the DB_NAME function to find out what database has the lock.
If its an object I can normally join to sys.tables to check what table it is.
However if the resource_type is Page or Key is there any way to trace this back to its parent table so I can get a good idea of which tables are locking?

This is what the resource_associated_entity_id column is for (Example query).
SELECT dm_tran_locks.request_session_id,
dm_tran_locks.resource_database_id,
DB_NAME(dm_tran_locks.resource_database_id) AS dbname,
CASE
WHEN resource_type = 'OBJECT'
THEN OBJECT_NAME(dm_tran_locks.resource_associated_entity_id)
ELSE OBJECT_NAME(partitions.OBJECT_ID)
END AS ObjectName,
partitions.index_id,
indexes.name AS index_name,
dm_tran_locks.resource_type,
dm_tran_locks.resource_description,
dm_tran_locks.resource_associated_entity_id,
dm_tran_locks.request_mode,
dm_tran_locks.request_status
FROM sys.dm_tran_locks
LEFT JOIN sys.partitions ON partitions.hobt_id = dm_tran_locks.resource_associated_entity_id
LEFT JOIN sys.indexes ON indexes.OBJECT_ID = partitions.OBJECT_ID AND indexes.index_id = partitions.index_id
WHERE resource_associated_entity_id > 0
AND resource_database_id = DB_ID()
ORDER BY request_session_id, resource_associated_entity_id

You've got to find the object_id associated with that resource, and it may involve joining to another table. For example,
SELECT *, OBJECT_NAME(p.object_id)
FROM sys.dm_tran_locks l
JOIN sys.partitions p
ON l.resource_associated_entity_id = p.hobt_id
WHERE resource_type = 'KEY'
Look up sys.dm_tran_locks in Books Online to figure out what the joining tables should be for each resource.

Related

SQL Server query to find tables across all databases, that references a column in a table?

I want to find the tables from all the databases that references a column in a table in one of the database. Can anyone help me here?
Not sure how to proceed
this should do, please see that i have used like operator (where col.name like '%COLUMN_NAME_HERE%'), so it will find matching patterns, if you want to find exact match then please use (where col.name = 'COLUMN_NAME_HERE')
select
schema_name(tab.schema_id) as schema_name
,tab.name as table_name
, col.column_id,col.name as column_name
, t.name as data_type, col.max_length, col.precision
from sys.tables as tab
inner join sys.columns as col on tab.object_id = col.object_id
left join sys.types as t on col.user_type_id = t.user_type_id
where col.name like '%COLUMN_NAME_HERE%'
order by schema_name,table_name, column_id*

Check if any database table has any rows

I am trying to understand how to check if any table in my db has data using entity framework. I can check for one table but how can I check for all tables at once? DO we have any option with ef6?
using (var db = new CreateDbContext())
{
if(!db.FirstTable.Any())
{
// The table is empty
}
}
Any pointers on how to loop through entities would be helpful.
Here is one way you could do this with t-sql. This should be lightning fast on most systems. This returned in less than a second on our ERP database. It stated 421 billion rows in more than 15,000 partition stats.
select sum(p.row_count)
from sys.dm_db_partition_stats p
join sys.objects o on o.object_id = p.object_id
where o.type not in ('S', 'IT') --excludes system and internal tables.
Similar to #SeanLange, but shows schema name and table name for tables without any rows.
SELECT Distinct OBJECT_SCHEMA_NAME(p.object_id) AS [Schema],
OBJECT_NAME(p.object_id) AS [Table]
FROM sys.partitions p
INNER JOIN sys.indexes i
ON p.object_id = i.object_id
AND p.index_id = i.index_id
WHERE OBJECT_SCHEMA_NAME(p.object_id) != 'sys'
And p.Rows = 0
ORDER BY [Schema], [Table]

given a foreign key, how do i find all tables that use that particular key?

i have a column (myColumn) that serves as the primary key of a table (tableA).
i've noticed that 2 or 3 tables reference this myColumn as foreign key.
how do i detect all tables that use and reference myColumn?
im guessing that more than 3 tables use myColumn because when i tried updating it like this
UPDATE tableA
SET myColumn = 1
WHERE myColumn = 1
6 rows were updated.
it was earlier suggested to me to use
sp_helpconstraint('your_table_name')
but i then found out that this does not give the complete information that i need.
any other suggestions?
Try this - this is the more up-to-date, SQL Server 2005 and newer version of my original answer that Mitch linked to (that was for SQL Server 2000):
SELECT
ConstraintName = fk.name,
TableName = t.name,
ColumnName = c.name
FROM
sys.foreign_keys fk
INNER JOIN
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
sys.tables t ON fk.parent_object_id = t.object_id
INNER JOIN
sys.columns c ON fkc.parent_object_id = c.object_id AND fkc.parent_column_id = c.column_id
INNER JOIN
sys.tables tref ON fk.referenced_object_id = tref.object_id
INNER JOIN
sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id
WHERE
tref.Name = 'Person'
AND cref.Name = 'OID'
It uses the system catalog views sys.foreign_keys and sys.foreign_key_columns to find out which tables/columns reference that table and column you're interested in.
You just basically type in the table name and the column name in the WHERE clause - and you get your list of other tables/columns referencing that table/column
If you install Redgate's free sql search tool you can easily search your database schema for strings such as tables and column names.
http://www.red-gate.com/products/sql-development/sql-search/
There are other ways to search the schema using system tables but I highly recommend this tool. It will become part of your everyday workflow if you are constantly reverse engineering large databases.

Finding all tables and fields on "many" side of relations with a certain table

in Sql Server 2005, I have a master table, and several other tables which are related to
this master through several one to many relations.
How can I find all tables and fields which are in relation with the
primary key in the master table, on "many" side?
I know I can extract this by querying views from INFORMATION_SCHEMA,
but I don't know where exactly I can find this info.
Thank you
Check out:
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
I found the answer with some help on sql server groups
I use the following query, which returns me schema name, table and field name on one and many side of the relations:
SELECT
SchemaParent.name AS ParentSchemaName,
TableParent.name AS ParentTableName,
ColumnParent.name AS ParentColumnName,
SchemaChild.name AS ChildSchemaName,
TableChild.name AS ChildTableName,
ColumnChild.name AS ChildColumnName
FROM
sys.foreign_key_columns AS kc INNER JOIN
sys.objects AS TableChild ON kc.parent_object_id = TableChild.object_id INNER JOIN
sys.schemas AS SchemaChild ON TableChild.schema_id = SchemaChild.schema_id INNER JOIN
sys.objects AS TableParent ON kc.referenced_object_id = TableParent.object_id INNER JOIN
sys.schemas AS SchemaParent ON TableParent.schema_id = SchemaParent.schema_id INNER JOIN
sys.columns AS ColumnParent ON kc.referenced_object_id = ColumnParent.object_id AND kc.referenced_column_id = ColumnParent.column_id INNER JOIN
sys.columns AS ColumnChild ON kc.parent_object_id = ColumnChild.object_id AND kc.parent_column_id = ColumnChild.column_id
ORDER BY ParentTableName, ChildTableName

list of tables without indexes in sql 2008

How do I list tables without indexes in my SQL 2008 database?
Edit
I want the Schema name and the Table name.
This should cover what your looking for. i.e. tables that are heaps (no clustered index) and do not have any non-clustered indexes. It uses the new sys. table objects used in 2005/2008.
in addition, you probably want to look for tables that do have a clustered index, but have no nonclustered indexes (this is the 2nd part of the statement which I've left commented out.
SELECT
schemaname = OBJECT_SCHEMA_NAME(o.object_id)
,tablename = o.NAME
FROM sys.objects o
INNER JOIN sys.indexes i ON i.OBJECT_ID = o.OBJECT_ID
-- tables that are heaps without any nonclustered indexes
WHERE (
o.type = 'U'
AND o.OBJECT_ID NOT IN (
SELECT OBJECT_ID
FROM sys.indexes
WHERE index_id > 0
)
)
-- OR
-- table that have a clustered index without any nonclustered indexes
--(o.type='U'
-- AND o.OBJECT_ID NOT IN (
-- SELECT OBJECT_ID
-- FROM sys.indexes
-- WHERE index_id>1))
Here's an example:
select SCHEMA_NAME(schema_id), name from sys.tables
where OBJECTPROPERTY(object_id, 'IsIndexed')= 0
In addition to #Philip Fourie's suggestion you might want to think about which indexes to create.
Once you have been accessing your data, SQL Server 2008 keeps track of places where it thinks indexes will be helpful (it refers to these as "missing indexes." There are a hand full of new Dynamic Managed Views which can show these missing indexes and some info about them.
From MSSQlTips:
sys.dm_db_missing_index_details - Returns detailed information about a missing index
sys.dm_db_missing_index_group_stats - Returns summary information about missing index groups
sys.dm_db_missing_index_groups - Returns information about a specific group of missing indexes
sys.dm_db_missing_index_columns(index_handle) - Returns information about the database table columns that are missing for an index. This is a function and requires the index_handle to be passed.
select shema = s.name, table_name = o.name
from sys.objects o
join sys.schemas s on o.schema_id = s.schema_id
where type = 'U'
and not exists (select i.index_id
from sys.indexes i
where i.type <> 0 --ignore default heap index row
and o.object_id = i.object_id )
Edit:
I have updated the SQL to include the Schema name as requested. (Note I had to sys.objects instead of sysobjects to cater for schemas that were introduced in SQL 2005)
The catalog tables are documented in the SQL Server documentation, see this link.
This FAQ contains more samples and might also be useful.
Note that these are system tables and can change between SQL server versions, where possible rather use the system table-independent views called Information Schema Views.
This code gives all the details about the indexes for all the tables:
SELECT
sch.name AS [Schema],
obj.name AS TableName,
indx.name AS IndexName,
CASE
WHEN indx.type_desc = 'HEAP' THEN 'N/A'
ELSE indx.type_desc
END AS IndexType
FROM sys.objects obj
JOIN sys.indexes indx ON indx.object_id = obj.object_id
JOIN sys.schemas AS sch ON sch.schema_id = obj.schema_id
WHERE
obj.type = 'U'
ORDER BY
obj.name

Resources