SQLServer地址搜索性能优化地址搜索性能优化
网上关于SQL优化的教程很多,但是比较杂乱。今天我们来从一个简单的例子来探讨下SQLServer地址搜索性能优化
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享。
1.需求
1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内。
1.2 数据库地址表结构和数据:
表TBAddress
表数据
1.3 例子:
e.g. 给出一个字符串如“广 大”,找出地址全路径中包含有“广” 和“大”的所有地址,結果如下:
下面将通过4个方法来实现,再分析其中的性能优劣,然后选择一个比较优的方法。
2.创建表和插入数据
2.1 创建数据表TBAddress
use test;
go
/* create table */
if object_id('TBAddress') is not null
drop table TBAddress;
go
create table TBAddress
(
ID int ,
Parent int not null ,
LevelNo smallint not null ,
Name nvarchar(50) not null ,
constraint PK_TBAddress primary key ( ID )
);
go
create nonclustered index ix_TBAddress_Parent on TBAddress(Parent,LevelNo) include(Name) with(fillfactor=80,pad_index=on);
create nonclustered index ix_TBAddress_Name on TBAddress(Name)include(LevelNo)with(fillfactor=80,pad_index=on);
go
create table
2.2 插入数据
use test
go
/*insert data*/
set nocount on
Begin Try
Begin Tran
Insert Into TBAddress ([ID],[Parent],[LevelNo],[Name])
Select 1,0,0,N'中国' Union All
Select 2,1,1,N'直辖市' Union All
Select 3,1,1,N'辽宁省' Union All
评论0
最新资源