Home  >  Article  >  Database  >  mssql 30万条数据 搜索文本字段的各种方式对比

mssql 30万条数据 搜索文本字段的各种方式对比

WBOY
WBOYOriginal
2016-06-07 18:00:021264browse

30万条,有ID列但无主键,在要搜索的“分类”字段上建有非聚集索引

数据库:
30万条,有ID列但无主键,在要搜索的“分类”字段上建有非聚集索引
过程T-SQL:
代码如下:
/*
用户自定义函数:执行时间在1150-1200毫秒左右
CREATE FUNCTION [dbo].[gethl] (@types nvarchar(4))
RETURNS table AS
return select 书名 from 图书三十万条 Where 分类 Like '%'+@types+'%'
存储过程:
CREATE PROCEDURE [dbo].[getfl](@typen nvarchar(4))
AS
select 书名 from 图书三十万条 Where 分类 Like '%'+@typen+'%'
*/


代码如下:
declare @a datetime,@b nvarchar(4)
set @a=getDate()
select 书名 from 图书三十万条 Where 分类 Like '%医学%' --“分类”列有非聚集索引,比聚集索引1150快一点,差不多执行时间在1100左右
-- select 书名 from gethl('医学') --使用用户自定义函数,效率和建立聚集索引一样,还稍慢一点 在1150-1200
-- Execute getfl '医学' --调用存储过程不能用括号包含参数 Execute getfl('医学')
-- select 书名 from VIEW1 --视图
print '运行时间:
print datediff(ms,@a,getDate())


结论:
1、以上各种使用直接查询、函数、视图、存储过程性能都差不多;
2、在这种文本字段,非聚集比聚集索引效果好。
比这些更好的方法是,在另外一个表上建立相应的检索ID,会更快!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn