2013-08-22 70 views
1

我嘗試使用準備選擇從MySQL獲取數據,因爲我認爲這比普通選擇更快。比較常規選擇和準備選擇性能

這是選擇語法:

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

該ID,D2,D3型unsigned int__int64

我WIRTE我的代碼編寫象下面這樣:

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 

如果這個確定持續時間是相同的,或者我不使用準備選擇以及?

我該如何改進它?

謝謝。

回答

2

數據庫服務器以相同的速度執行預準備語句和常規語句。當您使用不同的參數執行相同的查詢時,會出現性能差異:準備好的語句被解析並準備執行一次然後可以用不同的參數低成本執行,而每次要執行時都要解析常規語句它。