看了几个面试题目:
有员工表empinfo
(
Fempno varchar2(10) not null pk,
Fempname varchar2(20) not null,
Fage number not null,
Fsalary number not null
);
假如数据量很大约1000万条;写一个你认为最高效的SQL,用一个SQL计算以下四种人:
fsalary>9999 and fage > 35
fsalary>9999 and fage < 35
fsalary<9999 and fage > 35
fsalary<9999 and fage < 35
每种员工的数量;
我用10万条数据测试,这样写的:
SQL> select count(*) from empinfo;
COUNT(*)
----------
100000
已用时间: 00: 00: 00.01
SQL> with total as (
2 select * from empinfo
3 )
4 select (select count(*) from total where fsalary>9999 and fage>35) t1,
5 (select count(*) from total where fsalary>9999 and fage<35) t2,
6 (select count(*) from total where fsalary<9999 and fage>35) t3,
7 (select count(*) from total where fsalary<9999 and fage<35) t4
8 from dual ;
T1 T2 T3 T4
---------- ---------- ---------- ----------
89966 33 9997 2
已用时间: 00: 00: 00.06
统计信息
----------------------------------------------------------
13 recursive calls
0 db block gets
2996 consistent gets
0 physical reads
0 redo size
570 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
不知道大家有没有其他的方法。讨论讨论啦。。
阅读(1528) | 评论(0) | 转发(0) |