比较常规选择和准备选择性能

时间:2013-08-22 08:49:21

标签: c++ mysql c mysql-connector

我尝试使用准备好的select来从mysql获取数据,因为我认为这比常规选择更快。

这是选择语法:

char *sql = "select id,d1,d2,d3,d4,d5 from pricelist where d1 > ? limit 1000000";

id,d2,d3类型unsigned int和其他__int64

我的代码准备如下:

stmt = mysql_stmt_init(conn);
mysql_stmt_prepare(stmt, sql, strlen(sql));

// Select
param[0].buffer_type     = MYSQL_TYPE_LONG;
param[0].buffer         = (void *) &myId;
param[0].is_unsigned    = 1;
param[0].is_null        = 0;
param[0].length         = 0;

// Result


result[0].buffer_type     = MYSQL_TYPE_LONG;
result[0].buffer         = (void *) &id;
result[0].is_unsigned    = 1;
result[0].is_null         = &is_null[0];
result[0].length         = 0;

result[1].buffer_type     = MYSQL_TYPE_LONGLONG;
result[1].buffer         = (void *) &d1;
result[1].is_unsigned    = 1;
result[1].is_null         = &is_null[0];
result[1].length         = 0;

result[2].buffer_type     = MYSQL_TYPE_LONG;
result[2].buffer         = (void *) &d2;
result[2].is_unsigned    = 1;
result[2].is_null         = &is_null[0];
result[2].length         = 0;

result[3].buffer_type     = MYSQL_TYPE_LONG;
result[3].buffer         = (void *) &d3;
result[3].is_unsigned    = 1;
result[3].is_null         = &is_null[0];
result[3].length         = 0;

result[4].buffer_type     = MYSQL_TYPE_LONGLONG;
result[4].buffer         = (void *) &d4;
result[4].is_unsigned    = 1;
result[4].is_null         = &is_null[0];
result[4].length         = 0;

result[5].buffer_type     = MYSQL_TYPE_LONGLONG;
result[5].buffer         = (void *) &d5;
result[5].is_unsigned    = 1;
result[5].is_null         = &is_null[0];
result[5].length         = 0;

mysql_stmt_bind_param(stmt, param);
mysql_stmt_bind_result(stmt, result);
mysql_stmt_execute(stmt);
mysql_stmt_store_result(stmt);
while(mysql_stmt_fetch (stmt) == 0){
}

我的reqular选择代码如下:

mysql_query(conn,"select id ,d1,d2,d3,d4,d5 from pricebook where us > 12 limit 1000000")
result = mysql_use_result(conn); 
while (mysql_fetch_row(result)){
}

我从远程pc运行这两个功能并检查每个功能的时间段,两者的持续时间相同,等于6秒 当我检查pcap文件时,我发现准备发送的卷与reqular查询相同,即使是在准备好的竞争数据中也是如此。

$ capinfos prepared.pcap regular.pcap
File name:           prepared.pcap
File type:           Wireshark - pcapng
File encapsulation:  Ethernet
Packet size limit:   file hdr: (not set)
Number of packets:   40 k
File size:           53 MB
Data size:           52 MB
Capture duration:    6 seconds
Start time:          Thu Aug 22 09:41:54 2013
End time:            Thu Aug 22 09:42:00 2013
Data byte rate:      8820 kBps
Data bit rate:       70 Mbps
Average packet size: 1278.63 bytes
Average packet rate: 6898 packets/sec
SHA1:                959e589b090e3354d275f122a6fe6fbcac2351df
RIPEMD160:           7db6a437535d78023579cf3426c4d88d8ff3ddc3
MD5:                 888729dc4c09baf736df22ef34bffeda
Strict time order:   True

File name:           regular.pcap
File type:           Wireshark - pcapng
File encapsulation:  Ethernet
Packet size limit:   file hdr: (not set)
Number of packets:   38 k
File size:           50 MB
Data size:           49 MB
Capture duration:    6 seconds
Start time:          Thu Aug 22 09:41:05 2013
End time:            Thu Aug 22 09:41:11 2013
Data byte rate:      7740 kBps
Data bit rate:       61 Mbps
Average packet size: 1268.65 bytes
Average packet rate: 6101 packets/sec
SHA1:                badf2040d826e6b0cca089211ee559a7c8a29181
RIPEMD160:           68f3bb5d4fcfd640f2da9764ff8e9891745d4800
MD5:                 4ab73a02889472dfe04ed7901976a48c
Strict time order:   True

如果确定持续时间相同或者我没有使用准备好的选择以及?

我如何改进它?

感谢。

1 个答案:

答案 0 :(得分:2)

数据库服务器以相同的速度执行预准备语句和常规语句。当您使用不同的参数执行相同的查询时会出现性能差异:准备好的语句被解析并准备执行一次然后可以使用不同的参数廉价执行,而每个必须解析一个常规语句你想要执行的时间。