MySQL的优化(七)2011-10-04三十一、事务的例子MyIASM表如何进行事务处理:mysql> LOCK TABLES trans READ, customer WRITE;mysql> select sum(value) from trans where customer_id=some_id;mysql> update customer set total_value=sum_from_previous_statementwhere customer_id=some_id;mysql> UNLOCK TABLES;BDB表如何进行事务:mysql> BEGIN WORK;mysql> select sum(value) from trans where customer_id=some_id;mysql> update customer set total_value=sum_from_previous_statementwhere customer_id=some_id;mysql> COMMIT;注意你可以通过下列语句回避事务:UPDATE customer SET value=value+new_value WHERE customer_id=some_id;三十二、使用REPLACE的例子REPLACE的功能极像INSERT,除了如果一条老记录在一个唯一索引上具有与新纪录相同的值,那么老记 录在新纪录插入前则被删除。不使用 SELECT 1 FROM t1 WHERE key=#IF found-rowLOCK TABLES t1DELETE FROM t1 WHERE key1=#INSERT INTO t1 VALUES (...)UNLOCK TABLES t1;ENDIF而用REPLACE INTO t1 VALUES (...)