数据库的问题!Sql server
索引?怎么创建索引?索引到底有什么好处?只是为了查询搜索么?有没有个例题...源码,介绍一下,要详细...
索引?
怎么创建索引?
索引到底有什么好处?只是为了查询搜索么?
有没有个例题... 源码 , 介绍一下,要详细 展开
怎么创建索引?
索引到底有什么好处?只是为了查询搜索么?
有没有个例题... 源码 , 介绍一下,要详细 展开
今至电子科技有限公司
2024-08-23 广告
2024-08-23 广告
数据库备份是确保数据安全与业务连续性的关键环节。我们上海今至电子科技有限公司高度重视数据保护,定期执行全面的数据库备份策略。这包括使用先进工具和技术,对关键业务数据进行自动化备份,并存储在安全可靠的外部存储介质或云端。通过定期验证备份的完整...
点击进入详情页
本回答由今至电子科技有限公司提供
展开全部
在联机帮助上输入create index--查看
范例
A. 建立简单的非丛集索引
下列范例会在 Purchasing.ProductVendor 资料表的 VendorID 资料行上建立非丛集索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_ProductVendor_VendorID')
DROP INDEX IX_ProductVendor_VendorID ON Purchasing.ProductVendor;
GO
CREATE INDEX IX_ProductVendor_VendorID
ON Purchasing.ProductVendor (VendorID);
GO
B. 建立简单的非丛集复合索引
下列范例会在 Sales.SalesPerson 资料表的 SalesQuota 和 SalesYTD 资料行上建立非丛集复合索引。
复制程式码
USE AdventureWorks
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_SalesPerson_SalesQuota_SalesYTD')
DROP INDEX IX_SalesPerson_SalesQuota_SalesYTD ON Sales.SalesPerson ;
GO
CREATE NONCLUSTERED INDEX IX_SalesPerson_SalesQuota_SalesYTD
ON Sales.SalesPerson (SalesQuota, SalesYTD);
GO
C. 建立唯一的非丛集索引
下列范例会在 Production.UnitMeasure 资料表的 Name 资料行上建立唯一非丛集索引。索引会强制将资料上的唯一性插入 Name 资料行中。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name from sys.indexes
WHERE name = N'AK_UnitMeasure_Name')
DROP INDEX AK_UnitMeasure_Name ON Production.UnitMeasure;
GO
CREATE UNIQUE INDEX AK_UnitMeasure_Name
ON Production.UnitMeasure(Name);
GO
下列查询会尝试插入一个含有其值与现有资料列相同的资料列,来测试唯一性条件约束。
复制程式码
--Verify the existing value.
SELECT Name FROM Production.UnitMeasure WHERE Name = N'Ounces';
GO
INSERT INTO Production.UnitMeasure (UnitMeasureCode, Name, ModifiedDate)
VALUES ('OC', 'Ounces', GetDate());
产生的错误讯息如下:
复制程式码
Server: Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'UnitMeasure' with unique index 'AK_UnitMeasure_Name'. The statement has been terminated.
D. 使用 IGNORE_DUP_KEY 选项
下列范例分别利用两种不同的选项设定 (先将选项设为 ON,再将选项设为 OFF) 将多个资料列插入暂存资料表中,来示范 IGNORE_DUP_KEY 选项的效果。单一资料列会插入 #Test 资料表中,该资料表则会在第二个多重资料列 INSERT 陈述式执行时刻意造成重复的值。资料表中的资料列计数会传回所插入的资料列数目。
复制程式码
USE AdventureWorks;
GO
CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C2)
WITH (IGNORE_DUP_KEY = ON);
GO
INSERT INTO #Test VALUES (N'OC', N'Ounces', GETDATE());
INSERT INTO #Test SELECT * FROM Production.UnitMeasure;
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
以下是第二个 INSERT 陈述式的结果。
复制程式码
Server: Msg 3604, Level 16, State 1, Line 5 Duplicate key was ignored.
Number of rows
--------------
38
请注意,从 Production.UnitMeasure 资料表插入之未违反唯一性条件约束的资料列已顺利插入。发出警告且忽略重复的资料列,但不回复整个交易。
重新执行相同的陈述式,但 IGNORE_DUP_KEY 设为 OFF。
复制程式码
USE AdventureWorks;
GO
CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C2)
WITH (IGNORE_DUP_KEY = OFF);
GO
INSERT INTO #Test VALUES (N'OC', N'Ounces', GETDATE());
INSERT INTO #Test SELECT * FROM Production.UnitMeasure;
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
以下是第二个 INSERT 陈述式的结果。
复制程式码
Server: Msg 2601, Level 14, State 1, Line 5
Cannot insert duplicate key row in object '#Test' with unique index
'AK_Index'. The statement has been terminated.
Number of rows
--------------
1
请注意,Production.UnitMeasure 资料表中即使只有一个资料列违反 UNIQUE 索引约束条件,资料表中的所有资料列就无法插入资料表中。
E. 利用 DROP_EXISTING 来卸除和重新建立索引
下列范例会利用 DROP_EXISTING 选项,在 Production.WorkOrder 资料表的 ProductID 资料行上卸除及重新建立现有的索引。也会设定 FILLFACTOR 和 PAD_INDEX 选项。
复制程式码
USE AdventureWorks;
GO
CREATE NONCLUSTERED INDEX IX_WorkOrder_ProductID
ON Production.WorkOrder(ProductID)
WITH (FILLFACTOR = 80,
PAD_INDEX = ON,
DROP_EXISTING = ON);
GO
F. 在检视上建立索引
下列范例在该检视上建立检视和索引。内含使用索引检视的两项查询。
复制程式码
USE AdventureWorks;
GO
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('Sales.vOrders', 'view') IS NOT NULL
DROP VIEW Sales.vOrders ;
GO
CREATE VIEW Sales.vOrders
WITH SCHEMABINDING
AS
SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Revenue,
OrderDate, ProductID, COUNT_BIG(*) AS COUNT
FROM Sales.SalesOrderDetail AS od, Sales.SalesOrderHeader AS o
WHERE od.SalesOrderID = o.SalesOrderID
GROUP BY OrderDate, ProductID;
GO
--Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_V1
ON Sales.vOrders (OrderDate, ProductID);
GO
--This query can use the indexed view even though the view is
--not specified in the FROM clause.
SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Rev,
OrderDate, ProductID
FROM Sales.SalesOrderDetail AS od
JOIN Sales.SalesOrderHeader AS o ON od.SalesOrderID=o.SalesOrderID
AND ProductID BETWEEN 700 and 800
AND OrderDate >= CONVERT(datetime,'05/01/2002',101)
GROUP BY OrderDate, ProductID
ORDER BY Rev DESC;
GO
--This query can use the above indexed view.
SELECT OrderDate, SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Rev
FROM Sales.SalesOrderDetail AS od
JOIN Sales.SalesOrderHeader AS o ON od.SalesOrderID=o.SalesOrderID
AND DATEPART(mm,OrderDate)= 3
AND DATEPART(yy,OrderDate) = 2002
GROUP BY OrderDate
ORDER BY OrderDate ASC;
GO
G. 利用内含 (非索引键) 资料行建立索引
下列范例会利用一个索引键资料行 (PostalCode) 和四个非索引键资料行 (AddressLine1、AddressLine2、City、StateProvinceID) 来建立非丛集索引。其后有一个由索引处理的查询。若要显示查询最佳化工具所选取的索引,请先在 SQL Server Management Studio 的 [查询] 功能表上选取 [显示实际执行计划],再执行查询。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_Address_PostalCode')
DROP INDEX IX_Address_PostalCode ON Person.Address;
GO
CREATE NONCLUSTERED INDEX IX_Address_PostalCode
ON Person.Address (PostalCode)
INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID);
GO
SELECT AddressLine1, AddressLine2, City, StateProvinceID, PostalCode
FROM Person.Address
WHERE PostalCode BETWEEN N'98000' and N'99999';
GO
H. 建立主要 XML 索引
下列范例会在 Production.ProductModel 资料表的 CatalogDescription 资料行上建立主要 XML 索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT * FROM sys.indexes
WHERE name = N'PXML_ProductModel_CatalogDescription')
DROP INDEX PXML_ProductModel_CatalogDescription
ON Production.ProductModel;
GO
CREATE PRIMARY XML INDEX PXML_ProductModel_CatalogDescription
ON Production.ProductModel (CatalogDescription);
GO
I. 建立次要 XML 索引
下列范例会在 Production.ProductModel 资料表的 CatalogDescription 资料行上建立次要 XML 索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IXML_ProductModel_CatalogDescription_Path')
DROP INDEX IXML_ProductModel_CatalogDescription_Path
ON Production.ProductModel;
GO
CREATE XML INDEX IXML_ProductModel_CatalogDescription_Path
ON Production.ProductModel (CatalogDescription)
USING XML INDEX PXML_ProductModel_CatalogDescription FOR PATH ;
GO
J. 建立资料分割索引
下列范例会在 TransactionsPS1 这个现有的资料分割配置上建立非丛集资料分割索引。这个范例假设您已安装资料分割索引范例。如需安装资讯,请参阅<Readme_PartitioningScript>。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_TransactionHistory_ReferenceOrderID')
DROP INDEX IX_TransactionHistory_ReferenceOrderID
ON Production.TransactionHistory;
GO
CREATE NONCLUSTERED INDEX IX_TransactionHistory_ReferenceOrderID
ON Production.TransactionHistory
范例
A. 建立简单的非丛集索引
下列范例会在 Purchasing.ProductVendor 资料表的 VendorID 资料行上建立非丛集索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_ProductVendor_VendorID')
DROP INDEX IX_ProductVendor_VendorID ON Purchasing.ProductVendor;
GO
CREATE INDEX IX_ProductVendor_VendorID
ON Purchasing.ProductVendor (VendorID);
GO
B. 建立简单的非丛集复合索引
下列范例会在 Sales.SalesPerson 资料表的 SalesQuota 和 SalesYTD 资料行上建立非丛集复合索引。
复制程式码
USE AdventureWorks
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_SalesPerson_SalesQuota_SalesYTD')
DROP INDEX IX_SalesPerson_SalesQuota_SalesYTD ON Sales.SalesPerson ;
GO
CREATE NONCLUSTERED INDEX IX_SalesPerson_SalesQuota_SalesYTD
ON Sales.SalesPerson (SalesQuota, SalesYTD);
GO
C. 建立唯一的非丛集索引
下列范例会在 Production.UnitMeasure 资料表的 Name 资料行上建立唯一非丛集索引。索引会强制将资料上的唯一性插入 Name 资料行中。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name from sys.indexes
WHERE name = N'AK_UnitMeasure_Name')
DROP INDEX AK_UnitMeasure_Name ON Production.UnitMeasure;
GO
CREATE UNIQUE INDEX AK_UnitMeasure_Name
ON Production.UnitMeasure(Name);
GO
下列查询会尝试插入一个含有其值与现有资料列相同的资料列,来测试唯一性条件约束。
复制程式码
--Verify the existing value.
SELECT Name FROM Production.UnitMeasure WHERE Name = N'Ounces';
GO
INSERT INTO Production.UnitMeasure (UnitMeasureCode, Name, ModifiedDate)
VALUES ('OC', 'Ounces', GetDate());
产生的错误讯息如下:
复制程式码
Server: Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'UnitMeasure' with unique index 'AK_UnitMeasure_Name'. The statement has been terminated.
D. 使用 IGNORE_DUP_KEY 选项
下列范例分别利用两种不同的选项设定 (先将选项设为 ON,再将选项设为 OFF) 将多个资料列插入暂存资料表中,来示范 IGNORE_DUP_KEY 选项的效果。单一资料列会插入 #Test 资料表中,该资料表则会在第二个多重资料列 INSERT 陈述式执行时刻意造成重复的值。资料表中的资料列计数会传回所插入的资料列数目。
复制程式码
USE AdventureWorks;
GO
CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C2)
WITH (IGNORE_DUP_KEY = ON);
GO
INSERT INTO #Test VALUES (N'OC', N'Ounces', GETDATE());
INSERT INTO #Test SELECT * FROM Production.UnitMeasure;
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
以下是第二个 INSERT 陈述式的结果。
复制程式码
Server: Msg 3604, Level 16, State 1, Line 5 Duplicate key was ignored.
Number of rows
--------------
38
请注意,从 Production.UnitMeasure 资料表插入之未违反唯一性条件约束的资料列已顺利插入。发出警告且忽略重复的资料列,但不回复整个交易。
重新执行相同的陈述式,但 IGNORE_DUP_KEY 设为 OFF。
复制程式码
USE AdventureWorks;
GO
CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);
GO
CREATE UNIQUE INDEX AK_Index ON #Test (C2)
WITH (IGNORE_DUP_KEY = OFF);
GO
INSERT INTO #Test VALUES (N'OC', N'Ounces', GETDATE());
INSERT INTO #Test SELECT * FROM Production.UnitMeasure;
GO
SELECT COUNT(*)AS [Number of rows] FROM #Test;
GO
DROP TABLE #Test;
GO
以下是第二个 INSERT 陈述式的结果。
复制程式码
Server: Msg 2601, Level 14, State 1, Line 5
Cannot insert duplicate key row in object '#Test' with unique index
'AK_Index'. The statement has been terminated.
Number of rows
--------------
1
请注意,Production.UnitMeasure 资料表中即使只有一个资料列违反 UNIQUE 索引约束条件,资料表中的所有资料列就无法插入资料表中。
E. 利用 DROP_EXISTING 来卸除和重新建立索引
下列范例会利用 DROP_EXISTING 选项,在 Production.WorkOrder 资料表的 ProductID 资料行上卸除及重新建立现有的索引。也会设定 FILLFACTOR 和 PAD_INDEX 选项。
复制程式码
USE AdventureWorks;
GO
CREATE NONCLUSTERED INDEX IX_WorkOrder_ProductID
ON Production.WorkOrder(ProductID)
WITH (FILLFACTOR = 80,
PAD_INDEX = ON,
DROP_EXISTING = ON);
GO
F. 在检视上建立索引
下列范例在该检视上建立检视和索引。内含使用索引检视的两项查询。
复制程式码
USE AdventureWorks;
GO
--Set the options to support indexed views.
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,
QUOTED_IDENTIFIER, ANSI_NULLS ON;
GO
--Create view with schemabinding.
IF OBJECT_ID ('Sales.vOrders', 'view') IS NOT NULL
DROP VIEW Sales.vOrders ;
GO
CREATE VIEW Sales.vOrders
WITH SCHEMABINDING
AS
SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Revenue,
OrderDate, ProductID, COUNT_BIG(*) AS COUNT
FROM Sales.SalesOrderDetail AS od, Sales.SalesOrderHeader AS o
WHERE od.SalesOrderID = o.SalesOrderID
GROUP BY OrderDate, ProductID;
GO
--Create an index on the view.
CREATE UNIQUE CLUSTERED INDEX IDX_V1
ON Sales.vOrders (OrderDate, ProductID);
GO
--This query can use the indexed view even though the view is
--not specified in the FROM clause.
SELECT SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Rev,
OrderDate, ProductID
FROM Sales.SalesOrderDetail AS od
JOIN Sales.SalesOrderHeader AS o ON od.SalesOrderID=o.SalesOrderID
AND ProductID BETWEEN 700 and 800
AND OrderDate >= CONVERT(datetime,'05/01/2002',101)
GROUP BY OrderDate, ProductID
ORDER BY Rev DESC;
GO
--This query can use the above indexed view.
SELECT OrderDate, SUM(UnitPrice*OrderQty*(1.00-UnitPriceDiscount)) AS Rev
FROM Sales.SalesOrderDetail AS od
JOIN Sales.SalesOrderHeader AS o ON od.SalesOrderID=o.SalesOrderID
AND DATEPART(mm,OrderDate)= 3
AND DATEPART(yy,OrderDate) = 2002
GROUP BY OrderDate
ORDER BY OrderDate ASC;
GO
G. 利用内含 (非索引键) 资料行建立索引
下列范例会利用一个索引键资料行 (PostalCode) 和四个非索引键资料行 (AddressLine1、AddressLine2、City、StateProvinceID) 来建立非丛集索引。其后有一个由索引处理的查询。若要显示查询最佳化工具所选取的索引,请先在 SQL Server Management Studio 的 [查询] 功能表上选取 [显示实际执行计划],再执行查询。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_Address_PostalCode')
DROP INDEX IX_Address_PostalCode ON Person.Address;
GO
CREATE NONCLUSTERED INDEX IX_Address_PostalCode
ON Person.Address (PostalCode)
INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID);
GO
SELECT AddressLine1, AddressLine2, City, StateProvinceID, PostalCode
FROM Person.Address
WHERE PostalCode BETWEEN N'98000' and N'99999';
GO
H. 建立主要 XML 索引
下列范例会在 Production.ProductModel 资料表的 CatalogDescription 资料行上建立主要 XML 索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT * FROM sys.indexes
WHERE name = N'PXML_ProductModel_CatalogDescription')
DROP INDEX PXML_ProductModel_CatalogDescription
ON Production.ProductModel;
GO
CREATE PRIMARY XML INDEX PXML_ProductModel_CatalogDescription
ON Production.ProductModel (CatalogDescription);
GO
I. 建立次要 XML 索引
下列范例会在 Production.ProductModel 资料表的 CatalogDescription 资料行上建立次要 XML 索引。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IXML_ProductModel_CatalogDescription_Path')
DROP INDEX IXML_ProductModel_CatalogDescription_Path
ON Production.ProductModel;
GO
CREATE XML INDEX IXML_ProductModel_CatalogDescription_Path
ON Production.ProductModel (CatalogDescription)
USING XML INDEX PXML_ProductModel_CatalogDescription FOR PATH ;
GO
J. 建立资料分割索引
下列范例会在 TransactionsPS1 这个现有的资料分割配置上建立非丛集资料分割索引。这个范例假设您已安装资料分割索引范例。如需安装资讯,请参阅<Readme_PartitioningScript>。
复制程式码
USE AdventureWorks;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_TransactionHistory_ReferenceOrderID')
DROP INDEX IX_TransactionHistory_ReferenceOrderID
ON Production.TransactionHistory;
GO
CREATE NONCLUSTERED INDEX IX_TransactionHistory_ReferenceOrderID
ON Production.TransactionHistory
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
索引可以缩短查询速度
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询