Oracle数据库的表的增、删、改SQL操作方法(二)

1. select count( )语句的优化

        一般都是直接这样写,select count(*) from tablename t,最近数据库比较忙,这样写执行速度非常慢。

        这样写就会非常快:count(主健或者其他)

select count(t.key) from tablename t

2. in、exists、not in 、not exists的区别

2.1   exists是用loop的方式,所以,循环的次数对于exists影响大:外表记录数越少效率越高,内表就无所谓;而in用的是hash join,内表越小效率越高,若内表和外表都很大,in方式效率就会很低。

2.2   not exists查询,外表存在空值,存在空值的那条记录最终会输出,内表存在空值对查询结果没有影响。

2.3   not in查询,外表存在空值,存在空值的那条记录最终将被过滤,其他数据不受影响;内表存在空值将导致最终的查询结果为空,。

2.4   not in 查询,内外表都进行全表扫描,没有用到索引; 而not extsts 的子查询依然能用到表上的索引

        一般来说,not in语句比not exists语句效率差很多,尽量用not exists就好了。 not exists语句是一个简单的两表关联,内表与外表中存在空值本身就不参与关联,在CBO(基于成本的优化器)中常用的执行计划是hash join,所以它的效率很好。

        in和exists需要具体情况具体分析

3.查看某字段在数据库中的内码

select dump(t.col) from table t

---以十六进制显示内码

select t.col,dump(t.col,16) from table t

4. 两表联合查询之count() 、group by 用法

        两个表A和B,查询表B中与A有关联的数据,并统计每一条关联的总数

select count(substr(t.gbcode,0,12)),substr(t.gbcode,0,12)
       from D_REGION_WG t, D_REGION_COMMUNITY b
       where b.code= substr(t.gbcode,0,12) and t.lv='6'  group by substr(t.gbcode,0,12)

5.两边联合查询之order by asc(升序)、desc(降序)用法

-----升序
select b.col,a.col from D_REGION_WG a,D_REGION_COMMUNITY b 
       where b.code=substr(a.gbcode,0,12) and a.lv='6' order by to_number(b.id) asc


-----降序
select b.col,a.col from D_REGION_COUNT_WG a,D_REGION_COMMUNITY b 
       where a.com_code=b.code order by to_number(b.id) desc

猜你喜欢

转载自blog.csdn.net/aganliang/article/details/81358664
今日推荐