Questions about MySQL
#1

Hello!
I've been reading about threading MySQL queries. Some people say it is much better.
I have a question about it:
What better does it actually bring, and is it worth moving from normal queries to threaded queries?
If so, why?

Also, which one is better when handling received data from database.
Use mysql_fetch_field_row, or sscanf?
And is the difference big?

Thanks, Johnson_boy
Reply
#2

Quote:
Originally Posted by Calgon
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 Код:
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 Код:
#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).
I wrote this up a while ago. Read it and reply if you have any questions, I'll be happy to answer them.
Reply
#3

Thanks for the answer Calg00ne. I have one more question (for now ).
I have a query which returns tens of fields from table.
What is the smartest and fastest way to fetch them to server variables?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)