0

When updating date/time fields in a file using embedded SQL in an RPGLE program I can use either CURRENT_DATE/CURRENT_TIME or store the current date/time value into a host variable. And use this host for assignign.

Now I wonder which way is the faster one? Or is this irrelevant?

exec sql
  update testpf
  set t1date = CURRENT_DATE, t1time = CURRENT_TIME
  where t1key = someValue;

or

dcl-s date date;
dcl-s time time;
exec sql
  set :date = CURRENT_DATE;
exec sql
  set :time = CURRENT_TIME;
exec sql
  update testpf
  set t1date = :date, t1time = :time
  where t1key = someValue;

Note: This is all written "on the fly"! But I hope you get what I mean

Edit: To clarify, the goal is not to update just one row, its ment for several updates. Like having a database with invoice positions and a state field. This state field has 3 neigbour fields which track the user changing it on which day at which time. And in my case there can be several hundrets of positions where I need to update the time and date.

Radinator
  • 1,048
  • 18
  • 58

4 Answers4

1

If you are going to use the CURRENT_DATE and CURRENT_TIME registers, I expect it would be best to just use them in the SQL statement that needs them. Your second example involves three requests to the database, while the first example only involves one. So I suspect the first would perform better as all three have the same (get this register) overhead, but the second has call overhead three times vs. one time.

Plus the first is just way easier to read (the most important consideration, IMO, unless it's not). That is, there is no need to optimize if you don't really need to. Of course, if I were to desire to retrieve the current date and time in RPG, I wouldn't use SQL to do it, but an RPG built-in like %date() or %time() ;-)

Edit: There are some non-performance considerations here. If you want the same date/time on all rows, across all updates, you will have to capture the date and time ahead of time. If you want the actual date and time of the update, you will want to use the registers. One nice thing that SQL does though is that if you use CURRENT_DATE, CURRENT_TIME, or CURRENT_TIMESTAMP or some mixture of these multiple times in a single SQL statement, all rows updated will have the same date, time, and timestamp for a given execution of the statement.

jmarkmurphy
  • 11,030
  • 31
  • 59
1

My two cents. Are you aware of the row change timestamp column on IBM i? They are described in https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/sqlp/rbafysqlprcts.htm?

They can save you a lot of coding by keeping an automatically maintained timestamp column each time a row is inserted or updated. On the other hand, it is only supported for timestamp columns, not for separated date and time columns.

Daniel Lema
  • 324
  • 2
  • 9
1

I think your question have no a simple answer, infact if you search info about host variable, also IBM doesn't give you a quick answer, look here:

Host variables require default filter factors. When you bind a static SQL statement that contains host variables, Db2 uses a default filter factor to determine the best access path for the SQL statement. Db2 often chooses an access path that performs well for a query with several host variables. However, in a new release or after maintenance has been applied, Db2 might choose a new access path that does not perform as well as the old access path. In many cases, the change in access paths is due to the default filter factors, which might lead Db2 to optimize the query in a different way.

Nifriz
  • 1,033
  • 1
  • 10
  • 21
1

Personally I'd use the first option. The code is simpler and I can't imagine there would be an significant performance difference.

The only time I'd go with the second option is if the date might sometimes need to be something other than the system date (e.g. UDATE).

jtaylor___
  • 609
  • 1
  • 6
  • 14