1

我尝试使用准备好的选择从 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){
}

我的常规选择代码如下:

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 文件时,我看到发送准备好的 vol 与常规查询相同,即使是准备好的比较数据。

$ 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

如果可以,那么持续时间相同还是我不使用准备好的选择?

我该如何改进它?

谢谢。

4

1 回答 1

2

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

于 2013-08-22T09:24:57.117 回答