SQL执行异常系列之——隐式转换

SQL> select object_id,CREATED from dba_objects where rownum < 10;

 OBJECT_ID CREATED
---------- ------------------
    20 25-JUL-18
    46 25-JUL-18
    28 25-JUL-18
    15 25-JUL-18
    29 25-JUL-18
     3 25-JUL-18
    25 25-JUL-18
    41 25-JUL-18
    54 25-JUL-18

9 rows selected.

select object_name from function where  to_char(created,‘yyyymmdd hh24:mi‘)=‘20180725 12:51‘;

20180725 12:54

drop table tab;
create table tab(id number(20),datetime date);

declare
i number;
dd date;
BEGIN
dd := sysdate;
i:= 0;
for x in 1..5000 loop
--if mod(i,100)=0 then
insert into tab(id,datetime) values(i,dd);
dd := dd+1;
i := i+1;
end loop;
END;
/

create table tab(id varchar2(20),datetime date);

1289             20220429 13:58

29-APR-22

************************
测试1
************************

select * from tab where id=1289 and datetime=‘29-APR-22‘;

select * from tab where id=1289 and datetime=‘29-APR-22‘;SQL> 

no rows selected

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    21 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    21 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(TO_NUMBER("ID")=1289 AND "DATETIME"=‘29-APR-22‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      5  recursive calls
      0  db block gets
     33  consistent gets
      0  physical reads
      0  redo size
    400  bytes sent via SQL*Net to client
    508  bytes received via SQL*Net from client
      1  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      0  rows processed

************************
测试2
************************

select * from tab where id=‘1289‘ and datetime=‘29-APR-22‘;
select * from tab where id=‘1289‘ and datetime=‘29-APR-22‘;

SQL> 

no rows selected

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    21 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    21 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("ID"=‘1289‘ AND "DATETIME"=‘29-APR-22‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      5  recursive calls
      0  db block gets
     33  consistent gets
      0  physical reads
      0  redo size
    400  bytes sent via SQL*Net to client
    508  bytes received via SQL*Net from client
      1  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      0  rows processed

刷新cache之后再执行

SQL> select * from tab where id=‘1289‘ and datetime=‘29-APR-22‘;

no rows selected

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    21 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    21 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("ID"=‘1289‘ AND "DATETIME"=‘29-APR-22‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      0  recursive calls
      0  db block gets
     16  consistent gets
     14  physical reads
      0  redo size
    400  bytes sent via SQL*Net to client
    508  bytes received via SQL*Net from client
      1  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      0  rows processed

************************
测试3
************************
select * from tab where id=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;
SQL> select * from tab where id=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;

ID             DATETIME
-------------------- --------------
1289             29-04-22 13:58

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    13 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    13 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("ID"=‘1289‘ AND TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyy
          ymmdd‘)=‘20220429‘)

Statistics
----------------------------------------------------------
     62  recursive calls
      0  db block gets
     51  consistent gets
      0  physical reads
      0  redo size
    600  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      4  sorts (memory)
      0  sorts (disk)
      1  rows processed

************************
测试4
************************
create index tab_index on tab(id,to_char(datetime,‘yyyymmdd‘));

SQL> /

ID             DATETIME
-------------------- --------------
1289             29-04-22 13:58

Execution Plan
----------------------------------------------------------
Plan hash value: 4028735706

-----------------------------------------------------------------------------------------
| Id  | Operation            | Name    | Rows    | Bytes | Cost (%CPU)| Time    |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |        |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TAB    |     1 |    13 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN        | TAB_INDEX |     1 |    |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("ID"=‘1289‘ AND TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyyymmdd‘)=‘
          20220429‘)

Statistics
----------------------------------------------------------
      0  recursive calls
      0  db block gets
      4  consistent gets
      3  physical reads
      0  redo size
    600  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

************************
测试5
************************

新创建的表
select * from tab where id=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;
SQL> /

    ID DATETIME
---------- --------------
      1289 29-04-22 22:42

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    22 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    22 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("ID"=1289 AND TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyyym
          mdd‘)=‘20220429‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      0  recursive calls
      0  db block gets
     17  consistent gets
     14  physical reads
      0  redo size
    599  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

************************
测试5
************************

create index tab_index on tab(id,to_char(datetime,‘yyyymmdd‘));

SQL> select * from tab where id=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;

    ID DATETIME
---------- --------------
      1289 29-04-22 22:42

Execution Plan
----------------------------------------------------------
Plan hash value: 4028735706

-----------------------------------------------------------------------------------------
| Id  | Operation            | Name    | Rows    | Bytes | Cost (%CPU)| Time    |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |        |     1 |    28 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TAB    |     1 |    28 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN        | TAB_INDEX |     1 |    |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("ID"=1289 AND TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyyymmdd‘)=‘20
          220429‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
     10  recursive calls
      0  db block gets
     27  consistent gets
      3  physical reads
      0  redo size
    599  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

************************
测试6
************************
select * from tab where id=1289 and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;

SQL> select * from tab where id=1289 and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;

    ID DATETIME
---------- --------------
      1289 29-04-22 22:42

Execution Plan
----------------------------------------------------------
Plan hash value: 4028735706

-----------------------------------------------------------------------------------------
| Id  | Operation            | Name    | Rows    | Bytes | Cost (%CPU)| Time    |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |        |     1 |    28 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TAB    |     1 |    28 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN        | TAB_INDEX |     1 |    |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("ID"=1289 AND TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyyymmdd‘)=‘20
          220429‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      7  recursive calls
      0  db block gets
     25  consistent gets
      0  physical reads
      0  redo size
    599  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

************************
测试6
************************
select * from tab where to_char(id)=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;
SQL> select * from tab where to_char(id)=‘1289‘ and to_char(datetime,‘yyyymmdd‘)=‘20220429‘;

    ID DATETIME
---------- --------------
      1289 29-04-22 22:42

Execution Plan
----------------------------------------------------------
Plan hash value: 1995730731

--------------------------------------------------------------------------
| Id  | Operation      | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     |     1 |    28 |     5   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| TAB  |     1 |    28 |     5   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(TO_CHAR("ID")=‘1289‘ AND
          TO_CHAR(INTERNAL_FUNCTION("DATETIME"),‘yyyymmdd‘)=‘20220429‘)

Note
-----
   - dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
      5  recursive calls
      0  db block gets
     35  consistent gets
      0  physical reads
      0  redo size
    599  bytes sent via SQL*Net to client
    519  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
      0  sorts (memory)
      0  sorts (disk)
      1  rows processed

原文地址:https://www.cnblogs.com/dayu-liu/p/9813797.html

时间: 2024-10-12 05:47:27

SQL执行异常系列之——隐式转换的相关文章

【C++自我精讲】基础系列五 隐式转换和显示转换

0 前言 1)C++的类型转换分为两种,一种为隐式转换,另一种为显式转换. 2)C++中应该尽量不要使用转换,尽量使用显式转换来代替隐式转换. 1 隐式转换 定义:隐式转换是系统跟据程序的需要而自动转换的. 1)C++类型(char,int,float,long,double等)的隐式转换: 算术表达式隐式转换顺序为: 1.char - int - long - double 2.float - double //1)算术表达式 int m = 10; double n = m;//n = 10

MySQL SQL优化之字符串索引隐式转换

之前有用户很不解:SQL语句非常简单,就是select * from test_1 where user_id=1 这种类型,而且user_id上已经建立索引了,怎么还是查询很慢? test_1的表结构: CREATE TABLE `test_1` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` varchar(30) NOT NULL, `name` varchar(30) DEFAULT NULL, PRIMARY KEY (`id`),

SQL Server中提前找到隐式转换提升性能的办法

    http://www.cnblogs.com/shanksgao/p/4254942.html 高兄这篇文章很好的谈论了由于数据隐式转换造成执行计划不准确,从而造成了死锁.那如果在事情出现之前发现了这类潜在的风险岂不是更好?     那么我们来看一个简单的例子,如代码清单1所示.   1: SELECT * 2: FROM HumanResources.Employee 3: WHERE NationalIDNumber = 243322160 4:  5: SELECT * 6: FR

【转】SQL SERVER标量表达式的隐式转换

在SQL Server中的数据类型中,存在着优先级的问题.标量表达示的返回结果类型也会根据操作数的类型而定,如1 +'1'=2.而不是'11',因些Int型的优先级比VARCHAR型的优先级要高.所以在表达示的结果隐式转换成Int型. 同样对于标量函数,如一个表的一列是Int型,表中有两行值为2和3如果对这一列使用AVG函数,则结果是2,而不是2.5.但是如果这一列是Decimal类型的话,那么结果就是2.5.因为结果类型依据操作数据类型. 如下的一个Case语名 CASE WHEN <logi

SQL Server有意思的数据类型隐式转换问题

写这篇文章的时候,还真不知道如何取名,也不知道这个该如何将其归类.这个是同事遇到的一个案例,案例比较复杂,这里抽丝剥茧,仅仅构造一个简单的案例来展现一下这个问题.我们先构造测试数据,如下所示: CREATE TABLE TEST (   ID    INT,   GOOD_TYPE  VARCHAR(12),   GOOD_WEIGHT NUMERIC(18,2) )   INSERT INTO dbo.TEST VALUES( 1, 'T1',1.27) SELECT  GOOD_TYPE,

oracle 表字段类型,与业务SQL不合理,导致的隐式转换

今天遇到一个生产问题,业务SQL很简单,单表查询,而且表只有三个字段,有个主键ID,而且通过主键ID过滤,业务页面会传一百多个ID过来调用SQL,这个表数据量大小为100多万,但是偏偏这条SQL执行跑了15秒,完全影响业务不能使用. select a,b,c from t where t.id in (1111,222,333,444,555..........) 我一开始并没有去查看表设计,而是直接看了执行计划, 1 alter session set statistics_level=all

Scala 系列(十三)—— 隐式转换和隐式参数

一.隐式转换 1.1 使用隐式转换 隐式转换指的是以implicit关键字声明带有单个参数的转换函数,它将值从一种类型转换为另一种类型,以便使用之前类型所没有的功能.示例如下: // 普通人 class Person(val name: String) // 雷神 class Thor(val name: String) { // 正常情况下只有雷神才能举起雷神之锤 def hammer(): Unit = { println(name + "举起雷神之锤") } } object T

隐式转换

发现某一条语句消耗较高,执行比较频繁数据库版本如下将TextData语句拷贝到查询窗口执行将sp_executesql中的主体语句拷贝到查询窗口执行执行计划的总体流向是一致的,根据token得到LKLoginTokenRecord,然后嵌套循环AccountsInfoSimple.但是sp_executesql语句的执行计划有常量扫描和计算标量的操作,并且在索引查找中有谓词CONVERT_IMPLICIT(nvarchar(32),[DBname].[dbo].[LKLoginTokenReco

关于MySQL隐式转换

一.如果表定义的是varchar字段,传入的是数字,则会发生隐式转换. 1.表DDL 2.传int的sql 3.传字符串的sql 仔细看下表结构,rid的字段类型: 而用户传入的是int,这里会有一个隐式转换的问题,隐式转换会导致全表扫描. 把输入改成字符串类型,执行计划如下,这样就会很快了. 此外,还需要注意的是: 数字类型的0001等价于1 字符串的0001和1不等价 二.如果表定义的是int字段,传入的是字符串,在不超过int范围内,不会发生隐式转换,如果超出范围并且比较大小(以字符串类型