628 Chapter 14 • Implementing ObjectsExample 14.9 creates a clustered index by star name and a nonclustered index by star type on the Stars table we created in the previous example.. Ind
Trang 1Option Explanation
PAD_INDEX = ON | OFF When this option is ON, free space is
allocated in each page of the index Allows for new values to be inserted without rear-ranging a large amount of data The amount
of free space allocated is specified by the FILLFACTOR parameter.
When this option is OFF, enough free space for one row is reserved in every page during index creation.
FILLFACTOR = fill factor
percentage Specifies the percentage of each page that should be filled up with data For example,
a fill factor of 80 means 20% of each page will be empty and available for new data
The fill factor is used only when you create or rebuild an index Fill factor and index padding are discussed in detail in Chapter 7.…
SORT_IN_TEMPDB = ON | OFF Specifies whether the data should be sorted
in the tempdb database instead of the current database This may give performance advantages if the tempdb database is stored
on a different disk to the current database.
IGNORE_DUP_KEY = ON | OFF Specifies that duplication errors should be
ignored when creating unique indexes.
STATISTICS_NORECOMPUTE =
ON | OFF Specifies that optimization statistics should not be updated at this time.
DROP_EXISTING = ON | OFF Specifies that the existing index with the
same name should be dropped and then be re-created This equates to an index rebuild.
ONLINE = ON | OFF Specifies that the underlying table should
remain online and accessible by users while the index is being built This option is only available in SQL Server 2008 Enterprise or Developer edition.
ALLOW_ROW_LOCKS =
ON | OFF Specifies whether locks should be held on each row, as necessary.
ALLOW_PAGE_LOCKS =
ON | OFF Specifies whether locks should be held on each page, as necessary.
Table 14.2 Index Options
Continued
Trang 2628 Chapter 14 • Implementing Objects
Example 14.9 creates a clustered index (by star name) and a nonclustered index (by star type) on the Stars table we created in the previous example Figure 14.3
shows how the IX_Star_Name can be created using the interface of SQL Server
Management Studio
MAXDOP = max_degree_
of_parallelism Specifies the maximum number of processors that are to be used during the rebuild
operation.
DATA_COMPRESSION =
NONE | ROW | PAGE Use data compression at row or page level of the index Data compression is discussed
in detail in Chapter 7.
Table 14.2 Continued Index Options
Example 14.9 Working with Indexes
Create the table specifying that the primary key index is to be
created as nonclustered
CREATE TABLE Stars
(StarID int PRIMARY KEY NONCLUSTERED,
StarName varchar(50) Unique,
SolarMass decimal(10,2) CHECK(SolarMass > 0),
StarType varchar(50) DEFAULT 'Orange Giant');
GO
CREATE CLUSTERED INDEX Ix_Star_Name
ON Stars(StarName)
WITH (PAD_INDEX = ON,
FILLFACTOR = 70,
ONLINE = ON);
GO
CREATE NONCLUSTERED INDEX Ix_Star_Type
ON Stars(StarType)
WITH (PAD_INDEX = ON,
FILLFACTOR = 90);
GO
Trang 3When you are creating a PRIMARY KEY constraint, an index on the column(s) designated as PRIMARY KEY will be created automatically This index will be
clus-tered by default, but this can be overridden when creating the index by specifying
PRIMARY KEY NONCLUSTERED option As a best practice, it is
recom-mended that you accept the default of the clustered primary key column, unless
you have a specific reason to designate another column as the clustered index key
Usually, the automatically created index is named PK_TableName_<Unique
Number>, but this can be changed at any time by renaming the index For example,
a newly created Stars table with a PRIMARY KEY of StarID automatically has an
index named UQ Stars A4B8A52A5CC1BC92.
Figure 14.3 Creating an Index Using SQL Server Management Studio
Trang 4630 Chapter 14 • Implementing Objects
Working with Full-Text Indexes
Standard indexes are great when used with the simple WHERE clause of the SELECT statement An index will greatly reduce the time it will take you to locate
rows where the indexed column is equal to a certain value, or when this column starts with a certain value However, standard indexes are inadequate for fulfilling more complex text-based queries For example, creating an index on StarType will not help you find all rows where the StarType column contains the words “giant,” but not the words “supermassive”
To fulfill these types of queries, you must use full-text indexes Full-text indexes are complex structures that consolidate the words used in a column and their relative weight and position, and links these words with the database page contain-ing the actual data Full-text indexes are built uscontain-ing a dedicated component of SQL
Server 2008—the Full-Text Engine In SQL Server 2005 and earlier, the Full-Text
Engine was its own service, known as full-text search In SQL Server 2008, the Full-Text Engine is part of the database engine (running as the SQL Server Service) Full-text indexes can be stored on a separate filegroup This can deliver perfor-mance improvements, if this filegroup is hosted on a separate disk from the rest of the database Only one full-text index can be created on a table, and it can only be created on a single, unique column that does not allow null values Full-text indexes must be based on columns of type char, varchar, nchar, nvarchar, text, ntext, image, xml, varbinary, and varbinary(max) You must specify a type column, when creating
a full-text index on a image, varbinary, or varbinary(max) columns The type
column stores the file extension (.docx, pdf, xlsx) of the document stored in the indexed column
Example 14.10 amends the Stars table to include a Description column and creates a full-text index on this column The FREETEXT function allows us to
search on any of the words specified using the full-text index This yields a similar user experience as using an Internet search engine
Exam Warning
Remember that when creating a table, a unique index will be
automati-cally created on the columns designated as the PRIMARY KEY If you
wish to avoid the long rebuild time associated with building a clustered index, or if you wish to create the clustered index on a column different
from the PRIMARY KEY, you must explicitly specify the PRIMARY KEY
NONCLUSTERED option The PRIMARY KEY will always be unique.
Trang 5Example 14.10 Creating and Using a Full-Text Index
ALTER TABLE Stars
ADD Description ntext DEFAULT 'No description specified' NOT NULL ;
GO
CREATE FULLTEXT CATALOG FullTextCatalog AS DEFAULT;
CREATE FULLTEXT INDEX ON Stars(Description)
KEY INDEX PK Stars 06ABC6465F9E293D;
GO
UPDATE Stars SET Description = 'Deneb is the brightest star in the
constellation Cygnus and one of the vertices of the Summer Triangle
It is the 19th brightest star in the night sky, with an apparent
magnitude of 1.25 A white supergiant, Deneb is also one of the most
luminous stars known It is, or has been, known by a number of other
traditional names, including Arided and Aridif, but today these are
almost entirely forgotten Courtesy Wikipedia.'
WHERE StarName = 'Deneb';
UPDATE Stars SET Description = 'Pollux, also cataloged as Beta
Geminorum, is an orange giant star approximately 34 light-years away
in the constellation of Gemini (the Twins) Pollux is the brightest
star in the constellation (brighter than Castor (Alpha Geminorum)
As of 2006, Pollux was confirmed to have an extrasolar planet orbiting
it Courtesy Wikipedia.'
WHERE StarName = 'Pollux';
GO
SELECT StarName
FROM Stars
WHERE FREETEXT (Description, 'planet orbit, giant');
GO
Results:
StarName
- Pollux