索引试验
USE [Test]
GO
/****** 对象: Table [dbo].[t_index] 脚本日期: 11/19/2008 10:49:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[t_index](
[ID] [int] NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Age] [int] NOT NULL,
CONSTRAINT [PK_t_index] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND name = N'IX_first_include_lastName')
CREATE NONCLUSTERED INDEX [IX_first_include_lastName] ON [dbo].[t_index]
(
[FirstName] ASC
)
INCLUDE ( [LastName]) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND name = N'IX_t_index')
CREATE NONCLUSTERED INDEX [IX_t_index] ON [dbo].[t_index]
(
[FirstName] ASC,
[LastName] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
truncate table t_index;
GO
SET NOCOUNT ON;
DECLARE @i INT;
SET @i = 1;
WHILE @i < 10000
BEGIN
INSERT INTO [Test].[dbo].[t_index]
([ID]
,[FirstName]
,[LastName]
,[Age])
VALUES
(@i
,'first-name-'+ ltrim(str(@i%1000+10))
,'last-name-'+ ltrim(str(@i%500+10))
,@i%30+20)
SET @i = @i + 1;
END
GO
SELECT TOP 100 * FROM t_index;
--聚集索引扫描或者扫描一定范围
GO
SELECT TOP 100 * FROM t_index ORDER BY FirstName DESC
--
GO
SELECT TOP 100 * FROM t_index ORDER BY Age ASC
--先排序后执行聚集索引扫描,由于Age列上没有索引,所以执行排序开销很大,在排序之后执行的是聚集索引扫描
GO
SELECT TOP 100 * FROM t_index WHERE id = 2000 ORDER BY Age ASC
GO
SELECT * FROM t_index where id IN (1,2,90,3,200,30)
GO
SELECT * FROM t_index WHERE ID = 1 OR ID=2 OR ID = 90 OR ID = 3 OR ID = 200 OR ID = 30
GO
SELECT * FROM t_index WHERE id <100 OR id > 1000
GO
SELECT * FROM t_index WHERE 0=0 AND ID = 100 --AND ID>90
GO
---使用非聚集索引
SELECT top 10 * FROM t_index ORDER BY firstName
GO
--使用不了非聚集索引
SELECT TOP 10 * FROM t_index ORDER BY lastName
GO
--聚集索引扫描
SELECT * FROM t_index WHERE firstName LIKE 'first-name-10%'
--先非聚集索引查找,然后聚集索引查找
SELECT * FROM t_index WHERE firstName LIKE 'first-name-100%'
GO
SELECT * FROM t_index WHERE LastName LIKE 'last-name-10%';
SELECT * FROM t_index WHERE LastName LIKE 'last-name-100%';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10';
SELECT * FROM t_index where LastName = 'last-name-10';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10' AND lastName = 'last-name-10';
SELECT * FROM t_index WHERE lastName = 'last-name-10' AND firstName = 'first-name-10';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10' OR lastName = 'last-name-10';
SELECT * FROM t_index WHERE lastName = 'last-name-10' OR firstName = 'first-name-10';
SELECT DISTINCT * FROM (
SELECT * FROM t_index WHERE firstName = 'first-name-10'
UNION
SELECT * FROM t_index WHERE lastName = 'last-name-10'
) T;
GO
GO
/****** 对象: Table [dbo].[t_index] 脚本日期: 11/19/2008 10:49:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[t_index](
[ID] [int] NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Age] [int] NOT NULL,
CONSTRAINT [PK_t_index] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND name = N'IX_first_include_lastName')
CREATE NONCLUSTERED INDEX [IX_first_include_lastName] ON [dbo].[t_index]
(
[FirstName] ASC
)
INCLUDE ( [LastName]) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[t_index]') AND name = N'IX_t_index')
CREATE NONCLUSTERED INDEX [IX_t_index] ON [dbo].[t_index]
(
[FirstName] ASC,
[LastName] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
GO
truncate table t_index;
GO
SET NOCOUNT ON;
DECLARE @i INT;
SET @i = 1;
WHILE @i < 10000
BEGIN
INSERT INTO [Test].[dbo].[t_index]
([ID]
,[FirstName]
,[LastName]
,[Age])
VALUES
(@i
,'first-name-'+ ltrim(str(@i%1000+10))
,'last-name-'+ ltrim(str(@i%500+10))
,@i%30+20)
SET @i = @i + 1;
END
GO
SELECT TOP 100 * FROM t_index;
--聚集索引扫描或者扫描一定范围
GO
SELECT TOP 100 * FROM t_index ORDER BY FirstName DESC
--
GO
SELECT TOP 100 * FROM t_index ORDER BY Age ASC
--先排序后执行聚集索引扫描,由于Age列上没有索引,所以执行排序开销很大,在排序之后执行的是聚集索引扫描
GO
SELECT TOP 100 * FROM t_index WHERE id = 2000 ORDER BY Age ASC
GO
SELECT * FROM t_index where id IN (1,2,90,3,200,30)
GO
SELECT * FROM t_index WHERE ID = 1 OR ID=2 OR ID = 90 OR ID = 3 OR ID = 200 OR ID = 30
GO
SELECT * FROM t_index WHERE id <100 OR id > 1000
GO
SELECT * FROM t_index WHERE 0=0 AND ID = 100 --AND ID>90
GO
---使用非聚集索引
SELECT top 10 * FROM t_index ORDER BY firstName
GO
--使用不了非聚集索引
SELECT TOP 10 * FROM t_index ORDER BY lastName
GO
--聚集索引扫描
SELECT * FROM t_index WHERE firstName LIKE 'first-name-10%'
--先非聚集索引查找,然后聚集索引查找
SELECT * FROM t_index WHERE firstName LIKE 'first-name-100%'
GO
SELECT * FROM t_index WHERE LastName LIKE 'last-name-10%';
SELECT * FROM t_index WHERE LastName LIKE 'last-name-100%';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10';
SELECT * FROM t_index where LastName = 'last-name-10';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10' AND lastName = 'last-name-10';
SELECT * FROM t_index WHERE lastName = 'last-name-10' AND firstName = 'first-name-10';
GO
SELECT * FROM t_index WHERE firstName = 'first-name-10' OR lastName = 'last-name-10';
SELECT * FROM t_index WHERE lastName = 'last-name-10' OR firstName = 'first-name-10';
SELECT DISTINCT * FROM (
SELECT * FROM t_index WHERE firstName = 'first-name-10'
UNION
SELECT * FROM t_index WHERE lastName = 'last-name-10'
) T;
GO