




[PHP] Hızlı İnsertler İçin Prepared Statement Kullanın
-
merhaba
uğraştığım bir işte hızlıca bissürü insert yapmam gerek, bissürü deneme yaptım ve prepared statement beklemediğim kadar hızlı oldu. normalde 10sn kadar süren ekleme işlemim 1 saniyelere düştü. ben de hayretler içindeyim.
örnek kodu yazıyorum, değiştirip kullanırsınız . execute fonksiyonuna bakmanız yeterli olacaktır, kod aşağıda sik gibi çıkıyorsa bu da paste linki : https://paste.ee/p/pvxQL
ayrıca, transactionlar sadece innodb de var ama myisam lı tabloda da bu kodu kullandım ve yedi. (hayret)
class SoapCall_GetImages extends PlentySoapCall { public $mysqli = null; public $images = array(); public $imagePagesCount; public function __construct() { parent::__construct(__CLASS__); } public function fillMysqli() { if ($this->mysqli == null) { try { $this->mysqli = new mysqli(SQL_DATA_SOURCE, SQL_USERNAME, SQL_PASSWORD, SQL_DATA_BASE); } catch (Exception $ex) { $this->getLogger()->debug('cant connect to mysqli'); } $this->mysqli->query("SET NAMES 'utf8'"); return true; } return false; } public function execute() { $this->getLogger()->debug('here i am'); $this->getImagePages(); for ($i = 0; $i < $this->imagePagesCount; $i ++) { $this->getLogger()->debug('Getting images for page: ' . $i); $this->getImages($i); } $this->getLogger()->debug('Images count: ' . count($this->images)); $this->fillMysqli(); $this->getLogger()->debug('Starting to inserting images to database'); $query = "INSERT INTO `images` ( `itemId`, `imageUrl`, `fileName`) VALUES (?, ?, ?)"; $stmt = $this->mysqli->prepare($query); $stmt->bind_param("iss", $itemId, $imageUrl, $fileName); $this->mysqli->query("start transaction"); foreach ($this->images as $img) { $itemId = $img['itemId']; $imageUrl = $img['imageUrl']; $fileName = pathinfo(parse_url($img['imageUrl'])['path'])['basename']; $stmt->execute(); } $stmt->close(); $this->mysqli->query("COMMIT"); $this->getLogger()->debug("Images Committed to database"); $this->processImages(); $this->clearProducts(); $this->getLogger()->debug("GetImages.class finished"); }
-
bence güzel hareketler bunlar.