简体   繁体   中英

150 fields Update - PHP + MYsql Best Solution

I am reading one XML field which has 150 tags ( fields ) in it.My XML field has the foll fields

productid, name and soon... 150 fields

The feeds shows me the data which is EITHER a NEW record or the DATA which is updated.

I know how to read, insert, update data in PHP/Mysql.

My main concern is that when there is an UPDATE, do i have to write UPDATE statement for all 150 fields ? Is there any shortcut to UPDATE only the fields that has been changed ?

Is it the STANDARD METHOD to write UPDATE name SET name = name, WHERE ... for ALL 150 fields ?

Or is there any SMART solution for doing this, because i dont think its best solution to update all fields ( if anyone of the field is updated )

You can update all 150 fields with just one query.

UPDATE tbl SET field1 = val1, field2 = val2, ..., fieldn = valn WHERE condition

It sounds like you're concerned about whether it's feasible to update all 150 fields in one query, and I don't see an issue with that. Alternatively, you would have to first select the tuple from the database, then compare each of the 150 fields, and update the database. That seems like a lot more work than just updating everything. That's two queries and a lot of comparisons instead of just one query.

If I understand correctly your mysql scheme has 150 columns. Some database models (such as Zend ), allows you to do this:

// This is just pseudocode!
$object = $db->fetch( ...); // $object is object supporting method save()
foreach( $xmlArray as $key => $value){
  if( $object->hasProperty( $key)){
    $object->$key = $value;
  } else {
    throw ...
  }
}
$object->save();

// And internally:
function save() {
  $updateData = array();
  foreach( $this->newData as $key => $val){
    if( $this->oldData[$key] !== $val){
       $updateData[$key] = $val;
    }
  }

  if( !count( $updateData)){
     return false;
  }

  $sql = $this->updateStatement( $this->table, $updateData, 'WHERE ' . $this->primaryKey .
         ' = ' . $this->oldData[$this->primaryKey]);
}

Anyway, this is useful just when you have troubles with maximum mysql packet size. Mysql doesn't change columns which doesn't need to be changed. Not even affected rows is increased when no data is changed.

I guess that you have data already in some kind of array when you load them from xml and you decide whether to add them or update them. And you can do this:

$updateData = $addData;
unset( $updateData['createDate']); // any field that you want to change
$db->update( 'tableName', $updateData, 'condition');

Doing this without database model, just by generating query is bad solution. And (when using InnoDb) putting all updates/inserts into TRANSACTION will make your application much faster than any attempt to decrease package size (I assume you're not using large amount of data such as image, or binary files, 20KB texts and so on), because db engine will update all indexes at once.

If I understand your question, the query you're looking for is:

UPDATE `table_name` SET `column1` = [value for column1] WHERE [your description of which records to change];

To update just one field in any given row.

If you wanted to update, say, columns 1, 50, 123, and 145 (I'm just giving them numbers because I don't know the names of course, then you just include those in your query as so:

UPDATE `table_name` SET `column1` = [value for column1], `column50` = [value for column50], `column123` = [value for column123], `column145` = [value for column145] WHERE [your description of which records to change];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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