.Net A Host A Record Active Directory AD Application Layer Archives Backup Exec Backup Media Backup Tape Backups Bandwidth Blade Broadband Router Broadcast domain c++ CCENT CCNA Cisco Citrix VDI Citrix VPN Citrix Xenapp Cloud Computing Collision domain Corrupt Profile DAE Data Backup Data Imaging Data Layer DATA Recovery Default Gateway Dell Server Developer Disk Array DNS DNS Lookup DNS Query DNS Stuff Part 1 DOS Attack Dual Power Supply ESXi Ethernet Hub Ethernet Switch Exchange Mail Fibre Optic File Restore Firewall Forward lookup Ghost Gigabit port Harddrive Hop How to Migrate a Mailbox from Exchange 2003 to 2007 HP ICND1 Imaging a Machine Internet Bandwidth Ipconfig LAN LAN Speed Layer 2 Switch Layer 2 vs Layer 3 Switches Layer Switch Linksys router Mail store Mailbox Mandatory Profile Maximum-transmission Unit Mini ITX Blade Server MX Record NAS Nbtstat NEC Blade Server Network Area Storage Network Bandwidth Network Cards Network Monitor Network Profiles Network Security Network Traffic Tool NSLOOKUP Online Backup Optimization OSI Model OST Outlook Ping POP Port 25 Power Supply Poweredge Dell M710 Blade server specifications Procurve Swiitch PST PTR Record Recursion Reverse Lookup Roaming Profile Routers SAN Server Server Detection Shadow Protect Shadowing SMTP Some Useful SQL Stuff SQL Server Indexes Storage Area Network STP Symantec sysprep system backup system Monitoring System Recovery TCP/IP TCPIP Throughput Tracert Troubleshoot network Connectivity UDP VCP Virtual LAN Virtual Machines Virtualization Vista Temporary Profile Issue VLAN VLANS VMware VMware ESX VMWARE VDI Vmware workstation Vsphere Client VTSP WAN Acceleration Windows Backup Wireless Router Xen App Xen desktop

SQL Server Indexes

| Posted in | Posted on

A database index is similar to an index in a book – it is comprised of a lookup value, and a number identifier that corresponds to the row number in a table. In SQL Server, there are two kinds of indexes – clustered and non-clustered. Clustered Indexes require that the data in the table is physically sorted in the order of the index. Because the data in a table can be physically sorted only one way, there can be at most only one clustered index per table. Non clustered index do not require that data be physically sorted, so there can be more that one non-clustered index per table. In fact SQL Server allows up to 249 non-clustered indexes per table. Because data is not physically sorted, range searches using a non clustered index are not very efficient.

The command for creating an index in T-SQL is

CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) [ WITH <> [ ,...n] ] [ ON filegroup ] <> :: = { PAD_INDEX | FILLFACTOR = fillfactor | IGNORE_DUP_KEY | DROP_EXISTING | STATISTICS_NORECOMPUTE | SORT_IN_TEMPDB }

PAD_INDEX specifies the percentage of space left free on the non-leaf levels of the index. FILLFACTOR specifies the percentage to fill the leaf pages. SORT_IN_TEMPDB specifies that intermediate results of the sort will be stored in tempdb. This increases disk space requirement but affects speed index creation. STATISTICS_NO_RECOMPUTE tells the system not to automatically update index statistics.

Of course, indexes can also be created and managed using the Enterprise Manager. They can be created using the Create Index Wizard, from the Database Diagram, or by modifying fields in the Table Designer.

There is a trade off with indexes. While they speed up execution of queries immensely, there is overhead associated with them. They consume additional disk space, and require additional time to update themselves whenever data is updated or appended to a table. When loading large amounts of data it may pay to drop the index prior to the loading, then recreate the index after the new records have been appended to the table. Indexes can be dropped using the Table Designed, or by using the DROP INDEX command.

Indexes can also become fragmented. To defrag an index, either drop and recreate the index, or issue the command dbcc indexdefrag.