第一次做优化,问一下这个语句怎样优化,感觉很慢,like语句

lgr0727 2010-08-05 03:56:20
select count(*) as counter
from tableA
where effectDate >= getdate()
and workLocus like '%goung bing%'
and isUnderWay = 1
and delFlag = 0


只要是 like语句,不会走索引的,有没有其他性能比较高的方法替代。
...全文
274 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
yulei243007703 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 lgr0727 的回复:]
select count(*) as counter
from tableA
where effectDate >= getdate()
and workLocus like '%goung bing%'
and isUnderWay = 1
and delFlag = 0


只要是 like语句,不会走索引的,有没有其他性能比较高的方法替代。
[/Quote]
那是因为你没有针对workLocus建索引,你可以针对varchar、nvarchar、text等字符类型的数据见全文索引:create fulltext index
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 weasle 的回复:]

看了下,索引先用整形,然后时间的快点儿,楼上的正确。不过我想问下:楼上为什么后面加个include(workLocus) 有这个语法吗?
[/Quote]
有。2005新特性,包含列索引。
http://msdn.microsoft.com/zh-cn/library/ms190806%28SQL.90%29.aspx
lg314 2010-08-08
  • 打赏
  • 举报
回复
建个索引
  create index tableA_Index on tableA(isUnderWay,delFlag,effectDate) include(workLocus)
obuntu 2010-08-08
  • 打赏
  • 举报
回复
提供表结构,数据信息,或者直接贴执行计划。
正如楼上所说的,类似于like '%%'是不会走索引的。
人鱼传说 2010-08-08
  • 打赏
  • 举报
回复
看看你的执行计划,看哪里占用的时间最多,再针对优化
weasle 2010-08-08
  • 打赏
  • 举报
回复
看了下,索引先用整形,然后时间的快点儿,楼上的正确。不过我想问下:楼上为什么后面加个include(workLocus) 有这个语法吗?
me_child 2010-08-07
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gaojier1000 的回复:]
这样的like 不可能利用到索引,因为索引根本就不起作用。
[/Quote]


up....
  • 打赏
  • 举报
回复
这样的like 不可能利用到索引,因为索引根本就不起作用。

jm_qinqin 2010-08-06
  • 打赏
  • 举报
回复
effectDate创建索引。
Johnson 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shutao917 的回复:]
SQL code

create index index_abcds on tableA (effectDate ,isUnderWay,delFlag,workLocus );
go
select count(*) as counter
from tableA
where effectDate >= getdate()
and isUnderWay = 1
……
[/Quote]

嗯,如果可以的话,effectDate上建立索引,甚至分区.
Q315054403 2010-08-06
  • 打赏
  • 举报
回复
这个要注意结构设计了,不单是SQL的问题
lrjt1980 2010-08-06
  • 打赏
  • 举报
回复
设个主键,后面几项用个括号括起来。
topking2001 2010-08-06
  • 打赏
  • 举报
回复
charindex??
lcqtgb 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wkzhx 的回复:]
like '%goung bing%'
不会走索引
like 'goung bing%'
这样就会走索引
[/Quote]

汗。。我不是这样理解的。。两个%是包含。。前%和后%分别代表前面和后面任意。。
wkzhx 2010-08-05
  • 打赏
  • 举报
回复
like '%goung bing%'
不会走索引
like 'goung bing%'
这样就会走索引
情殇无限 2010-08-05
  • 打赏
  • 举报
回复

create index index_abcds on tableA (effectDate ,isUnderWay,delFlag,workLocus );
go
select count(*) as counter
from tableA
where effectDate >= getdate()
and isUnderWay = 1
and delFlag = 0
and workLocus like '%goung bing%'

建个索引试试
billpu 2010-08-05
  • 打赏
  • 举报
回复
不是所有使用like关键字的select 语句都无法使用索引
你看看执行计划,真的没用到索引吗?直接表扫描?
天-笑 2010-08-05
  • 打赏
  • 举报
回复
你这语句是 最简单的了,等待高人解决

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧