sql查询like和charindex和substring比较性能测试

300万条记录 like 和 charindex 函数以及substring函数性能比较:

环境:sql2005

数据量:300万

查询结果数据量:2849999

机器环境

P4 3.0双核 2G内存

1. 执行语句

表结构:

USE [Test]
GO
/****** 对象:  Table [dbo].[TabTest]    脚本日期: 03/22/2011 09:46:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TabTest](
	[id] [int] IDENTITY(1,1) NOT NULL,
	[ypmc] [nvarchar](50) NULL,
	[ypdm] [nvarchar](50) NULL,
	[cdmc] [nvarchar](50) NULL,
	[cddm] [nvarchar](50) NULL,
	[gg] [nvarchar](50) NULL,
	[list] [nvarchar](50) NULL,
	[doc1] [nvarchar](50) NULL,
	[ywuserid] [int] NULL,
	[flag] [tinyint] NULL,
	[flagstr] [nchar](10) NULL
) ON [PRIMARY]
/*仅list字段建聚集索引*/
select * from TabTest where list like '%a%' and ywuserid=10215
select * from TabTest where charindex('a',list)>0 and ywuserid=10215
select * from TabTest where substring(list ,1,1) = 'a' and ywuserid=10215
/*仅list字段建非聚集索引*/
select * from TabTest where list like '%a%' and ywuserid=10215
select * from TabTest where charindex('a',list)>0 and ywuserid=10215
select * from TabTest where substring(list ,1,1) = 'a' and ywuserid=10215
/*list字段不建索引*/
select * from TabTest where list like '%a%' and ywuserid=10215
select * from TabTest where charindex('a',list)>0 and ywuserid=10215
select * from TabTest where substring(list ,1,1) = 'a' and ywuserid=10215
/*查询字段和list建索引*/
select * from TabTest where list like '%a%' and ywuserid=10215
select * from TabTest where charindex('a',list)>0 and ywuserid=10215
select * from TabTest where substring(list ,1,1) = 'a' and ywuserid=10215
/*全部字段和list建索引*/
select * from TabTest where list like '%a%' and ywuserid=10215
select * from TabTest where charindex('a',list)>0 and ywuserid=10215
select * from TabTest where substring(list ,1,1) = 'a' and ywuserid=10215

仅list字段建聚集索引性能分析:
sql查询like和charindex和substring比较性能测试_第1张图片
仅list字段建非聚集索引性能分析:
sql查询like和charindex和substring比较性能测试_第2张图片

sql查询like和charindex和substring比较性能测试_第3张图片

字段不建索引性能分析:
sql查询like和charindex和substring比较性能测试_第4张图片

看到上面结果后:

试了一下如下查询:

/*查询300万条*/
/*仅list字段建聚集索引*/
select * from TabTest where list like '%a%' 
select * from TabTest where charindex('a',list)>0 
select * from TabTest where substring(list ,1,1) = 'a' 
/*仅list字段建非聚集索引*/
select * from TabTest where list like '%a%' 
select * from TabTest where charindex('a',list)>0 
select * from TabTest where substring(list ,1,1) = 'a' 
/*list字段不建索引*/
select * from TabTest where list like '%a%'
select * from TabTest where charindex('a',list)>0 
select * from TabTest where substring(list ,1,1) = 'a' 

/*list字段不建索引*/

/*仅list字段建非聚集索引*/

sql查询like和charindex和substring比较性能测试_第5张图片

sql查询like和charindex和substring比较性能测试_第6张图片

/*仅list字段建聚集索引*/
sql查询like和charindex和substring比较性能测试_第7张图片
sql查询like和charindex和substring比较性能测试_第8张图片

自己看看性能比较结果吧!

你可能感兴趣的:(substring)