我从主机数据库中提取了一个Calendar表,并保存为本地Access表。该表的历史可以追溯到20世纪30年代(我知道我们至少在一个地方使用了50年代),产生了31k的记录。此Calendar表有3个感兴趣的字段:
Bus_Dt - 每天,而不仅仅是工作日。主键
Bus_Day_Ind - 表示当天是否是股票市场的有效营业日
Prir_Bus_Dt - 前一个工作日。包含一些错误(大约50个),都是旧的。
我已经编写了一个查询来检索当前日历日或之后的第一个工作日,但它的运行速度非常慢。 (5分钟以上)我检查了showplan输出并看到它是通过x-join运行的,它在30k +记录表之间提供了大约1000万的解决方案空间(和日期比较)。然而,实际任务并不难,并且可以通过简单的排序在最短的时间内通过excel轻松完成。
我的问题是,有没有办法解决查询的不良性能,或者这是SQL固有的失败? (在大型机上运行的DB2也很慢,虽然不是那么严重。抛出问题的循环和所有这些。)其次,如果我相信prir_bus_dt,我可以更好地到达那里吗?或者限制日期范围(又名,"作弊"),或者我还没有想到的任何其他技巧?
SQL:
SELECT TE2Clndr.BUS_DT AS Cal_Dt
, Min(TE2Clndr_1.BUS_DT) AS Next_Bus_Dt
FROM TE2Clndr
, TE2Clndr AS TE2Clndr_1
WHERE TE2Clndr_1.BUS_DAY_IND="Y" AND
TE2Clndr.BUS_DT<=[te2clndr_1].[bus_dt]
GROUP BY TE2Clndr.BUS_DT;
显示计划:
Inputs to Query
Table 'TE2Clndr'
Table 'TE2Clndr'
End inputs to Query
01) Restrict rows of table TE2Clndr
by scanning
testing expression "TE2Clndr_1.BUS_DAY_IND="Y""
store result in temporary table
02) Inner Join table 'TE2Clndr' to result of '01)'
using X-Prod join
then test expression "TE2Clndr.BUS_DT<=[te2clndr_1].[bus_dt]"
03) Group result of '02)'
同样,问题是,这可以变得更好(更快),还是已经变得更好了?
答案 0 :(得分:0)
I have a new query that is much faster for the same job, but it depends on the prir_bus_dt field (which has some errors). It also isn't great theory since prior business day is not necessarily available on everyone's calendar. So I don't consider this "the" answer, merely an answer.
New query:
After 1 seconds, temperature is 69.910000
After 2 seconds, temperature is 69.820090
After 3 seconds, temperature is 69.730270
After 4 seconds, temperature is 69.640540
After 5 seconds, temperature is 69.550899
After 6 seconds, temperature is 69.461348
After 7 seconds, temperature is 69.371887
After 8 seconds, temperature is 69.282515
After 9 seconds, temperature is 69.193232
After 10 seconds, temperature is 69.104039
After 11 seconds, temperature is 69.014935
After 12 seconds, temperature is 68.925920
After 13 seconds, temperature is 68.836994
After 14 seconds, temperature is 68.748157
After 15 seconds, temperature is 68.659409
After 16 seconds, temperature is 68.570750
After 17 seconds, temperature is 68.482179
After 18 seconds, temperature is 68.393697
After 19 seconds, temperature is 68.305303
After 20 seconds, temperature is 68.216998
After 21 seconds, temperature is 68.128781
After 22 seconds, temperature is 68.040652
After 23 seconds, temperature is 67.952611
After 24 seconds, temperature is 67.864659
After 25 seconds, temperature is 67.776794
After 26 seconds, temperature is 67.689017
After 27 seconds, temperature is 67.601328
After 28 seconds, temperature is 67.513727
After 29 seconds, temperature is 67.426213
After 30 seconds, temperature is 67.338787
After 31 seconds, temperature is 67.251448
After 32 seconds, temperature is 67.164197
After 33 seconds, temperature is 67.077033
After 34 seconds, temperature is 66.989956
After 35 seconds, temperature is 66.902966
After 36 seconds, temperature is 66.816063
After 37 seconds, temperature is 66.729247
After 38 seconds, temperature is 66.642517
After 39 seconds, temperature is 66.555875
After 40 seconds, temperature is 66.469319
After 41 seconds, temperature is 66.382850
After 42 seconds, temperature is 66.296467
After 43 seconds, temperature is 66.210170
After 44 seconds, temperature is 66.123960
After 45 seconds, temperature is 66.037836
After 46 seconds, temperature is 65.951798
After 47 seconds, temperature is 65.865847
After 48 seconds, temperature is 65.779981
After 49 seconds, temperature is 65.694201
After 50 seconds, temperature is 65.608507
After 51 seconds, temperature is 65.522898
After 52 seconds, temperature is 65.437375
After 53 seconds, temperature is 65.351938
After 54 seconds, temperature is 65.266586
After 55 seconds, temperature is 65.181319
After 56 seconds, temperature is 65.096138
After 57 seconds, temperature is 65.011042
After 58 seconds, temperature is 64.926031
After 59 seconds, temperature is 64.841105
After 60 seconds, temperature is 64.756264
64.75626360008506
答案 1 :(得分:-1)
这种方法怎么样
select min(bus_dt)
from te2Clndr
where bus_dt >= date()
and bus_day_ind = 'Y'
This是我对代表当前日期的date()
的引用