* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download 152194Ch09
Survey
Document related concepts
Transcript
SQL Server 2000 Notes Chapter 9 Page 1 of 5 Chapter 9: SQL Server Storage and Indexes SQL Server storage is arranged in a four level hierarchy. A Database object contains one or more extents. An extent object contains eight pages. A page object takes up 8 kB of disk space and consists of a page header, rows of data, and row offsets. Database Properties Made up of one or more extents. Highest level at which a lock can be placed. Can be made up of multiple files (but a file cannot hold multiple databases) Default files of every database: o Primary physical database file - *.mdf o Log file - *.ldf; contains a serial record of what happened to the database since the last time data was “committed” (stored in the *.mdf file) Extent Properties Made up of eight pages When the first table in a database is created, only one extent with two pages is allocated after the first row insertion; as data is added, new pages are allocated until the extent is full; space is allocated one full extent at a time thereafter. Implications: Once an extent is full, the next record takes up not its size, but the size of the whole next extent. SQL Server 2000 Notes Chapter 9 Page 2 of 5 Pre-allocation saves SQL Server time. Extents are lockable resources. Two kinds of extents: o Shared extents, in which each page can contain a different object o Uniform extents, in which each page is from the same object. New tables and indexes start off with shared extents, then are moved to uniform extents once they reach eight or more pages. Page Properties A page consists of 8 kB. A given row may not span pages. A page can be locked. Pages have three components: o Page header, with info about the page o Data rows, which store the actual records from a table o Row offsets, telling where each new data row begins in the page Page Types: o Data Page: holds the data from your tables, except for BLOB data (image, text, ntext), unless the BLOB is defined with the text in row option. BLOB data is usually stored in its own page; a BLOB column in your data row stores a 16 byte pointer to where the actual BLOB data is stored. o Index Page: holds the non-leaf and leaf level pages of a non-clustered index, and the non-leaf level pages of a clustered index. o BLOB Page: stores Binary Large Objects (any data stored in text, ntext, and image fields). A BLOB can be 2GB, so many BLOB pages may be needed to store a single BLOB; the pages may not be contiguous. A BLOB page can store from more than one row. o Global Allocation Map (GAM), Shared Global Allocation Map (SGAM), and Page Free Spaces (PFS) Page: store information about which extents are in use, and which are not. o Index Allocation Map (IAM) Page: maps the extents in a database file used by a heap or index. Page Splits: when a page gets about half full, it splits, and about half the data from the existing page is moved to the new page. The exception is inserting a row to the last record in a table with a clustered index. Data Row Properties A row can be up to 8060 kB. An individual row can be locked. An index is an ordered list of where to find data… B-Trees: a balanced way to store information. The root node tells what table to look in to find either the data desired or an intermediate look-up table (called a non-leaf node) that tells where to find the data. SQL Server uses B-trees to retrieve all indexed data! SQL Server 2000 Notes Chapter 9 Page 3 of 5 Notice the implications of the B-tree structure on Page splits: when you split a page, a new page must be created, data must be migrated to the new page, your data must be added to the page, and another entry must be added to a parent node. And, since root and non-leaf pages are stored on pages, those may split as well… How SQL Server Acceses Data Table scan: start at the physical beginning of a table and scan through every row sequentially. Using Indexes: find the start of a range of data in the index and read until the end of the data is reached. Table types Clustered tables: a table that is physically stored in order on some field, called the clustered index Heap tables: a table without a cluster index that is essentially stored in the random order that data is inserted into it. SQL Server 2000 Notes Chapter 9 Page 4 of 5 Types of indexes: Clustered index (on a clustered table): an index on a clustered table Can only have one per table SQL server makes a B-tree to look up the information in the table; the leaf level data is the actual table data Records inserted into a clustered index can result in a lot of data shifting. PLACING A PRIMARY KEY OR UNIQUE CONSTRAINT ON A TABLE AUTOMATICALLY CREATES AN INDEX ON THAT TABLE! Non-clustered index on a heap table: an placed index on a heap table Can have several per table Leaf-level data contains RIDs (Row Identifiers), which have the extent, page, and row of the data corresponding to the record desired. System has to retrieve information from pages stored all over the database a lot of I/O overhead Non-clustered index on a clustered table: an additional index placed on an already clustered table Can have several per table Leaf-level data contains an index number that is then used by the clustered index to find the data in the table. The CREATE INDEX Statement CREATE [UNIQUE] [CLUSTERED\NONCLUSTERED] INDEX <index_name> ON <table_or_view_name> (<column_name> [ASC|DESC] [,…n]) [WITH [PAD_INDEX] [[,] FILLFACTOR=<fillfactor>] [[,] IGNORE_DUP_KEY] [[,] DROP_EXISTING] [[,] STATISTICS_NORECOMPUTE] [[,] SORT_IN_TEMPDB] ] [ON <filegroup>] Stuff in [] is optional… ASC|DESC – ascending or descending sort for the index…default is ASC WITH – tells SQL Server you’ll be using an option PAD_INDEX – tells SQL Server how full non-leaf pages should be when the index is first created; used with FILLFACTOR… FILLFACTOR – a value between 1 and 100 (percent) of how full non-leaf pages should be when an index is created or re-built (set it low for tables with high insert rates, high for tables with stable data) IGNORE_DUP_KEY – tells SQL Server to not roll back transactions that try to insert a duplicate row that has a UNIQUE constraint on one of its columns; SQL Server will only give an error message and not insert the row. DROP_EXISTING – tells SQL Server to drop an existing index of the same name as the index being created SQL Server 2000 Notes Chapter 9 Page 5 of 5 STATISTICS _NORECOMPUTE – tells SQL Server you will take responsibility for updating statistics on the indexes SORT_IN _TEMPDB – tells SQL Server to write intermediate pages created during the creation of an index to tempdb, which can save time if tempdb is on a different drive from the working database because reads and writes are not competing on the same drive ON <filegroup> - tells SQL Server where to store indexes separately from the data Dropping an index: DROP INDEX <table_name>.<index_name>