We are using DB2 in our company and we have a scheduling program (which I don't have access to) which runs set of SQL files every 5 mins. I have the control on the SQL statements though

one of the SQL within a file needs to run only twice a day and its really heavy one. So running it every 5 mins is killing the system.

Hence my question/situation is,

  • Is there a clever way to modify the SQL so that it checks if its within a prescribed time period , then run it. (i.e. Do not execute the logic, if its outside the specific time)?
  • I'm not able to put script or if loop as its a pure SQL. So amendment needs to be within the Data Manipulation Language (DML) statements
  • If I simply add a hard-coded time parameter, will DB2 execute the logic and then do the time check. Else it would have been very simple to put a hard-coded between time parameter into the SQL.

Thanks in advance

有帮助吗?

解决方案

Try something like this. It gets the current timestamp first, and cross-joins your query to it. The optimizer will be smart enough to see that "A" doesn't have any rows, and won't run "B".

SELECT B.*
FROM (SELECT CURRENT TIME AS CTIME FROM SYSIBM.SYSDUMMY1) A
JOIN (...your_query...) B
  ON 1=1
WHERE A.CTIME BETWEEN '08:00:00' AND '09:00:00'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top