设为首页 加入收藏

TOP

从Java的类型转换看MySQL和Oracle中的隐式转换(二)
2015-11-10 12:16:43 来源: 作者: 【 】 浏览:4
Tags:Java 类型 转换 MySQL Oracle
'))
可以看到谓词信息已经发生了变化。? 2 - access("ID1"=TO_NUMBER('A'))从这个地方我们可以看到确实触发了一个to_number的操作。
?而优化器在这个时候虽然触发了,但是在sql运行的时候,就会报出错误,这个时候可以看到Oracle还是蛮严谨的。
SQL> select *from test where id1='A';
?select *from test where id1='A'
? ? ? ? ? ? ? ? ? ? ? ? ? ? *
?ERROR at line 1:
?ORA-01722: invalid number
而如果使用双引号,生成执行计划都会抛错。
SQL> explain plan for select *from test where id1="A";
explain plan for select *from test where id1="A"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *
ERROR at line 1:
ORA-00904: "A": invalid identifier
可见单引号和双引号在Oracle代表的含义还是有很大差别。


?我们来看看在MySQL中的表现。
?还是创建一个简单的表,插入一些数据。
> create table test (id1 int,id2 varchar(10));
?> insert into test values(1,'1');
?> insert into test values(2,'2');
?> insert into test values(3,'3');
?> commit;
?> create index idx_id1 on test(id1);
?> create index idx_id2 on test(id2);
这个时候生成执行计划,可以发现走了索引
> explain select * from test where id1='1';
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?| id | select_type | table | type | possible_keys | key? ? | key_len | ref? | rows | Extra? ? ? |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?|? 1 | SIMPLE? ? ? | test? | ref? | idx_id1? ? ? | idx_id1 | 5? ? ? | const |? ? 1 | Using where |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?1 row in set (0.00 sec)
而如果查看id1为varchar的类型时,也走了索引。
> explain select * from test where id1='a';
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?| id | select_type | table | type | possible_keys | key? ? | key_len | ref? | rows | Extra? ? ? |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?|? 1 | SIMPLE? ? ? | test? | ref? | idx_id1? ? ? | idx_id1 | 5? ? ? | const |? ? 1 | Using where |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?1 row in set (0.00 sec)
差别更大的就是如果使用id1='a',也能够正常执行,只是没有任何匹配的记录。
> select * from test where id1='a';
?Empty set (0.00 sec)
而如果由单引号改为双引号,也能够正常运行。
> select * from test where id1="a";
?Empty set (0.00 sec)
而且双引号的情况下,生成执行计划也没有问题。
> explain select * from test where id1="a";
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?| id | select_type | table | type | possible_keys | key? ? | key_len | ref? | rows | Extra? ? ? |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?|? 1 | SIMPLE? ? ? | test? | ref? | idx_id1? ? ? | idx_id1 | 5? ? ? | const |? ? 1 | Using where |
?+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
?1 row in set (0.00 sec)
可以看出在MySQL中这个时候的范围似乎更宽,在MySQL中不光用单引号,双引号,而且还经常会看到·这种符号。
?这种在MySQL中可以灵活声明一些变化???,举个不太恰当的例子,比如我们创建一个表,一个字段为int,类型为int直接按照下面的方式来写,肯定抛错。
> create table test1(int int);
?ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'int int)' at line 1
?crea' at line 1
可以加上·,就可以识别了。
> create table test1(`int` int);
?Query OK, 0 rows affected (0.00 sec)


这个对比的跨度有点大,但是通过一些小把戏似乎还是能够看出在这些类型的转换中,优化器这边的触发情况。再接再厉,继续探究。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Oracle 还原库 下一篇MySQL服务器时间同步问题处理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: