The database server executes prepared statements and regular statements with the same speed. The performance difference comes when you execute the same query with different parameters: a prepared statement is parsed and prepared for execution once and then can be executed cheaply with different parameters, while a regular statement has to be parsed every time you want to execute it.
compare regular select and prepared select performace
-
26-06-2022 - |
Pergunta
I try to use prepared select for get data from mysql,beacuse I think this faster than regular select.
this is select syntax:
char *sql = "select id,d1,d2,d3,d4,d5 from pricelist where d1 > ? limit 1000000";
that id,d2,d3 type unsigned int
and other __int64
I wirte my code for prepared like below:
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){
}
and my code for reqular select is like below:
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)){
}
I run this two functions from remote pc and check time period for each one,duration for both of then is same equal to 6 sec and when I check pcap file I see that vol that sent for prepared is same with reqular query even that in prepared comperes data.
$ 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
if this ok that duration is same or I don't use prepared select as well as?
how I can improve it?
thanks.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow