MySQL INSERT速记与速记性能对比

MySQL INSERT速记与速记性能对比,mysql,database,performance,query-performance,insert-into,Mysql,Database,Performance,Query Performance,Insert Into,以下两者之间的区别是什么: INSERT INTO `table_name` SET `col1` = 'val1', `col2` = 'val2', `col3` = 'val3' 及 使用速记对性能有好处吗?我自己更喜欢速记,但从逻辑上讲,出于性能原因,我应该每次都使用速记吗?基准测试告诉我,上面的选项2速度快了一点,大约3%,但为什么呢 public function benchmarkInsert() { $int_value = 1; $varchar_value

以下两者之间的区别是什么:

INSERT INTO `table_name` SET `col1` = 'val1', `col2` = 'val2', `col3` = 'val3'

使用速记对性能有好处吗?我自己更喜欢速记,但从逻辑上讲,出于性能原因,我应该每次都使用速记吗?基准测试告诉我,上面的选项2速度快了一点,大约3%,但为什么呢

public function benchmarkInsert() {

    $int_value = 1;
    $varchar_value = 'Stuff';
    $serialized_value = json_encode(array('Serialized Stuff'));

    $this->db->query("TRUNCATE TABLE `query_benchmark`");

    $start = microtime(true);       

    for ( $i = 0; $i < 10000; $i++ ) {          
        $this->db->query("INSERT INTO `query_benchmark` (`col1`,`col2`,`col3`) VALUES($int_value,'$varchar_value','$serialized_value')");
    }       

    $elapsed = microtime(true) - $start;

    echo 'SHORTHAND: '.$elapsed.'<br/>';

    $this->db->query("TRUNCATE TABLE `query_benchmark`");       

    $start = microtime(true);       

    for ( $i = 0; $i < 10000; $i++ ) {

        $this->db->query("INSERT INTO `query_benchmark` SET `col1` = $int_value, `col2` = '$varchar_value', `col3` = '$serialized_value'");

    }       

    $elapsed = microtime(true) - $start;

    echo 'LONGHAND: '.$elapsed.'<br/>';

}
public function benchmarkInsert(){
$int_值=1;
$varchar_value='Stuff';
$serialized_value=json_encode(数组('serialized Stuff'));
$this->db->query(“截断表`query\u benchmark`”);
$start=microtime(真);
对于($i=0;$i<10000;$i++){
$this->db->query(“插入到`query\u benchmark`(`col1`、`col2`、`col3`)值($int\u值、$varchar\u值'、'$serialized\u值');
}       
$appeased=微时间(true)-$start;
回显“速记:”.$appeased.
; $this->db->query(“截断表`query\u benchmark`”); $start=microtime(真); 对于($i=0;$i<10000;$i++){ $this->db->query(“插入`query\u benchmark`SET`col1`=$int\u值,`col2`='$varchar\u值',`col3`='$serialized\u值'”); } $appeased=微时间(true)-$start; 回音“LONGHAND:”.$appeased.
; }
这两个查询应该没有区别。数据库应将它们转换为相同的执行计划

3%的差异可能是由不相关的因素(缓存、数据库负载等)造成的

您可以通过使用
EXPLAIN

差别是2#1比#2长2个字符

我在记事本上测试过

随着时间的推移,我们正在谈论腕管综合征


同时,用“

或解析器逻辑路径中的一些细微差别来打断您的左小指。必须是缓存。当我切换速记和速记时,差分基本上是一样的,但可能是翻来翻去的。@FurryWombat。3%的利润率很小,可以用很多不同的东西来解释。哈,德鲁,你在和一个钢琴家说话。无名指不停地敲击它。但是感谢您的想法:)如果您在一个
insert
语句中插入100行,您将获得大约10倍的改进。也许1000%比3%好?(它需要第二种语法的变体。)
public function benchmarkInsert() {

    $int_value = 1;
    $varchar_value = 'Stuff';
    $serialized_value = json_encode(array('Serialized Stuff'));

    $this->db->query("TRUNCATE TABLE `query_benchmark`");

    $start = microtime(true);       

    for ( $i = 0; $i < 10000; $i++ ) {          
        $this->db->query("INSERT INTO `query_benchmark` (`col1`,`col2`,`col3`) VALUES($int_value,'$varchar_value','$serialized_value')");
    }       

    $elapsed = microtime(true) - $start;

    echo 'SHORTHAND: '.$elapsed.'<br/>';

    $this->db->query("TRUNCATE TABLE `query_benchmark`");       

    $start = microtime(true);       

    for ( $i = 0; $i < 10000; $i++ ) {

        $this->db->query("INSERT INTO `query_benchmark` SET `col1` = $int_value, `col2` = '$varchar_value', `col3` = '$serialized_value'");

    }       

    $elapsed = microtime(true) - $start;

    echo 'LONGHAND: '.$elapsed.'<br/>';

}