Category Archives: Helpful Scripts

How Enabling Change Tracking Can Cause Backup Failure

— By Lori Brown @SQLSupahStah

Recently I had a client who contacted me because all of the sudden the backup for a couple of databases started to fail. While those backups had been running successfully in the past they suddenly started failing with this message:


With a little more digging he found more info. Here is more verbiage from the error message:


Now we have gotten somewhere. This error is commonly associated when change tracking has been enabled on databases. I found that for Microsoft Dynamics and Management Reporter application databases that change tracking must be enabled and is commonly set for about 21 tables. Sure enough, the databases that had the backup failures were Dynamics databases. Apparently the data in the sys.syscommittab system table can get corrupt in some way. The recommended fix is to disable change tracking, take a backup of the databases then enable change tracking once again. Backups should be successful after that.

Since change tracking is not just for Dynamics databases, and could potentially be found on tons of tables, I thought that I would make a bit of code that would find all the tables that have change tracking enabled and create all of the disable\enable statements necessary to fix this issue and get backups working again.

I tested this out on my local system. Here is how I did it.

Enable change tracking on my database.


Create a few tables. Some have change tracking enabled and some do not. I also set different settings for TRACK_COLUMNS_UPDATED so that I could be sure that all settings were correctly captured and the enable statements would put everything back as it had been initially found.



CREATE TABLE [dbo].[CTTest](

[Column1] [int] NOT NULL,

[Column2] [varchar](50) NULL,

[Column3] [date] NULL,



[Column1] ASC





CREATE TABLE [dbo].[CTTest2](

[Column1] [int] NOT NULL,

[Column2] [varchar](50) NULL,

[Column3] [date] NULL,



[Column1] ASC





CREATE TABLE [dbo].[CTTest3](

[Column1] [int] NOT NULL,

[Column2] [varchar](50) NULL,

[Column3] [date] NULL,



[Column1] ASC





I know….I am not very creative when making test tables. Each table must have a primary key in order to enable change tracking. After that, I went to the CTTest, CTTest3 & CTTest4 table properties and set change tracking up. I turned on TRACK_COLUMNS_UPDATED for CTTest & CTTest4 but turned it off for CTTest3.


Here is my code designed to find the tables with change tracking enabled and create the disable\enable statements needed to turn it all of then turn it all back on again. No statements are executed against the database. Print statements are used instead to create the statements you need so that you can control what happens and when. Run this while connected to the user database that is the target.

DECLARE @dbname VARCHAR(128)




DECLARE @RetPerUnitsDesc VARCHAR(60)

DECLARE @schema VARCHAR(128)

DECLARE @tblname VARCHAR(128)



DECLARE @sqlstr1 NVARCHAR(2000)

DECLARE @sqlstr2 NVARCHAR(2000)

DECLARE @sqlstr3 NVARCHAR(2000)

DECLARE @sqlstr4 NVARCHAR(2000)



SELECT AS SchemaName,

OBJECT_NAME(T.object_id) AS TableName,


FROM sys.change_tracking_tables T

INNER JOIN sys.tables TT ON TT.object_id = T.object_id

INNER JOIN sys.schemas S ON S.schema_id = TT.schema_id

ORDER BY SchemaName, TableName


SET @dbname = DB_NAME()

SELECT @AutoClnup = is_auto_cleanup_on,

@RetPer = retention_period ,

@RetPerUnitsDesc = retention_period_units_desc

FROM sys.change_tracking_databases WHERE database_id = DB_ID()


IF @AutoClnup = 1

SET @AutoClnupStr = ‘ON’


SET @AutoClnupStr = ‘OFF’


OPEN tblcur


FETCH NEXT FROM tblcur INTO @schema, @tblname, @ColUpdtFlg


— disable statements





SET @sqlstr1 = ‘ALTER TABLE [‘+@schema+‘].[‘+@tblname+‘] DISABLE CHANGE_TRACKING’


PRINT @sqlstr1


FETCH NEXT FROM tblcur INTO @schema, @tblname, @ColUpdtFlg


CLOSE tblcur


SET @sqlstr2 =


PRINT @sqlstr2



— open it again to create enable statements

OPEN tblcur


FETCH NEXT FROM tblcur INTO @schema, @tblname, @ColUpdtFlg


— enable statements


SET @sqlstr3 = ‘ALTER DATABASE [‘+@dbname+‘] SET CHANGE_TRACKING = ON (CHANGE_RETENTION = ‘+CAST(@RetPer AS VARCHAR(4))+‘ ‘+@RetPerUnitsDesc+‘, AUTO_CLEANUP = ‘+@AutoClnupStr+‘)


PRINT @sqlstr3




IF @ColUpdtFlg = 1

SET @ColUpdt = ‘ON’


SET @ColUpdt = ‘OFF’


SET @sqlstr4 = ‘ALTER TABLE [‘+@schema+‘].[‘+@tblname+‘] ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ‘+@ColUpdt+‘)’


PRINT @sqlstr4


FETCH NEXT FROM tblcur INTO @schema, @tblname, @ColUpdtFlg




CLOSE tblcur





Here is the output:


You can copy and paste the statements you need into a query window and run them to disable\enable change tracking. Or you can save the output to a file so that you have a script saved for just in case you need it.

I decided to also put a bit of data into one of the tables so that I could check to see what happens to the hidden tables when you go about disabling and enabling change tracking.

insert into CTTest values (1, ‘Value1’, ‘1-1-2018’)

insert into CTTest values (2, ‘Value2’, ‘1-2-2018’)

insert into CTTest values (3, ‘Value3’, ‘1-3-2018’)

insert into CTTest values (4, ‘Value4’, ‘1-4-2018’)

insert into CTTest values (150, ‘Value5’, ‘1-5-2018’)

update CTTest set Column2 = ‘Val1’ where Column3 = ‘1-1-2018’

delete from CTTest where Column3 = ‘1-5-2018’

Using a query from super smart Kendra Little (@Kendra_Little or ), I can see that my internal tables are being tracked.



select as CT_schema, as CT_table,

ps1.row_count as CT_rows,

ps1.reserved_page_count*8./1024. as CT_reserved_MB, as tracked_schema, as tracked_name,

ps2.row_count as tracked_rows,

ps2.reserved_page_count*8./1024. as tracked_base_table_MB,

change_tracking_min_valid_version(sot2.object_id) as min_valid_version

FROM sys.internal_tables it

JOIN sys.objects sot1 on it.object_id=sot1.object_id

JOIN sys.schemas AS sct1 on


JOIN sys.dm_db_partition_stats ps1 on

it.object_id = ps1. object_id

and ps1.index_id in (0,1)

LEFT JOIN sys.objects sot2 on it.parent_object_id=sot2.object_id

LEFT JOIN sys.schemas AS sct2 on


LEFT JOIN sys.dm_db_partition_stats ps2 on

sot2.object_id = ps2. object_id

and ps2.index_id in (0,1)

WHERE it.internal_type IN (209, 210);



When I ran the statements to disable change tracking the same query shows that no tables are being tracked:


My client reported that disabling\enabling change tracking did indeed fix his backup problem. The fun part is that while several databases had change tracking enabled, only a few had the backup issue.

Here are some of the links that I thought were interesting while researching:

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. We love to talk tech with anyone in our SQL family!

Query to Get SQL Job Info from sysprocesses

— By Lori Brown @SQLSupahStah

We recently were troubleshooting an issue where one client reported that the SQL instance was suddenly very slow. One of the things we do is check to see what queries are currently running in SQL by using the following query:

— Finds info on queries that are running NOW


SUBSTRING(text, CASE WHEN ((ExecReq.statement_start_offset/2) + 1) < 1 THEN 1 ELSE (ExecReq.statement_start_offset/2) + 1 end,

((CASE ExecReq.statement_end_offset WHEN -1 THEN DATALENGTH(text) ELSE ExecReq.statement_end_offset END

– ExecReq.statement_start_offset)/2) + 1) AS statement_text,



ExecReq.session_id AS [SPID],




ExecReq.total_elapsed_time / 1000.0 AS [total_elapsed_time (secs)],

ExecReq.wait_time / 1000.0 AS [wait_time (secs)],

ExecReq.cpu_time / 1000.0 AS [cpu_time (secs)],




ExecReq.blocking_session_id AS [BlockingSPID]

FROM sys.dm_exec_requests ExecReq

OUTER APPLY sys.dm_exec_sql_text(ExecReq.sql_handle) ExecSQLText

WHERE ExecReq.session_id > 50

AND ExecReq.session_id <> @@spid

AND ExecReq.last_wait_type <> ‘BROKER_RECEIVE_WAITFOR’

ORDER BY ExecReq.cpu_time DESC

— total_elapsed_time desc

— wait_time desc

— logical_reads desc

After running this a few times, we can see if any one query consistently shows up in the list as using lots of resources or is blocking or being blocked. We can also see if a lot of queries seem to be listed that usually are fast which might indicate that there is some kind of issue with the execution plan or statistics. Here is an example of what the output looks like:


If I was trying to further figure out what SPID 709 was doing (as in what user and what application), I might take a look at sp_who2 or sp_WhoIsActive from Adam Machanic ( and you might see something like this in the program name:

SQLAgent – TSQL JobStep (Job 0xCC1842F477AA1A4E84CD91228FFC799B : Step 1)

We know a job is running but what job is it? I know that we can get the job name from the hex but decided that instead of needing to copy the value from the program_name into a variable that I wanted to have a query to decipher any jobs that might be found on the fly. This would keep me from needing to open the job activity and try to find which job was running. If you have hundreds of jobs to look through or if more than one job is running then you might have to guess which one is the culprit that you are looking for. I also wanted to know what job step the job was on so that I could know where it was and so that I was better prepared to provide meaningful information back to my client.

SELECT p.spid, As ‘Job Name’, js.step_name as ‘Job Step’,

p.blocked, p.lastwaittype, p.dbid, p.cpu, p.physical_io, p.memusage, p.last_batch

FROM master.dbo.sysprocesses p JOIN msdb.dbo.sysjobs j

ON master.dbo.fn_varbintohexstr(convert(varbinary(16), job_id))

COLLATE Latin1_General_CI_AI = substring(replace(program_name, ‘SQLAgent – TSQL JobStep (Job ‘, ), 1, 34)

JOIN msdb.dbo.sysjobsteps js ON j.job_id = js.job_id

WHERE p.program_name like ‘SQLAgent – TSQL%’

AND js.step_id = (substring(p.program_name, (CHARINDEX(‘: Step’,p.program_name, 1)+7),

((CHARINDEX(‘)’, p.program_name, 1))-(CHARINDEX(‘: Step’, p.program_name, 1)+7))))


It is handy to have both of the above queries in one query window so that results are returned in one place making it easier to associate running queries with their job. Hope this helps someone out as much as it did me.

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. We love to talk tech with anyone in our SQL family!




Log Connections to SQL Instance

— by Ginger Keys

If you ever have a need to monitor connections to your SQL server, and any related information about the connections such as database, logins, etc., there are some DMVs that can give you tons of information. Previously you might have used the sys.sysprocesses table to derive much of this information, but this is being deprecated in the most recent versions of SQL server.

Instead, you can collect valuable information from these DMVs:




In order to capture and retain connection information for my SQL server, I will create a small database and a table to hold some basic information. Of course you can alter the script to include more, less, or different data than what I am demonstrating below, to better fit your specific information needs.

I will create a database and a table, then insert data from two of the DMVs listed above.

Step 1 – Create a table to hold login activity

— Create a database

USE master




( NAME = N’Connections’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Connections.mdf’ ,

SIZE = 1024MB , FILEGROWTH = 512MB )


( NAME = N’Connections_log’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Connections_log.ldf’ ,




— Create table to hold Login info

USE [Connections]


CREATE TABLE [dbo].[LoginActivity]


host_name [nvarchar](128) NULL,

program_name [nvarchar](128) NULL,

login_name [nvarchar](128) NOT NULL,

client_net_address [nvarchar](48) NULL,

DatabaseName [nvarchar](128) NOT NULL,

login_time [datetime] NOT NULL,

status [nvarchar](30) NOT NULL,

date_time[datetime] NOT NULL,




Step 2 – Insert Data into Table

If you need to retain or archive this connection information, you can create a database which will hold the information, or export the results to a spreadsheet or other file. Otherwise you can simply select the information from the DMV below if you only need to see current data.


USE Connections


INSERT INTO LoginActivity









— run the following select statement by itself to see connection info if you don’t want to save the output





c.client_net_address, AS DatabaseName,



GETDATE() AS date_time

FROM sys.dm_exec_sessions s

JOIN sys.dm_exec_connections c ON s.session_id = c.session_id

JOIN sys.databases d ON d.database_id = s.database_id

–where = ‘ABCompany’ –can specify databases if needed

WHERE GETDATE() >= DATEADD(hh,-10, GETDATE()) –date range can be adjusted


Step 3 – View/Save Output Results

After inserting the data into my table, I can see the current connections from the last 10 hours (as per my insert statement). On a production server, this list would be far greater.

SELECT * FROM LoginActivity


From the columns I have included in my table:

Host_name – will give you the name of the workstation connecting – shows NULL for internal sessions.

Program_name – tells you the name of the client program or application connecting.

Client_net_address – provides the host address of each client connecting

Login_name, DatabaseName, and login_time – self-explanatory.

date_time – is the current day and time the query is run

Status – gives the status of the session, which will be running, sleeping, dormant, or preconnect.

This information can also be output to a text or excel file if preferred.



Being able to see users or applications making connections to your SQL Server can be useful or necessary for many reasons. The steps outlined above provide a general guideline for deriving connection information that can be altered to fit your organizational needs.

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. Visit us at!

Get Index Column Info with Includes for One or Many Tables

— by Lori Brown @SQLSupahStah

I was recently working with one of my clients on some low hanging fruit type of query tuning. We had checked the cache for plans with missing index warnings in them and were trying to see if we could tweak and existing index or add a new index to speed things up. If you ever work with missing indexes, you surely have seen it recommend crazy things, duplicates or existing indexes or it wants something that can be added to an existing index.

The bottom line for missing index recommendations is that you should NEVER, EVER create a missing index unless you know for absolutely sure that the index does not exist and that it will really help a known performance issue. For instance, you can have missing index warnings on small tables that only return a couple of rows to the query. Those are usually not worth working on since scanning a small table can usually be done very quickly by SQL. To this day, I still find many databases that are way over indexed with indexes that were implemented simply because someone found a missing index warning and did not do their homework or they ran the dreaded Database Tuning Advisor which shoved a bunch of duplicate indexes into tables.

If you are wondering how to get a list of missing indexes, please check out Jeff Schwartz’s blog post on how to do this. ( ) This will give you a place to start. It is better if you know what query is throwing the missing index warning so it is a good idea to collect those either in a trace or extended events. Jeff builds on his first post and in his second post on the subject ( ) also goes over the fun of having multiple missing index recommendations for a single table and how to deal with them.

Here’s a handy set of links for some of Jeff’s great index tuning work that you should really check out:

One of the things that I usually need when performance tuning is to know information about the existing indexes on specific tables. I always want to know what columns are in the indexes along with the included columns so that I can compare the existing indexes to the missing recommendations. This way I can better figure out if a recommendation is something that can be added to an existing index (like an included column) or if I really need to create a brand new index if it does not exist at all.

Like most DBA’s, I keep a toolkit with all kinds of handy scripts. However, I did not have one that would give me index included columns. I also wanted the query to be able to return info from one or many tables at the same time. This would be useful when dealing with things with lots of joins. I know that there are a few bloggers who have posted something similar but I wanted to have the ability to filter on one or multiple tables. So, here is what I came up with:


Returns index columns with included columns

plus other needed index info for tables

in @tablelist variable



DECLARE @tablelist VARCHAR(1000)



SET @tablelist = ‘InvoiceLines,OrderLines,StockItemHoldings’ — comma delimited list of tables, can be one or multiples EX: ‘mytable’ or ‘mytable,nothertable,thirdtable’


— Query the tables

IF @tablelist <> OR @tablelist <> ‘?,?,?’


SET @tablelist = REPLACE(QUOTENAME(@tablelist,””), ‘,’, ”’,”’) — Add quotes so the IN clause will work


SET @sqlstr = ‘SELECT SCHEMA_NAME(o.schema_id) AS SchemaName

       , AS TableName

       , AS IndexName

       ,i.type_desc AS IndexType

       , AS ColumnName







FROM sys.indexes i

JOIN sys.index_columns ic ON i.index_id = ic.index_id

JOIN sys.columns c ON ic.column_id = c.column_id AND ic.object_id = c.object_id

JOIN sys.objects o ON o.object_id = i.object_id AND c.object_id = i.object_id

JOIN sys.types t on t.user_type_id = c.user_type_id

WHERE IN (‘+@tablelist+‘)

ORDER BY SchemaName, TableName, IndexName, index_column_id’


EXEC sp_executesql @sqlstr

–PRINT @sqlstr


All you have to provide is a comma separated list of the table(s) you are interested in for the @tablelist variable and it will do the rest. The output looks like this:


I found several bloggers who had made queries that would concatenate the columns together but truthfully I found those hard to read so I settled for a list with some extra info on the data and index types. Hope this is useful to someone out there.

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. Visit us at!


Find Tables That Have Special Features Enabled

Find out if any of tables in your database have special features enabled using the queries below.  These features need to be understood and carefully managed.

— CDC Enabled Tables

select distinct AS CDCTables

from sys.tables t

where t.is_tracked_by_cdc = 1


— File Tables — SQL 2012 +

select distinct AS FileTables

from sys.tables t

where t.is_filetable = 1


— Temporal Tables — SQL 2016 +

select distinct AS TemporalTables

from sys.tables t

where t.temporal_type > 0


— Stretch Enabled Tables — SQL 2016 +

select distinct AS StretchTables

from sys.tables t

where t.is_remote_data_archive_enabled > 0


— External Tables — SQL 2016 +

select distinct AS ExternalTables

from sys.tables t

where t.is_external > 0

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. Visit us at!

Find Out Who Changed the Database Recovery Model

— By Lori Brown @SQLSupahStah

I ran into a situation where we were working on a migration and had been directed to put all databases into FULL recovery model in anticipation of using log shipping to push databases to the new server. Once we are ready to go live on the new server the plan was to ship the last transaction logs and then restore them WITH RECOVERY in an effort to make the final cutover as quick as possible. Of course this means that we had to make sure that all databases were having regular log backups, which we did. Things were going along nicely until we started receiving log backup failure notifications.

Upon checking things, we found that one of the databases had been changed to SIMPLE recovery model. You can find this type of information in the default trace or you can simply scroll through the SQL error logs until you find the entry that you are looking for. If you have a busy instance that has a lot of entries in the error log, this can be a bit time consuming so I came up with a set of queries that will grab the error log entry and attempt to tie it to the info in the default trace so that it was easier to identify WHO was the culprit who made an unauthorized change to the database properties.


DECLARE @tracefile VARCHAR(500)



CREATE TABLE [dbo].[#SQLerrorlog](


[ProcessInfo] VARCHAR(10) NULL,





Valid parameters for sp_readerrorlog

1 – Error log: 0 = current, 1 = Archive #1, 2 = Archive #2, etc…

2 – Log file type: 1 or NULL = error log, 2 = SQL Agent log

3 – Search string 1

4 – Search string 2


Change parameters to meet your needs


— Read error log looking for the words RECOVERY

–and either FULL, SIMPLE or BULK_LOGGED indicating a change from prior state


EXEC sp_readerrorlog 0, 1, ‘RECOVERY’, ‘FULL’



EXEC sp_readerrorlog 0, 1, ‘RECOVERY’, ‘SIMPLE’



EXEC sp_readerrorlog 0, 1, ‘RECOVERY’, ‘BULK_LOGGED’


UPDATE #SQLerrorlog

SET ProcessInfo = SUBSTRING(ProcessInfo,5,20)

FROM #SQLerrorlog

WHERE ProcessInfo LIKE ‘spid%’


— Get path of default trace file

SELECT @tracefile = CAST(value AS VARCHAR(500))

FROM sys.fn_trace_getinfo(DEFAULT)

WHERE traceid = 1

AND property = 2


— Get objects altered from the default trace

SELECT IDENTITY(int, 1, 1) AS RowNumber, *

INTO #temp_trc

FROM sys.fn_trace_gettable(@tracefile, default) g — default = read all trace files

WHERE g.EventClass = 164


SELECT t.DatabaseID, t.DatabaseName, t.NTUserName, t.NTDomainName,

t.HostName, t.ApplicationName, t.LoginName, t.SPID, t.StartTime, l.Text

FROM #temp_trc t

JOIN #SQLerrorlog l ON t.SPID = l.ProcessInfo

WHERE t.StartTime > GETDATE()-1 — filter by time within the last 24 hours



DROP TABLE #temp_trc




You can find more on the following:

sp_readerrorlog is an undocumented procedure that actually uses xp_readerrorlog –

sys.fn_trace_getinfo –

sys.fn_trace_gettable –

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. We love to talk tech with anyone in our SQL family!

Use MSDB to Get Database Backup Size and Total Time For Each

— by Lori Brown

We recently started using a third party software to do our in-house SQL backups so that the backup files are stored in a redundant and safe place. To confirm that the software was indeed compressing backups as it stated it would, we wanted to see what each backup size actually was in SQL so that we could compare that to what the software was telling us.

SQL stores lots of handy backup information in msdb in the backupset and backupmediafamily tables.

Here is my query. I am only wanting the information from the last 24 hours so have filtered the start date by subtracting 1 day from today. I have also provided some commented out options in case someone needs them.

— database backup size and how long it took to do backup

SELECT bs.database_name AS DatabaseName

, CAST(bs.backup_size/1024.0/1024/1024 AS DECIMAL(10, 2)) AS BackupSizeGB

, CAST(bs.backup_size/1024.0/1024 AS DECIMAL(10, 2)) AS BackupSizeMB

–, CAST(bs.compressed_backup_size/1024.0/1024/1024 AS DECIMAL(10, 2)) AS CompressedSizeGB   

       –, CAST(bs.compressed_backup_size/1024.0/1024 AS DECIMAL(10, 2)) AS CompressedSizeMB

, bs.backup_start_date AS BackupStartDate

, bs.backup_finish_date AS BackupEndDate

, CAST(bs.backup_finish_date – bs.backup_start_date AS TIME) AS AmtTimeToBkup

, bmf.physical_device_name AS BackupDeviceName

FROM msdb.dbo.backupset bs JOIN msdb.dbo.backupmediafamily bmf

ON bs.media_set_id = bmf.media_set_id


–bs.database_name = ‘MyDatabase’ and   — uncomment to filter by database name

bs.backup_start_date > DATEADD(dd, -1, GETDATE()) and

bs.type = ‘D’ — change to L for transaction logs

ORDER BY bs.database_name, bs.backup_start_date

And, here is the output.


It turned out that the software was indeed compressing all backups so that was a good thing.

There is a lot more info that can be pulled from msdb regarding backups. Have some fun and experiment with getting information that you need from there. Here are some links to some other backup related topics that we have blogged about already.

For more information about blog posts, concepts and definitions, further explanations, or questions you may have…please contact us at We will be happy to help! Leave a comment and feel free to track back to us. Visit us at!


Get a List of Tables That Are Compressed

Find out if any of your tables are compressed in your database using the query below. Compressed tables can save space and make queries run faster.

— Compressed Tables
select distinct AS CompressedTables
from sys.partitions p
inner join sys.tables t
on p.object_id = t.object_id
where p.data_compression > 0

If you don’t have any tables compressed but think you might want to compress some, you can check your data compression savings by running the stored procedure sp_estimate_data_compression_savings for your targeted table.

USE WideWorldImporters;
EXEC sp_estimate_data_compression_savings ‘Sales’, ‘Invoices’, NULL, NULL, ‘ROW’ ;

List Partitioned Tables And Other Info About Them

— By Lori Brown @SQLSupahStah

Here is a good way to find out if any of your tables are partitioned in your database using the query below. It is important to know so that you can find out how the tables are partitioned.

— Partitioned Tables


FROM sys.partitions p

INNER JOIN sys.tables t

ON p.object_id = t.object_id

WHERE p.partition_number <> 1

If you have partitioned tables here is a good way to find out how it is partitioned up.

— Get partition info

SELECT SCHEMA_NAME(o.schema_id) + ‘.’ + OBJECT_NAME(i.object_id) AS [object]

, p.partition_number AS [p#]

, AS [filegroup]

, p.rows

, au.total_pages AS pages

, CASE boundary_value_on_right

WHEN 1 THEN ‘less than’

ELSE ‘less than or equal to’ END AS comparison

, rv.value

, CONVERT (VARCHAR(6), CONVERT (INT, SUBSTRING (au.first_page, 6, 1) +

SUBSTRING (au.first_page, 5, 1))) + ‘:’ + CONVERT (VARCHAR(20),

CONVERT (INT, SUBSTRING (au.first_page, 4, 1) +

SUBSTRING (au.first_page, 3, 1) + SUBSTRING (au.first_page, 2, 1) +

SUBSTRING (au.first_page, 1, 1))) AS first_page

FROM sys.partitions p

INNER JOIN sys.indexes i

ON p.object_id = i.object_id

AND p.index_id = i.index_id

INNER JOIN sys.objects o

ON p.object_id = o.object_id

INNER JOIN sys.system_internals_allocation_units au

ON p.partition_id = au.container_id

INNER JOIN sys.partition_schemes ps

ON ps.data_space_id = i.data_space_id

INNER JOIN sys.partition_functions f

ON f.function_id = ps.function_id

INNER JOIN sys.destination_data_spaces dds

ON dds.partition_scheme_id = ps.data_space_id

AND dds.destination_id = p.partition_number

INNER JOIN sys.filegroups fg

ON dds.data_space_id = fg.data_space_id

LEFT OUTER JOIN sys.partition_range_values rv

ON f.function_id = rv.function_id

AND p.partition_number = rv.boundary_id

WHERE i.index_id < 2

AND o.object_id = OBJECT_ID(‘dbo.SomeTableName’);

List Tables That May Be Over Indexed

— By Lori Brown

While not having enough indexes can be bad for query performance, having too many indexes can also be just as bad. Use the query below to get a list of tables in your database that has more than 10 indexes.

— Tables with large number of indexes

select as TablesWithLargeNumInx, count( as CountIndexes

from sys.indexes i

inner join sys.tables t

on i.object_id = t.object_id

where i.index_id > 0

group by

having count( > 10

If you suspect that you have too many indexes on your tables, you can also check the sys.dm_db_index_usage_stats dynamic management view to know if indexes on your heavily indexed tables are being used well. (Hint: seeks are good and scans are not so much)

select u.user_seeks, u.user_lookups, u.user_scans

from sys.dm_db_index_usage_stats u

inner join sys.indexes i

on u.object_id = i.object_id and u.index_id = i.index_id

WHERE u.object_id=object_id(‘dbo.SomeTableName’)