folder Tahribat.com Forumları
linefolder Asp - Php - Cgi - Perl
linefolder [PHP] Hızlı İnsertler İçin Prepared Statement Kullanın



[PHP] Hızlı İnsertler İçin Prepared Statement Kullanın

  1. KısayolKısayol reportŞikayet pmÖzel Mesaj
    nurulmac11
    nurulmac11's avatar
    Kayıt Tarihi: 04/Ağustos/2012
    Erkek

    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");
        }

     


    black implies white, self implies other, life implies death.
  2. KısayolKısayol reportŞikayet pmÖzel Mesaj
    lepusmorris
    lepusmorris's avatar
    Kayıt Tarihi: 31/Mart/2007
    Erkek

    bence güzel hareketler bunlar.


    ..
Toplam Hit: 846 Toplam Mesaj: 2
php mysqli prepared statement