gpt4 book ai didi

MySQL INSERT 简写与普通写性能

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:31 24 4
gpt4 key购买 nike

<分区>

有什么区别:

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

INSERT INTO `table_name` (`col1`,`col2`,`col3`) VALUES('val1','val2','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/>';

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com