How to delete partitions? - sql-server

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?

Related

Tsql a way to find empty tables without using sys. Partitions

Is there a way I can find all empty tables in a database without using sys. Partition rows?
You can use this query:
SELECT i.rowcnt, o.NAME
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
AND i.rowcnt = 0
ORDER BY i.rowcnt desc

Does a SELECT COUNT(*) query have to do a full table scan?

Does a query that gets the count of all rows in a table have to do a full table scan or does SQL Server maintain a count of rows somewhere?
SELECT COUNT(*) FROM TABLE_NAME;
The table TABLE_NAME has a primary key, and therefore a clustered index, and looks like so:
CREATE TABLE TABLE_NAME
(
Id int PRIMARY KEY IDENTITY(1, 1),
Name nvarchar(50) NOT NULL
);
I am using Microsoft SQL Server 2014.
The server will always read all records (if there's an index then it will scan the entire index) to count the rows. You can't escape this as long as you are doing SELECT COUNT(*) FROM Table.
If your table has a clustered index, you can change your query to an "under the hood" query to retrieve the count without actually fetching the records with:
SELECT OBJECT_NAME(i.id) [Table_Name], i.rowcnt [Row_Count]
FROM sys.sysindexes i WITH (NOLOCK)
WHERE i.indid in (0,1)
ORDER BY i.rowcnt desc
if you are looking for an approximate count of the records, you can also use the following query:
SELECT
TableName = t.NAME,
SchemaName = s.Name,
[RowCount] = p.rows,
TotalSpaceMB = CONVERT(DECIMAL(18,2), SUM(a.total_pages) * 8 / 1024.0),
UsedSpaceMB = CONVERT(DECIMAL(18,2), SUM(a.used_pages) * 8 / 1024.0),
UnusedSpaceMB = CONVERT(DECIMAL(18,2), (SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024.0)
FROM
sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name,
s.Name,
p.Rows
ORDER BY
TotalSpaceMB DESC
This will show non-system tables with their calculated (not exact) row count and the sum of the sizes of their data (with any index they might have), relatively fast without retrieving the records.
When SQL Server performs a query like SELECT COUNT(*), SQL Server will use the narrowest non-clustered index to count the rows. If the table does not have any non-clustered index, it will have to scan the table.
If your table has a clustered index you can get your count even faster.
SELECT COUNT(*) FROM TABLE_NAME;
Does a full table scan.
For optimizations you can refer to this.
you can following way. it is better in performance I guess.
SELECT COUNT(1) FROM TABLE_NAME

How can we check whether data exists or not in a table thru sys. tables/functions directly

Is there any way to check whether data exists in a table thru sys. tables or functions directly without querying the table.
Any such sys. available?
** Not querying the dynamic sql..:)
Read this article:
Find Row Count in Table – Find Largest Table in Database
Here is a query to find a ROWCOUNT of a table:
SELECT SUM(pa.rows) RowCnt
FROM sys.tables ta
INNER JOIN sys.partitions pa
ON pa.OBJECT_ID = ta.OBJECT_ID
WHERE ta.is_ms_shipped = 0
AND pa.index_id IN (1,0)
AND ta.name='table1'
SQLFiddle demo
Or if you need only information about empty table or not then something like this:
SELECT
ISNULL(
(SELECT TOP 1 1 from sys.partitions pa
where pa.OBJECT_ID = ta.OBJECT_ID
AND
pa.rows>0
AND
pa.index_id IN (1,0)
)
,0) as TableIsNotEmpty
FROM sys.tables ta
WHERE ta.is_ms_shipped = 0
AND ta.name='table1'
-- This is how I got the result...
SELECT Distinct tbl.name, C.name , X.rowcnt
FROM Sys.Columns as c
INNER JOIN Sys.Tables as tbl
ON tbl.object_id = c.object_id
INNER JOIN Sys.Types as t
ON c.system_type_id = t.system_type_id
INNER JOIN Sys.Indexes I
ON I.object_id = Tbl.object_id
Inner Join Sys.sysindexes X
On I.index_id = X.indid
And I.object_id = X.id
WHERE X.rowcnt > 0
ORDER BY tbl.name

Finding blocking/locking queries in MS SQL (mssql)

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.

when were index statistics last updated?

Is there a quick and easy way to list when every index in the database last had their statistics updated? The preferred answer would be a query. Also, is it possible to determine the "quality" of the statistics: FULLSCAN, SAMPLE n, etc.
EDIT
This worked for what I needed, a slight mod to #OrbMan great answer...
SELECT
STATS_DATE(i.object_id, i.index_id) AS LastStatisticsDate
,o.Name AS TableName
,i.name AS IndexName
FROM sys.objects o
INNER JOIN sys.indexes i ON o.object_id = i.object_id
WHERE o.is_ms_shipped=0
ORDER BY 1 DESC
You can do: STATS_DATE ( table_id , index_id )
So:
USE AdventureWorks;
GO
SELECT 'Index Name' = i.name, 'Statistics Date' = STATS_DATE(i.object_id, i.index_id)
FROM sys.objects o
JOIN sys.indexes i ON o.name = 'Address' AND o.object_id = i.object_id;
GO
where Address is the name of the table whose indexes you would like to examine.

Resources