2009-03-23

Kullanıcı Tablolarında Bulunan İndexlerin Filegrouplarını Bulma

Primary filegroup üzerinde bulunan indexleri aşağıdaki kod ile bulabiliriz:

SELECT o.[name], o.[type], i.[name], i.[index_id], f.[name]
FROM sys.indexes i (nolock) INNER JOIN sys.filegroups f (nolock) ON i.data_space_id = f.data_space_id
INNER JOIN sys.all_objects o (nolock) ON i.[object_id] = o.[object_id]
WHEREo.type<>'s' and o.type='U' and f.name='PRIMARY'
ORDER BY o.name, i.name


Bu kod özellikle nesnelerinizi filegroup'lar arasında taşırken; özellikle "Hangi indexleri taşımadım?" sorusuna cevap niteliğinde.

2009-03-22

Bir Tabloya Ait İndekslerin İstatistiklerini Bulma

SELECT
object_name(sys.indexes.Object_id) AS Table_Name,
ISNULL(sys.indexes.name, '---') AS index_name,
partitions.Rows,
partitions.SizeMB,
sys.indexes.type_desc,
sys.indexes.is_unique,
sys.indexes.is_primary_key,
sys.indexes.is_unique_constraint,
sys.indexes.is_unique,
ISNULL(Index_Columns.index_columns_key, '---') AS index_columns_key,
ISNULL(Index_Columns.index_columns_include, '---') AS index_columns_include,
ISNULL(sys.dm_db_index_usage_stats.user_seeks,0) AS user_seeks,
ISNULL(sys.dm_db_index_usage_stats.system_seeks,0) AS system_seeks,
ISNULL(sys.dm_db_index_usage_stats.user_scans,0) AS user_scans,
ISNULL(sys.dm_db_index_usage_stats.system_scans,0) AS system_scans,
ISNULL(sys.dm_db_index_usage_stats.user_lookups,0) AS user_lookups,
ISNULL(sys.dm_db_index_usage_stats.system_lookups,0) AS system_lookups,
ISNULL(sys.dm_db_index_usage_stats.user_updates,0) AS user_updates,
ISNULL(sys.dm_db_index_usage_stats.system_updates,0) AS system_updates,
(
(
(CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.user_seeks,0))+CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.system_seeks,0)))*10
+ CASE WHEN sys.indexes.type=2 THEN (CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.user_scans,0))+CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.system_scans,0)))*1 ELSE 0 END
+ 1
)
/CASE WHEN sys.indexes.type=2 THEN (CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.user_updates,0))+CONVERT(Numeric(19,6), ISNULL(sys.dm_db_index_usage_stats.system_updates,0))+1) ELSE 1 END
) AS Score
FROM
sys.indexes
JOIN (
SELECT
object_id, index_id, SUM(row_count) AS Rows,
CONVERT(numeric(19,3), CONVERT(numeric(19,3), SUM(in_row_reserved_page_count+lob_reserved_page_count+row_overflow_reserved_page_count))/CONVERT(numeric(19,3), 128)) AS SizeMB
FROM sys.dm_db_partition_stats
GROUP BY object_id, index_id
) AS partitions ON sys.indexes.object_id=partitions.object_id AND sys.indexes.index_id=partitions.index_id
CROSS APPLY (
SELECT
LEFT(index_columns_key, LEN(index_columns_key)-1) AS index_columns_key,
LEFT(index_columns_include, LEN(index_columns_include)-1) AS index_columns_include
FROM
(
SELECT
(
SELECT sys.columns.name + ', '
FROM
sys.index_columns
JOIN sys.columns ON
sys.index_columns.column_id=sys.columns.column_id
AND sys.index_columns.object_id=sys.columns.object_id
WHERE
sys.index_columns.is_included_column=0
AND sys.indexes.object_id=sys.index_columns.object_id AND sys.indexes.index_id=sys.index_columns.index_id
ORDER BY key_ordinal
FOR XML PATH('')
) AS index_columns_key,
(
SELECT sys.columns.name + ', '
FROM
sys.index_columns
JOIN sys.columns ON
sys.index_columns.column_id=sys.columns.column_id
AND sys.index_columns.object_id=sys.columns.object_id
WHERE
sys.index_columns.is_included_column=1
AND sys.indexes.object_id=sys.index_columns.object_id AND sys.indexes.index_id=sys.index_columns.index_id
ORDER BY index_column_id
FOR XML PATH('')
) AS index_columns_include
) AS Index_Columns
) AS Index_Columns
LEFT OUTER JOIN sys.dm_db_index_usage_stats ON
sys.indexes.index_id=sys.dm_db_index_usage_stats.index_id AND sys.indexes.object_id=sys.dm_db_index_usage_stats.object_id
AND sys.dm_db_index_usage_stats.database_id=DB_ID()
WHERE
sys.indexes.object_id=object_id('TableName')

2009-03-18

Bir Veritabanının Büyümesini İzleme

Bir veritabanının büyümesini hızlı olarak aşağıdaki kod ile alabilirsiniz:

select
backup_start_date BackupDate
, floor(backup_size/1024000000) SizeGB
from msdb..backupset
where database_name = db_name()
and type = 'd'
order by backup_start_date desc

2009-03-17

Askerlikten Damlalar

Siyaha boyanmış bir pencereden
Seyretmek zor dünyayı...
Hayatın henüz baharında
Solmaya başlamış gülü yeşertmek zor.

Ekip ruhundan yoksun insanlardan uzak dur!
Vakit doldu, yeni bir gün bitti; son budur...

Akdağ/Karaburun, İzmir
17.06.2002 16:53 (Mesai çıkışı)

2009-03-11

Extended Property Nasıl Eklenir?

Bir tablo yada tablonun bir field'ına extended property nasıl eklenir? sorusuyla sık sık karşılaştığım için ekleme ihtiyacı hissettim.

--Tablonun extended property'sini ekleme
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Tablo Aciklamasi’, @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TabloAdi'
GO

--Field extended property'i ekleme
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'İlgili fieldin Aciklamasi’, @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'TabloAdi', @level2type=N'COLUMN',@level2name=N'FieldAdi'
GO

.::YASAL UYARI::.

©2004-2024 Mehmet GÜZEL, www.mehmetguzel.net

Site içeriği kaynak gösterilmek koşuluyla yayınlanabilir. Yazılan yazı ve yorumlar sadece yazı ve yorum sahiplerini bağlar.