查询语句相当慢

yym1202000 2008-12-15 04:35:19
我想查询A表里的记录,如我要查询A表里的当前时间前三月name是tom的记录,但是这个记录不存在于字段job like'经理'里面
如:
select * from (select t.iFollowid from ent_plan t
where t.dttrackingdate between to_date('20080901','yyyy-MM-dd') and to_date('20080930','yyyy-MM-dd') and
t.chfollowresult like '%3%' ) y where y.iFollowid not in
(select t.iFollowid from ent_plant where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%')

这条语句相当慢花了24秒,大哥用什么解决呢?
...全文
204 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
davinical 2009-05-22
  • 打赏
  • 举报
回复
必须慢,表结构建立的不合理
或渊在田 2009-05-22
  • 打赏
  • 举报
回复
select * from
(
select t.iFollowid from ent_plan t
where t.dttrackingdate
between to_date('20080901','yyyy-MM-dd')
and to_date('20080930','yyyy-MM-dd')
and t.chfollowresult like '%3%'
) y
where y.iFollowid not in
(
select t.iFollowid from ent_plant
where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%'
)

你相当于查询了三次ent_plan表,你说你的速度能不慢下来吗?
或渊在田 2009-05-22
  • 打赏
  • 举报
回复
select * from
(
select t.iFollowid from ent_plan t
where t.dttrackingdate
between to_date('20080901','yyyy-MM-dd')
and to_date('20080930','yyyy-MM-dd')
and t.chfollowresult like '%3%'
) y
where y.iFollowid not in
(
select t.iFollowid from ent_plant
where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%'
)

你相当于查询了三次ent_plan表,你说你的速度能不慢下来吗?
chenfawei 2009-04-29
  • 打赏
  • 举报
回复
学习ing,看看贴获得经验,少走弯路
anan208 2009-04-06
  • 打赏
  • 举报
回复
用 NOT EXISTS代替not in
对于LIKE"%"是走不到索引的。
wyg2002668 2009-03-30
  • 打赏
  • 举报
回复
给表加索引
HelloTWorld 2009-03-26
  • 打赏
  • 举报
回复
不要在条件里用函数,你可以在前面的变量里面存储函数运行的结果,然后放在条件里面。
leef_zh 2009-03-04
  • 打赏
  • 举报
回复
我想查询A表里的记录,如我要查询A表里的当前时间前三月name是tom的记录,
但是这个记录不存在于字段job like'经理'里面


-------------
select * from A
where name = 'tom'
and time = 取得前三月的函数(函数不记得了!!)
and job not like '经理'

这样不就可以了,为什么还举个那么多like的例子??

--------------------------
select * from
(
select t.iFollowid from ent_plan t
where t.dttrackingdate
between to_date('20080901','yyyy-MM-dd')
and to_date('20080930','yyyy-MM-dd')
and t.chfollowresult like '%3%'
) y
where y.iFollowid not in
(
select t.iFollowid from ent_plant
where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%'
)

------------------
select iFollowid from ent_plant
where t.dttrackingdate
between to_date('20080901','yyyy-MM-dd')
and to_date('20080930','yyyy-MM-dd')
and t.chfollowresult like '%3%'
and chfollow not like '%[456]%'



顾问Peng 2009-02-24
  • 打赏
  • 举报
回复
like %%的效率极低,同样not in 也是,可以优化这两个方面
Sky_666 2009-02-21
  • 打赏
  • 举报
回复
少用模糊查询,少用not in ,尽量不要用子查询,用内连接!
leonwan 2008-12-26
  • 打赏
  • 举报
回复
不要用not in ,效率低下
jlj84237485 2008-12-25
  • 打赏
  • 举报
回复
来看看
sunray_001 2008-12-25
  • 打赏
  • 举报
回复
来看看……
feifeiyiwen 2008-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 haierjodn 的回复:]
select t.iFollowid from ent_plant where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%
对它下手吧,这模糊不怎么好。
[/Quote]
我也有同感
hm8030 2008-12-23
  • 打赏
  • 举报
回复
最后前面不要加%,這樣可設置索引
haierjodn 2008-12-22
  • 打赏
  • 举报
回复
select t.iFollowid from ent_plant where t.chfollow like '%4%' or t.chFollow like '%5%'
or t.chFollow like '%6%
对它下手吧,这模糊不怎么好。
通过sql分析的学习,了解什么是sql,以及SQL会引起那些性能问题。清楚sql日志的设置,然后再通过sql分析工具的学习,清楚sql分析的步骤和流程。sql分析工具:mysqldumpslow工具、explain工具、profile工具、Optimizer Trace工具。 提供课程中所使用的sql语句。 课程内容:第一章:课程简介1、课程介绍2、课程大纲 第二章:sql简介1、sql简介2、sql会引起的问题 第三章:日志的设置1、sql的分析流程2、日志参数理解3、日志参数设置:第1种方式:my.ini文件设置4、日志参数设置:第2种方式:sql脚本设置5、日志参数设置-效果验证 第四章:如何发现sql1、如何发现sql:第1种方式:日志文件2、如何发现sql:第2种方式:mysql库的slow_log表 第五章:sql分析工具1、sql提取-mysqldumpslow工具-使用方法2、sql提取-mysqldumpslow工具-操作实战3、sql的执行计划分析-explain分析-执行计划结果说明4、sql的执行计划分析-explain分析-索引介绍+type类型举例5、sql的资源开销分析-profile分析-分析步骤6、sql的资源开销分析-profile分析-show profile执行阶段说明7、sql的资源开销分析-profile分析-完整列表说明+操作实战8、sql的跟踪分析-Optimizer Trace分析-分析步骤9、sql的跟踪分析-Optimizer Trace表的介绍10、索引失效场景举例 第六章:日志清理1、日志清理

8,028

社区成员

发帖
与我相关
我的任务
社区描述
高性能数据库开发
社区管理员
  • 高性能数据库开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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