MySql Multi-threading?
#1

I'm currently using sqlite, and wanting to change over to MySql. I've seen posts where people are saying that multi threaded querious are faster and dont lag the server.

I've searched but never really found any good explanation about how it works and how to do it.

Could someone direct me to a good tutorial that explains multi-threading, and how to do it in pawno. Also which mysql plugin is most reccommended, and has little or no bugs?
Reply
#2

It isn't really faster, but it does allow for the server to continue operating while it runs queries. SQLite is faster anyway, assuming you're using it right
Reply
#3

So how can I learn multi-threadding? Any websites / tutorials that explain how to script it properly for samp?
Reply
#4

Anyone know a tutorial site or something where i can learn about multi-threaded queries?
Reply
#5

If you think about the SA-MP dialog system, when you use ShowPlayerDialog() and a player actions the dialog, the callback OnDialogResponse is called with the data that the user entered or selected (via listitem or inputtext).

With MySQL query threading, it's pretty simple - you have two callbacks (OnQueryFinish and OnQueryError) which is called when one of your queries completes, when you create a query with G-StyleZzZ's plugin, you are given the following parameters for executing a query with the mysql_query function:

pawn Code:
mysql_query(query[], resultid, extraid, connectionHandle)
The resultid is what the thread will send back to OnQueryFinish or OnQueryError, every time you execute a query, one of those callbacks are executed. Here's an example of how you'd track a query via its thread:

pawn Code:
#define THREAD_ACCOUNTS 1 // Define 'THREAD_ACCOUNTS' as 1, but you can just use 1, but this way you remember what your query thread IDs are so you don't lose track and declare multiple queries as the same ID

public OnQueryFinish(query[], resultid, extraid, connectionHandle) {
    switch(resultid) { // Personal preference: switch > if, no advantages in PAWN IIRC
        case THREAD_ACCOUNTS: {
            mysql_store_result(); // Store our result so we can access mysql_num_rows()
           
            new
                szMessage[26]; // Create a string so we can format a message
               
            format(szMessage, sizeof(szMessage), "There are %d accounts.", mysql_num_rows());
                    // mysql_num_rows is used to count how many rows the query picks up based on the criteria of your query
            SendClientMessage(extraid, 0, szMessage); // Forward a message to the client
           
            mysql_free_result(); // Free the result from memory because we no longer need it
        }
    }
}

CMD:allaccounts(playerid, params[]) {
        // Remember, syntax: mysql_query(query[], resultid, extraid, connectionHandle)
    mysql_query("SELECT * FROM `accounts`", THREAD_ACCOUNTS, playerid);
    return 1;
}
If you see in the command, I execute the query to retrieve data from every row for `accounts`, with the 'resultid' (thread) of THREAD_ACCOUNTS, then in the OnQueryFinish callback, I use 'extraid' (when I wrote the query line, I included 'playerid' for the 'extraid' parameter so we know what the playerid that should relate to that query, then in the OnQueryFinish callback, I check to see if 'resultid' matches THREAD_ACCOUNTS (1), and if it does then it finishes the job of the command.

Multi-threading is useful because SA-MP is single-threaded, while the MySQL plugin isn't - if you send multiple queries to multiple threads, SA-MP will continue processing and MySQL will process the queries on the other threads, so hence why people claim that multi-threading is more efficient. This is inexplicably useful if you're sending multiple queries to your MySQL server that may be slightly intensive. (like a really long query).

Sorry, I wrote more than I expected.
Reply
#6

Thanks a lot, this is really helpfull. Kinda makes sense now
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)