简体   繁体   English

RPGLE中嵌入式SQL在Update中的性能

[英]Embedded SQL in RPGLE performance in Update

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. 在RPGLE程序中使用嵌入式SQL更新文件中的日期/时间字段时,我可以使用CURRENT_DATE / CURRENT_TIME或将当前日期/时间值存储到主机变量中。 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. 该状态字段具有3个neigbour字段,这些字段跟踪用户在哪一天的哪个时间对其进行更改。 And in my case there can be several hundrets of positions where I need to update the time and date. 就我而言,可能有几百个位置需要更新时间和日期。

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. 如果要使用CURRENT_DATECURRENT_TIME寄存器,我希望最好只在需要它们的SQL语句中使用它们。 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). 另外,第一个只是更易于阅读(最重要的考虑因素,IMO,除非并非如此)。 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() ;-) 当然,如果我想在RPG中检索当前日期和时间,我不会使用SQL来执行,而是使用内置的RPG,如%date()%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. SQL要做的一件好事是,如果您在单个SQL语句中使用CURRENT_DATECURRENT_TIMECURRENT_TIMESTAMP或这些多次的某种混合,则对于给定的执行时间,所有更新的行将具有相同的日期,时间和时间戳。声明。

My two cents. 我的两分钱。 Are you aware of the row change timestamp column on IBM i? 您是否知道IBM i上的行更改时间戳记列? They are described in https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_73/sqlp/rbafysqlprcts.htm ? https://www.ibm.com/support/knowledgecenter/zh-CN/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. 另一方面,仅时间戳列支持它,不支持分开的日期和时间列。

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: 我认为您的问题没有简单的答案,实际上,如果您搜索有关主机变量的信息,IBM也会给您一个快速的答案,请看这里:

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. 当您绑定包含主机变量的静态SQL语句时,Db2使用默认的筛选因子来确定SQL语句的最佳访问路径。 Db2 often chooses an access path that performs well for a query with several host variables. Db2经常选择对带有多个主机变量的查询执行效果良好的访问路径。 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. 但是,在新版本中或在应用维护后,Db2可能会选择一个性能不如旧访问路径的新访问路径。 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. 在许多情况下,访问路径的更改归因于默认过滤器因素,这可能导致Db2以不同的方式优化查询。

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 (eg UDATE). 我唯一选择的第二个选择是日期有时可能不是系统日期(例如UDATE)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM