我想知道在更新记录以检索现有记录、循环遍历字段检查更改并仅将更改的字段放入更新查询中是否值得花费服务器时间? (我正在使用 MySQL 和 PHP。)
这样做的主要原因是为了减少更改日志的更新查询的大小。通常查询可能有 15 个字段,但实际上只有 2 个字段被更改。该查询还可以用于记录日志,因为它只包含更改的字段,因此更容易解析。
我关心的是检索现有记录所需的时间。
或者有没有办法从 MySQL 检索它更新了哪些字段?
我认为值得改变 - 但可能不值得在插入之前进行选择。
我只更新已更改的字段,这是遵循 activerecord 模式的 DbEntity 类操作的一部分。执行此操作几乎不需要额外费用,因为我保存当前记录和原始记录 - 只要加载记录即可复制。
原因很简单——并不是真正的性能。您还可以通过在更新字段的旧值上添加 where 子句来检查并发修改,并抛出相应的错误。
在写入/更新方法中:
$s1 = "";
foreach ($this->record as $key => $value)
{
// only update fields that have been changed
if ($value != $this->orig_record[$key])
{
$s1 .= $comma."`$key`='".mysql_real_escape_string($value)."'";
$comma = ", ";
}
}
$query = "UPDATE ".$this->table." SET $s1 where {$this->id_field}='".$this->get_keyfield()."'";
$query .= $this->extra_sql_update;
mysql_query($query);
$ar = mysql_affected_rows();
//
// the number of affected rows is actually those changed by the update operation, which will
// either be zero, or 1. If the query affects more than one row then we have a problem.
if ($ar < 0 || $ar > 1)
{
cbf_error("cbf_dbentity: {$this->table} :: only one row (not $ar) must be affected by an insert operation. $query",
E_USER_ERROR);
}
else
{
$new_id = $this->get_keyfield();
GlobalEventBus::notify_all(new AuditLogSQL($this->table, "update", $query));
}
$this->orig_record = Array();
foreach ($this->record as $key => $value)
$this->orig_record[$key] = $value;
//
// sanity check - ensure that what we have just written is actually there.
$this->load($new_id);
foreach ($this->orig_record as $key => $value)
if (trim($this->record[$key]) != trim($value)
&& (!$this->record[$key] == "0" && $value=""))
cbf_error("cbf_dbentity: {$this->table} :: record differs during write after reload: field $key was \"$value\", after write it is now \"".
$this->record[$key]."\"",E_USER_ERROR);
在加载方法中
$this->orig_record = Array();
foreach ($this->record as $key => $value)
$this->orig_record[$key] = $value;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)