SQL大小写和where条件与表的顺序

1.
SQL的大小写要看数据库类型, 如果数据库是Oracle, 则答案是B
SQL Server中, 可以手工设置数据库是否区分大小写, 所以SQL大小写对解析没有什么影响, 但是要注意的是, 一定要统一表名, SQL语句等等的格式, 统一大写或小写.
 
Oracle则不一样, SQL设置为统一为大写时性能更优, 虽然比小写时性能高不了多少.
以下内容摘自Oracle官方的数据库调优手册:
Shared Pool Concepts目录下的Library Cache Concepts目录下的SQL Sharing Criteria中写到:
Many Oracle products, such as Oracle Forms and the precompilers, convert the
SQL before passing statements to the database. Characters are uniformly changed
to uppercase, white space is compressed, and bind variables are renamed so that a
consistent set of SQL statements is produced.
 
Accepting a SQL Profile 目录下写道: 
Typically, an accepted SQL Profile is associated with the SQL statement through a
special SQL signature that is generated using a hash function. This hash function
normalizes the SQL statement for case (changes the entire SQL statement to upper
case) and white spaces (removes all extra whites spaces) before generating the
signature.
 
以上两处提到系统强制转换SQL为大写, 所以如果sql写成小写, 则会有额外的开销
 
其他类型数据库我没有去考证, 如果有相关资料请告知
 
2.
答案也是要看条件, 如果在A,B,C表中数据量相当, 则答案是B
 
因为oracle数据库有两种优化器
RBO - 1、 基于规则的优化方式(Rule-Based Optimization,简称为RBO) 优化器 
CBO - 1、 基于代价的优化方式(Cost-Based Optimization,简称为CBO) 优化器
如果是RBO优化器, ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最先处理.
如果是CBO优化器, ORACLE则优先处理输出数据量少的表.
posted @ 2013-08-13 21:58  dushuai  阅读(803)  评论(0编辑  收藏  举报