10.04.2015, 21:38
Introduction
Recently, a friend asked me what the difference between mysql_pquery and mysql_tquery is, so I thought I’d make a tutorial. The wiki actually explains the difference, but I thought I’d go into some more detail.
mysql_tquery
mysql_tquery will send a MySQL query that’ll be executed in another thread. You also have the option of specifying a callback and passing through any parameters to that callback, which will be called when the query has finished executing. So, what is a callback used for?
Callbacks
A callback is usually set to be called after a SELECT query, where data is selected from the MySQL database and needs something to be done with it. For example, if I did:
I’d then need to do something with the rows that we just selected from the database. So, when the query has finished executing (all the rows have been selected), we can call a callback in which we will process our data:
You can also supply a specifier to pass certain parameters to the callback. For example, I can pass "playerid" through to the callback, so I can send the player a message with SendClientMessage:
You may find you also need to call a callback for other queries, for example if you delete or update rows.
mysql_pquery
mysql_pquery is slightly different from mysql_tquery, but their usages and parameters are the same. mysql_pquery uses multi-threading, and therefore can be faster, depending on how many connections are used. The amount of connections in the connection pool can be set in mysql_connect. A connection pool is essentially a cache of connections to a database that are stored for further use to enhance performance speeds. So, mysql_pquery can execute queries on different connections, thus improving speed and performance.
Conclusion
"Which should I use?"
I suppose it doesn't entirely matter which of the two functions you use, but if you have multiple connections to your MySQL database and would prefer to see a performance enhancement, you might want to look into using mysql_pquery.
I hope I explained things well. If there's anything I explained incorrectly or badly, please let me know.
Recently, a friend asked me what the difference between mysql_pquery and mysql_tquery is, so I thought I’d make a tutorial. The wiki actually explains the difference, but I thought I’d go into some more detail.
mysql_tquery
mysql_tquery will send a MySQL query that’ll be executed in another thread. You also have the option of specifying a callback and passing through any parameters to that callback, which will be called when the query has finished executing. So, what is a callback used for?
Callbacks
A callback is usually set to be called after a SELECT query, where data is selected from the MySQL database and needs something to be done with it. For example, if I did:
pawn Код:
mysql_tquery(connectionhandle, "SELECT * FROM `players`", "OnPlayerCount");
pawn Код:
forward OnPlayerCount();
public OnPlayerCount()
{
new rows, fields; //These can be made global, of course
cache_get_data(rows, fields, connectionhandle);
//Fetching the amount of rows and fields found (in the result)
printf("%i players found in the table.", rows);
return 1;
}
pawn Код:
CMD:moneyscore(playerid, params[])
{
new id;
if(sscanf(params, "d", id)) return SendClientMessage(playerid, -1, "/moneyscore [reg ID]");
format(query, sizeof(query), "SELECT * FROM `players` WHERE id = %d", id);
mysql_tquery(connectionhandle, query, "OnPlayerDataSelected", "i", playerid);
return 1;
}
forward OnPlayerDataSelected(playerid);
public OnPlayerDataSelected(playerid)
{
new string[40], score, money;
cache_get_row(0, 0, string); score = strval(string);
cache_get_row(0, 1, string); money = strval(string);
//I doubt your money and score columns will ever be the first and second in the table, but it's just an example.
format(string, sizeof(string), "Score: %d || Money: $%d", score, money);
SendClientMessage(playerid, -1, string);
return 1;
}
mysql_pquery
mysql_pquery is slightly different from mysql_tquery, but their usages and parameters are the same. mysql_pquery uses multi-threading, and therefore can be faster, depending on how many connections are used. The amount of connections in the connection pool can be set in mysql_connect. A connection pool is essentially a cache of connections to a database that are stored for further use to enhance performance speeds. So, mysql_pquery can execute queries on different connections, thus improving speed and performance.
Conclusion
"Which should I use?"
I suppose it doesn't entirely matter which of the two functions you use, but if you have multiple connections to your MySQL database and would prefer to see a performance enhancement, you might want to look into using mysql_pquery.
I hope I explained things well. If there's anything I explained incorrectly or badly, please let me know.