要我贴语句吗?好吧,下面这个是我3年前在一个程序中写的,其中的“##”是要用日期替换的,
但是我认为,即使贴出来也没有什么太大的意思,因为,还需要花更多的功夫解释表结构的设计
才能让各位能看明白,而我只是想知道,如何能计算出这些sql语句的效率(只要理论就行)。
这个sql语句在vb+access中可以运行,其中data表(主要数据表,还有些其它的辅助表),数据量
在200条记录以下,大约可以在5~10秒内完成(PⅡ233+win98+vb5+access97),而当数据量超过
500条时,大约要将近2分钟才能完成,更多就更加不得了。我这个人有个小毛病,总是喜欢用一个
sql语句尽可能地把一个要填入界面grid的所有数据查询出来,所以,就产生了下面这个效率低下
的庞然大物,而现在我想,如果把其中的子查询拆出来,执行,可能效率会提高不少。
SELECT zb.bh AS id,zb.ssd,(select a2.data from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='8' and Year(a2.date)=Year(##)-0 AND Month(a2.date)=Month(##)) AS Dnow8,(select a2.data from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='7' and Year(a2.date)=Year(##)-0 AND Month(a2.date)=Month(##)) AS Dnow7,(select a2.data from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-0 AND Month(a2.date)=Month(##)) AS Dnow0,Switch(zb.ssd='PJS',(select sum(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-0 AND Month(a2.date)<=Month(##))/iif((select count(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-0 AND Month(a2.date)<=Month(##))=0,1,(select count(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-0 AND Month(a2.date)<=Month(##))),zb.ssd='SQS',(select sum(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-0 AND Month(a2.date)<=Month(##)),zb.ssd='SDS','') AS p0J,(select a2.data from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-1 AND Month(a2.date)=Month(##)) AS Dnow0Q,Switch(zb.ssd='PJS',(select sum(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-1 AND Month(a2.date)<=Month(##))/iif((select count(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-1 AND Month(a2.date)<=Month(##))=0,1,(select count(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-1 AND Month(a2.date)<=Month(##))),zb.ssd='SQS',(select sum(a2.data) from data a2,zb_private zb1 where a2.zdbh=a1.zdbh and zb1.bh like a2.zdbh+'*' and a2.data<>0 and a2.zdlb='0' and Year(a2.date)=Year(##)-1 AND Month(a2.date)<=Month(##)),zb.ssd='SDS','') AS p0Q FROM data AS a1 Right Join zb_private AS zb On zb.bh like a1.zdbh+'*' WHERE zb.bh Like 'U2*' and val(mid(zb.bh,instr(instr(instr(zb.bh,'.')+1,zb.bh,'.')+1,zb.bh,'.')+1))<>'0' GROUP BY a1.zdbh,zb.bh, zb.ssd, zb.bh ORDER BY val(mid(zb.bh,instr(instr(instr(zb.bh,'.')+1,zb.bh,'.')+1,zb.bh,'.')+1));
而最近,我在编程时又遇到类似的问题,所以才提出来的,想听听大家有什么看法。
多谢wukw的夸奖,愧不敢当。