select-同样的sql只有查询的日期不同但执行速度相差很多

问题描述

同样的sql只有查询的日期不同但执行速度相差很多
SELECT messagestatusreceivernamemain.registnosendernameto_char(send_time) as send_timemobileriskcode
from prplflsmssend ps left join prplflsmslog pl on pl.smssendid=ps.id prplflmaininfo main
where trim(smstype)<>'' and pl.registno = main.registno and smstype='35'
and exists ( select pdrc.comCode from PrpDcompany pdrc where ( pdrc.upperPath like '000000004200000042010000%' ) and main.comcode = pdrc.comCode )

and operatedate between to_date('2015-09-04 00:00:00') and to_date('2015-09-04 23:59:59') ; --执行速度 大约30分钟

select messagestatusreceivernamemain.registnosendernameto_char(send_time) as send_timemobileriskcode
from prplflsmssend ps left join prplflsmslog pl on pl.smssendid=ps.id prplflmaininfo main
where trim(smstype)<>'' and pl.registno = main.registno and smstype='18'
and exists ( select pdrc.comCode from PrpDcompany pdrc where ( pdrc.upperPath like '000000004200000042010000%' ) and main.comcode = pdrc.comCode )

and operatedate >= to_date('2015-09-09 00:00:00') and operatedate<= to_date('2015-09-09 23:59:59') 执行速度 几秒钟

解决方案

执行查询计划,看慢到哪里了!

解决方案二:

QUERY: (OPTIMIZATION TIMESTAMP: 09-10-2015 16:17:08)

SELECT messagestatusreceivernamemain.registnosendernameto_char(send_time) as send_timemobileriskcode
from prplflsmssend ps left join prplflsmslog pl on pl.smssendid=ps.id prplflmaininfo main
where trim(smstype)<>'' and pl.registno = main.registno and smstype='35'
and exists ( select pdrc.comCode from PrpDcompany pdrc where ( pdrc.upperPath like '000000004200000042010000%' ) and main.comcode = pdrc.comCode )

and operatedate >= to_date('2015-09-05 00:00:00') and operatedate<= to_date('2015-09-05 23:59:59')

Estimated Cost: 3112
Estimated # of Rows Returned: 4204

1) flowctrl.main: SEQUENTIAL SCAN

2) flowctrl.pdrc: INDEX PATH

    Filters: flowctrl.pdrc.upperpath LIKE '000000004200000042010000%' (1) Index Name: flowctrl. 101_6    Index Keys: comcode   (Serial fragments: ALL)    Lower Index Filter: flowctrl.main.comcode = flowctrl.pdrc.comcode 

NESTED LOOP JOIN

3) flowctrl.pl: SEQUENTIAL SCAN

    Filters:     Table Scan Filters: (((flowctrl.pl.smstype = '35' AND flowctrl.pl.operatedate <= datetime(2015-09-05 23:59:59.00000) year to fraction(5) ) AND flowctrl.pl.operatedate >= datetime(2015-09-05 00:00:00.00000) year to fraction(5) ) AND TRIM ( BOTH ' ' FROM flowctrl.pl.smstype ) != '' ) 

DYNAMIC HASH JOIN
Dynamic Hash Filters: flowctrl.pl.registno = flowctrl.main.registno

4) flowctrl.ps: INDEX PATH

(1) Index Name: flowctrl. 118_47    Index Keys: id   (Serial fragments: ALL)    Lower Index Filter: flowctrl.pl.smssendid = flowctrl.ps.id 

NESTED LOOP JOIN

---------------华丽丽的分割线-------------

QUERY: (OPTIMIZATION TIMESTAMP: 09-10-2015 16:17:31)

SELECT messagestatusreceivernamemain.registnosendernameto_char(send_time) as send_timemobileriskcode
from prplflsmssend ps left join prplflsmslog pl on pl.smssendid=ps.id prplflmaininfo main
where trim(smstype)<>'' and pl.registno = main.registno and smstype='35'
and exists ( select pdrc.comCode from PrpDcompany pdrc where ( pdrc.upperPath like '000000004200000042010000%' ) and main.comcode = pdrc.comCode )

and operatedate >= to_date('2015-09-09 00:00:00') and operatedate<= to_date('2015-09-09 23:59:59')

Estimated Cost: 3112
Estimated # of Rows Returned: 4204

1) flowctrl.main: SEQUENTIAL SCAN

2) flowctrl.pdrc: INDEX PATH

    Filters: flowctrl.pdrc.upperpath LIKE '000000004200000042010000%' (1) Index Name: flowctrl. 101_6    Index Keys: comcode   (Serial fragments: ALL)    Lower Index Filter: flowctrl.main.comcode = flowctrl.pdrc.comcode 

NESTED LOOP JOIN

3) flowctrl.pl: SEQUENTIAL SCAN

    Filters:     Table Scan Filters: (((flowctrl.pl.smstype = '35' AND flowctrl.pl.operatedate <= datetime(2015-09-09 23:59:59.00000) year to fraction(5) ) AND flowctrl.pl.operatedate >= datetime(2015-09-09 00:00:00.00000) year to fraction(5) ) AND TRIM ( BOTH ' ' FROM flowctrl.pl.smstype ) != '' ) 

DYNAMIC HASH JOIN
Dynamic Hash Filters: flowctrl.pl.registno = flowctrl.main.registno

4) flowctrl.ps: INDEX PATH

(1) Index Name: flowctrl. 118_47    Index Keys: id   (Serial fragments: ALL)    Lower Index Filter: flowctrl.pl.smssendid = flowctrl.ps.id 

NESTED LOOP JOIN

两个执行计划都一样啊 看不太懂!

解决方案三:
between and也被解析成了 >= <= 了。 你先用 between and 了 应该是数据库服务器缓存了结果,所以在测试 >= <=就快了。 你可以试下执行顺序颠倒下,看看是执行时间!

解决方案四:
这样你把第一条sql执行一遍的时间和第二遍看看有多大差距。

时间: 2024-08-01 12:26:27

select-同样的sql只有查询的日期不同但执行速度相差很多的相关文章

Sql server 数据库中,纯SQL语句查询、执行 单引号问题。

原文:Sql server 数据库中,纯SQL语句查询.执行 单引号问题. 在默认值情况下, select 'abc',Titile from tb_Name;  ---输出内容 是abc: 如果想输出 单引号 'abc,需要使用select '''abc',Titile from tb_Name; ---这里用三个单引号'''abc: select '''abc''',Title from tbName;   输出内容是'abc':两边带有单引号: 谨记:如果字符串包含单引号,则需要在单引号前

SQL怎么查询上次交易日期呢?

问题描述 SQL怎么查询上次交易日期呢? 现有test表表结构如下: time goods num2014/5/9 内存条 202014/4/28 硬盘 17....../*海量数据*/2013/7/2 内存条 18 怎么用表自连接的方法查询出上次交易的时间呢?结果表是这样的: time goods num LastTime 如果数据量太大不能用表自连接方法还有其它方法吗? 新人刚入行虚心请教学习感谢了~~~ 解决方案 上次交易日期而不是最新日期select a.test.timea.goods

mysql 查询指定日期时间内容sql查询语句

mysql教程 查询指定日期时间内容sql查询语句 在数据库教程设计时必须注意时间字段最好为int(4)这样,保存在数据库的时一个数字型日期时间截,我们可以用mktime函数求出当前日期的时间截进行加减就OK了,下面看实例 $time = time();   //一个月 $lastMonth = mktime(date('h'),date('i'),date('s'),date('m')-1,date('d'),date('y')); $where .= "  dtime < $lastM

sql 查询指定日期时间记录sql语句与查询实例

sql 查询指定日期时间记录sql语句与查询实例  首先创建一个表abc结构如下    CREATE TABLE `cc`.`loup` (  `id` INT( 4 ) NOT NULL AUTO_INCREMENT ,  `datetimes` INT( 4 ) NULL ,  `ipaddress` VARCHAR( 20 ) NULL ,  PRIMARY KEY ( `id` )  ) ENGINE = MYISAM    再批量插入数据    INSERT INTO `loup` (

查询给定日期是当月的第几周sql查询语句

查询给定日期是当月的第几周sql查询语句 declare @date datetime; set @date = getdate() --思路,给定日期是当年的第几周-给定日期所在月第一天是当年的第几周 select datepart(week,@date)-datepart(week,dateadd(month,datediff(month,0,@date),0))+1 [weekofmonth] select datepart(week,@date)-datepart(week,datead

查询给定日期是当年的第几季度sql语句

查询给定日期是当年的第几季度sql语句 declare @date datetime; set @date = getdate() select datepart(quarter,@date) [quarterofyear]; --返回int型 select datename(quarter,@date) [quarterofyear]; --返回varchar型 go

查询给定日期是当年的第几月的sql语句

查询给定日期是当年的第几月的sql语句 declare @date datetime; set @date = getdate() select datepart(month,@date) [monthofyear]; --返回int型 select datename(month,@date) [monthofyear]; --返回varchar型 select month(@date) [monthofyear];--返回int型 go

查询给定日期所在星期的上一个星期日(星期日为第一天)sql语句

查询给定日期所在星期的上一个星期日(星期日为第一天)sql语句 declare @date  datetime set @date= getdate() --思路:当前日志所在星期的星期日再减1周 --datepart(weekday,date)的返回值与@@datefirst相关 set datefirst 7 -- 或者设置为美国英语set language us_english; (星期日为第一天) select dateadd(week,-1,dateadd(day,1-datepart

sql查询时select id,*无法查询

问题描述 sql查询时select id,*无法查询 select id,* from table;-- 这句sql是错误的: select id,a.* from table a ;--这句就可以执行: 为啥给表设置了别名之后就可以了? 解决方案 或许这就是思想,先记住,慢慢就理解了. 解决方案二: 代表的是全部,id+等于重复了,而起别名就可以避免这个计算机认为重复的情况 解决方案三: 代表全部字段,id字段也被包含在里 解决方案四: 我觉得是这样的,第一个错误本身语法就是有问题,星号里面就