sql执行效率优化


$sql = "INSERT INTO dc_temp.{$this->tableName}
(userId,lossLoginDay,lossVouAmount)
(SELECT
userId,loginDay,vouchAmountTotal
FROM server_user_{$this->where['productId']}
WHERE userId IN (
SELECT userId FROM dc_temp.{$this->tableName} GROUP BY userId
)
) ON DUPLICATE KEY UPDATE
lossLoginDay= IF(lossLoginDay = 0, VALUES(lossLoginDay), lossLoginDay),
lossVouAmount = IF(lossVouAmount = 0.00, VALUES(lossVouAmount), lossVouAmount)";
这个sql主要是更新表{$this->tableName}中的lossLoginDay、lossVouAmount值,当这2个表数据量很大时执行速度非常之慢,而且十分占资源!同志们帮忙给优化一下

mysql 性能

hicooy 12 years, 9 months ago

如果可以拆分比较好吧,把SELECT userId FROM dc_temp.{$this->tableName} GROUP BY userId先检索出来,应该尽量减少用in,如果不多,应该可以用or之类的吧,外面的一层SELECT

   
  userId,loginDay,vouchAmountTotal应该也可以拆开来吧
 

断桥丶残雪 answered 12 years, 9 months ago

Your Answer