使用PHP生成器批量更新數據
阿新 • • 發佈:2018-10-15
每次 oge 記錄 取數據 use UNC rtrim( 行修改 for 使用PDO操作數據
生成器使用
隨機暫停緩解數據庫壓力
批量更新數據
<?php header(‘Content-Type: text/html; charset=utf-8‘); // 定義數據配置信息 $host = ‘‘; $dbname = ‘‘; $user = ‘‘; $pass = ‘‘; $prefix = ‘‘; try { // 是否開啟更新執行 define(‘DEBUG‘,true); // true 執行修改SQL false 反之, $testFor = 0; $testNum = 3; // 測試循環次數 如果開啟執行 true 更新SQL,測試無效, //每次執行多少條數據 $everyRow = 1000; $start = 0; $number = 1; // 序號 //記錄日誌 $loger = function($content){ $file = ‘/tmp/sql.log‘; $date = date(‘Y-m-d H:i:s‘); file_put_contents($file, "[{$date}] | {$content}".PHP_EOL, FILE_APPEND | LOCK_EX); }; // 獲取當前時間戳,精確到毫秒 $getCurrentTime = function () { list ($msec, $sec) = explode(" ", microtime()); return (float)$msec + (float)$sec; }; // 連接數據庫 $pdo = new PDO ("mysql:host={$host};dbname={$dbname}", $user, $pass); $tableName = $prefix.‘order_info‘; // 定義獲取數據函數 $querySql = ‘‘; $selectQuery = function ($pdo, $tableName, $start, $everyRow) { global $querySql; $querySql = "SELECT order_id,order_sn,buy_from FROM {$tableName} WHERE buy_from = 0 LIMIT {$start},{$everyRow}"; $queryStmt = $pdo->prepare($querySql); $queryStmt->execute(); $result = $queryStmt->fetchAll(PDO::FETCH_ASSOC); $queryStmt->closeCursor(); return $result; }; do { // 防止開發時死循環 if (DEBUG) { ++$testFor; if ($testFor > $testNum) { break; } } // 記錄開始時間 $begin = $getCurrentTime(); if ($result = $selectQuery($pdo, $tableName, $start, $everyRow)) { // 定義生成器 $getGenerator = function ($data) { foreach ($data as $key => $value) { yield $key => $value; } }; // 遍歷生成器,實現拼接執行SQL語句 1 web UU1%,U1% 2 wap UL%,UM% 3 iOS UA%,UUA% 4 Android UB%,UUB% $generator = $getGenerator($result); $sql = ‘UPDATE ‘.$tableName.‘ SET buy_from = CASE order_id‘; $fromArray = [‘U1‘=>1,‘UU1‘=>1,‘UM‘=>2,‘UL‘=>2,‘UA‘=>3,‘UUA‘=>3,‘UB‘=>4,‘UUB‘=>4]; $orderIdStr = ‘‘; foreach ($generator as $value) { $order_sn = isset($value[‘order_sn‘]) ? trim($value[‘order_sn‘]) : false; if ($order_sn) { $prefixFrom = substr($order_sn, 0, 2); // 截取字符串 if($prefixFrom == ‘UU‘){ // 快速訂單處理 $prefixFrom = substr($order_sn, 0, 3); $fromBy = isset($fromArray[$prefixFrom]) ? $fromArray[$prefixFrom] : ‘‘; }else{ $fromBy = isset($fromArray[$prefixFrom]) ? $fromArray[$prefixFrom] : ‘‘; } if ($fromBy) { $orderId = $value[‘order_id‘]; $orderIdStr .= $orderId.‘,‘; $sql .= " WHEN {$orderId} THEN {$fromBy}"; } } } //拼接SQL,處理大數據更新 $orderIdStrWhere = rtrim($orderIdStr, ","); $sql .= ‘ END WHERE order_id IN (‘.$orderIdStrWhere.‘)‘; $count = 0; if (!DEBUG) { $stmt = $pdo->prepare($sql); $stmt->execute(); $count = $stmt->rowCount(); $stmt->closeCursor(); } $end = $getCurrentTime(); $spend = round($end - $begin,5); $memory = memory_get_usage(); // 返回的單位是b,/1024得到kb,/(1024*1024)得到mb 1024*1024 = 1048576 $logContent = ‘序號:‘.$number.‘ | 查詢SQL:‘.$querySql.‘ | 腳本執行時間為:‘.$spend.‘ 秒 | 影響行數:‘.$count.‘ | 消耗內存:‘.($memory / 1048576).‘ mb‘; $loger($logContent); // 記錄日誌 echo $logContent.‘<br/>‘; } $start += $everyRow; // 分頁記錄計數 ++$number; //序號計數 // 隨機暫停 0.2 - 0.99 秒 緩解並發情況下對DB壓力 usleep(rand(200, 999) * 1000); } while ($result = $selectQuery($pdo, $tableName, $start, $everyRow)); } catch (PDOException $e) { die( "Error!: " . $e->getMessage()); }
使用PHP生成器批量更新數據