我有一个表,你可以在我的sql server中看到:
<div id="myId"></div>
我在这张表里面有16000000条记录。所以你知道这个表存储了所有关于cars的信息。所以我决定根据汽车模型创建文件组,这意味着我在一个文件组中保存了相同型号的汽车,你可以看到:
CREATE TABLE [dbo].[Cars](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[VIN] [nvarchar](max) NULL,
[ChassisNumber] [nvarchar](max) NULL,
[PlaqueCoded] [nvarchar](max) NULL,
[EngineNumber] [nvarchar](max) NULL,
[SystemType] [nvarchar](max) NULL,
[CarType] [nvarchar](max) NULL,
[CarTipe] [nvarchar](max) NULL,
[FuelType] [nvarchar](max) NULL,
[FuelSystem] [nvarchar](max) NULL,
[Model] [int] NULL,
[Color] [nvarchar](max) NULL,
[SubmitDatetime] [datetime] NOT NULL,
[ExpireDatetime] [datetime] NOT NULL,
[ReferenceOrganization] [nvarchar](max) NULL,
[ReferenceId] [nvarchar](max) NULL,
[Comment] [nvarchar](max) NULL,
CONSTRAINT [PK_Cars] PRIMARY KEY NONCLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [FullOrderDateRangePScheme]([Model])
所以我创建了这个函数
FILEGROUP [Filegroup_1395]
( NAME = N'data_1395', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_1395.ndf' , SIZE = 10240KB , MAXSIZE = 10240000KB , FILEGROWTH = 512000KB ),
FILEGROUP [Filegroup_1396]
( NAME = N'data_1396', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_1396.ndf' , SIZE = 10240KB , MAXSIZE = 10240000KB , FILEGROWTH = 512000KB ),
FILEGROUP [Filegroup_1397]
( NAME = N'data_1397', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\data_1397.ndf' , SIZE = 566976KB , MAXSIZE = 10240000KB , FILEGROWTH = 512000KB )
作为一个注释,VIN是唯一的。我的主要搜索是vin.i在我的桌子上创建这些索引:
CREATE PARTITION FUNCTION [FullOrderDateKeyRangePFN](int) AS RANGE LEFT FOR VALUES ( 1395, 1396, 1397)
GO
/****** Object: PartitionScheme [FullOrderDateRangePScheme] Script Date: 8/8/2017 11:51:38 PM ******/
CREATE PARTITION SCHEME [FullOrderDateRangePScheme] AS PARTITION [FullOrderDateKeyRangePFN] TO ( [Filegroup_1395], [Filegroup_1396], [Filegroup_1397])
GO
我的查询是:
CREATE CLUSTERED INDEX [ClusteredIndex-20170808-232559] ON [dbo].[Cars]
(
[Model] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [FullOrderDateRangePScheme]([Model])
GO
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20170808-232524] ON [dbo].[Cars]
(
[Id] ASC
)
INCLUDE ( [VIN]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [FullOrderDateRangePScheme]([Model])
GO
我的问题是:
我怎样才能做更好的表现呢?我的索引能帮忙吗?
我在调整方面是如此新颖
答案 0 :(得分:1)
您的查询是
select * from cars where vin='IRFC1374GH7162K'
您当前的索引对您的查询一点都没有帮助..
所以为了让你的查询表现更好,我建议..添加一个非聚集索引
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20170808-232524] ON [dbo].[Cars]
(
[vin] ASC
)
INCLUDE ( remaining columns)
这将是一个问题,因为如果您使用的是sql2016之前的版本,则最大索引键大小为900字节,对于大于等于2016的版本,限制为1700字节。
我也看到,你的搜索栏不是那么大(IRFC1374GH7162K
),所以我建议调整到nvarchar(100)。所以所有列都需要,因为你正在做select *
有了这个,你将有一个单独的结构维护......但这有助于你的查询..如果你不需要所有的列,那么开销会更少