ايندکس ها مباحث اين جلسه b-tree ها ، page ها و extent ها مفهوم page...
TRANSCRIPT
ايندکس ها
مباحث اين جلسه•B-Tree ، ها Page ها و Extentها و پيشگيري از آنPage Splitمفهوم •ايجاد ، استفاده و تغيير انواع ايندکس ها•XMLايندکس هاي • هاIndexنگهداري و به روز رساني •
NON-CLUSTERED INDEX On a Heap
NON-CLUSTERED INDEX On a CLUSTERED INDEX
انواع سيستم هاي ديتابيسOLTP (Online Transaction Processing)OLAP (Online Analytical Processing)
CREATE [UNIQUE] [CLUSTERED|NONCLUSTERED]INDEX <index name> ON <table or view
name>(<column name> [ASC|DESC] [,...n])INCLUDE (<column name> [, ...n]) [WITH[PAD_INDEX = { ON | OFF }][[,] FILLFACTOR = <fillfactor>][[,] IGNORE_DUP_KEY = { ON | OFF }][[,] DROP_EXISTING = { ON | OFF }][[,] STATISTICS_NORECOMPUTE = { ON | OFF }][[,] SORT_IN_TEMPDB = { ON | OFF }][[,] ONLINE = { ON | OFF }[[,] ALLOW_ROW_LOCKS = { ON | OFF }[[,] ALLOW_PAGE_LOCKS = { ON | OFF }[[,] MAXDOP = <maximum degree of parallelism>][ON {<filegroup> | <partition scheme name> |
DEFAULT }]
NONCLUSTERED INDEX مثال از يکCREATE TABLE MyTableKeyExample(Column1 int IDENTITYPRIMARY KEY NONCLUSTERED,Column2 int)
XML IndexesThe table containing the XML you want to
index must have a clustered index on it.A “primary” XML index must exist on the XML
data column before you can create “secondary” indexes
XML indexes can be created only on columns of XML type (and an XML index is the only kind of index you can create on columns of that type).
The XML column must be part of a base table — you cannot create the index on a view.
XML INDEXنحوه ايجاد يک USE [databaseName] GOSET ARITHABORT ON GOSET CONCAT_NULL_YIELDS_NULL ON GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOSET ANSI_PADDING ON GOSET NUMERIC_ROUNDABORT OFF GO
XML INDEXادامه ايجاد CREATE PRIMARY XML INDEX [test] ON
[dbo].[test] ( [xmldata] )WITH (PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100)GO
ساخت مجدد ايندکسALTER INDEX
PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID
ON Sales.SalesOrderDetailREBUILD WITH (FILLFACTOR = 100)
ALTER INDEX { <name of index> | ALL }ON <table or view name>{ REBUILD[ [ WITH ([ PAD_INDEX = { ON | OFF } ]| [[,] FILLFACTOR = <fillfactor>| [[,] SORT_IN_TEMPDB = { ON | OFF } ]| [[,] IGNORE_DUP_KEY = { ON | OFF } ]| [[,] STATISTICS_NORECOMPUTE = { ON | OFF
} ]| [[,] ONLINE = { ON | OFF } ]| [[,] ALLOW_ROW_LOCKS = { ON | OFF } ]| [[,] ALLOW_PAGE_LOCKS = { ON | OFF } ]| [[,] MAXDOP = <max degree of parallelism>) ]
| [ PARTITION = <partition number>[ WITH ( <partition rebuild index option>[ ,...n ] ) ] ] ]| DISABLE| REORGANIZE[ PARTITION = <partition number> ][ WITH ( LOB_COMPACTION = { ON | OFF } ) ]| SET ([ ALLOW_ROW_LOCKS= { ON | OFF } ]| [[,] ALLOW_PAGE_LOCKS = { ON | OFF } ]| [[,] IGNORE_DUP_KEY = { ON | OFF } ]| [[,] STATISTICS_NORECOMPUTE = { ON | OFF } ])} [ ; ]
DBCC SHOWCONTIG ([table name], [index name])Pages ScannedExtents ScannedExtent SwitchesAvg. Pages per ExtentScan Density [Best Count: Actual Count]Logical Scan FragmentationExtent Scan FragmentationAvg. Bytes Free per PageAvg. Page Density
DBCC DBREINDEX(db.owner.table[,index name[,fill factor]]))DBCC
DBREINDEX(OrderDetails,indexName,65]]))
انتخاب ايندکس مناسبUse Indexes (either CI or NCI) in Foreign KeysRange Selects (BETWEEN, <,>,Aggregate,
Sortes) Clustered IndexesHigh Selectivity NON-CLUSTERED IndexesIncrease Index Count Decrease Data Altering
InstructionsClustered Indexes decrease Alter Instructions
SpeedSo Always Keep Balance in Using Indexes
INDEX بدست آوردن اطالعات مربوط به يکDECLARE @db_id SMALLINT;DECLARE @object_id INT;SET @db_id = DB_ID(N'AdventureWorks2008');SET @object_id = OBJECT_ID
( N'AdventureWorks2008.Sales.SalesOrderDetail');SELECT database_id, object_id, index_id,
index_depth, avg_fragmentation_in_percent, page_count
FROM sys.dm_db_index_physical_stats(@db_id,@object_id,NULL,NULL,NULL);
نتيجه اجراي اسکريپت قبل