日期:2014-05-16  浏览次数:20954 次

效率问题,应该不难
有两个表 ,tab1,tab2(tab1数据量比tab2数据量大) 现在统计tab1表里面有tab2表里面数据的条数。请问各位有什么非常快的查询方法(每个表里面数据都是1000万以上),我现在又下面三种方法,你们给个建议,还有最好能给我提供更加快速的方法。谢谢……


select count(*) from tab1 inner join (select id from tab2 where months_between(sysdate,tab2.birthday)/12>65)e
on tab1.id=e.id;

select count(*) from tab1 eah inner join tab2 on tab1.id=tab2.id where months_between(sysdate,tab2.birthday)/12>65;

select count(*) from tab1 where EXISTS (select 1 from tab2 where tab1.id=tab2.id and months_between(sysdate,tab2.birthday)/12>65 );
 

------解决方案--------------------
SELECT COUNT(1)-- COUNT(*)个人习惯是count(1)或者具体的count(fieldName)
FROM tab1
INNER JOIN
(SELECT id FROM tab2 
WHERE months_between(sysdate,tab2.birthday)>65*12
--比较符左边尽量不使用计算
--WHERE months_between(sysdate,tab2.birthday)/12>65
)e
ON tab1.id=e.id;
------解决方案--------------------
探讨
如果tab2.birthday有索引的话,这样用months_between(sysdate,tab2.birthday)/12>65的话,可能会用不到索引,影响查询速度。

------解决方案--------------------
探讨
有两个表 ,tab1,tab2(tab1数据量比tab2数据量大) 现在统计tab1表里面有tab2表里面数据的条数。请问各位有什么非常快的查询方法(每个表里面数据都是1000万以上),我现在又下面三种方法,你们给个建议,还有最好能给我提供更加快速的方法。谢谢……

select count(*) from tab1 inner join (select id from tab2 where months_between(sysdate,tab2.birthday)/12>65)e
on tab1.id=e.id;

select count(*) from tab1 eah inner join tab2 on tab1.id=tab2.id where months_between(sysdate,tab2.birthday)/12>65;

select count(*) from tab1 where EXISTS (select 1 from tab2 where tab1.id=tab2.id and months_between(sysdate,tab2.birthday)/12>65 );