如何优化这一查询!!!
有如下两张表 表结构如下:(数据量都比较大 都当有1千万把)
tab1
-------------------------------------------------
id card name type intime
1 1001 jacke a100 20050401
2 1001 jackie a100 20050401
3 1002 anti a200 20050401
4 1002 anti a200 20050401
5 1003 wack a300 20050401
6 1004 waee a400 20050402
7 00 tt a500 20050401
8 00 tt a500 20050401
.. .. .. .. ........
--------------------------------------------
tab2
--------------------------------------------------
type typename other1 other2
a100 import test1 test2
a200 import test3 test4
a300 noimport test5 test6
a400 noimport test7 test8
a500 import test9 test10
.. .. .. ...
--------------------------------
现在要做如下查询 在Tab1 中card 出现次数大于1的记录并且时间字段(intime)为指定一具体值(比如20060501),需要显示 card ,name ,type ,intime,typename, other1,other2 这些字段 两张表的关联是 type 这一字段 card 的长度小于4位 不予理睬 不做显示!由于数据两表数据比较大 我做的查询速度慢!我只这么做的
先建立一张试图(view1)
select card ,name ,type intime,typename from tabl where card in(select card from tab1 where intime='20050401' group by card having count(card)>1) and intime='20050401'
然后 view1 有 tab2 在做关联(我做的暂不考虑card的长度) 因为速度太慢 希望大家给我一点建议!!不甚感激!!!
按照要求查询结果应该如下:
1001 jacke a100 20050401 import test1 test2
1001 jacke a100 20050401 import test3 test4
1002 anti a200 20050401 noimport test5 test6
1002 anti a200 20050401 noimport test7 test8
============================================================================
问题点数:20、回复次数:1Top
1 楼xiaoxi_LION(小溪 世界上只有一种东西不会因为你的付出太多而变少,那就是--爱!!!!)回复于 2006-05-05 17:15:34 得分 20
我个人的一点建议:
1.保证tab1上的type,intime,tab2 的type 上均建有索引,且确保你的查询的执行计划先按intime进行索引,这样在你按intime查询时,速度应该会有很大的提升;
2.可以把in查询改为: exists
Top




