SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 17/11/2017, 08:13 PM   #1
Meller
Banned
 
Join Date: Dec 2016
Posts: 641
Reputation: 132
Default MySQL 40 - (p/t)query.

Can I have a brief simple enough explanation to what the p stands for in pquery? As I fully understand that tquery stands for threaded queries and I'm currently looking into the MySQL's all functions to gather a better experience with the plugin.

Instead of telling me
Quote:
If you are not sure which query native to use, use mysql_tquery().
(stated int the Wiki)

I'd rather have a full answer and actually learn what the difference is other than "don't know it since you were born, then don't use it and never learn it". So please, only relative answers please.


Thanks.
Meller is offline   Reply With Quote
Old 17/11/2017, 08:25 PM   #2
IstuntmanI
High-roller
 
IstuntmanI's Avatar
 
Join Date: Mar 2009
Location: Romania
Posts: 1,863
Reputation: 370
Default Re: MySQL 40 - (p/t)query.

From me: ( https://github.com/pBlueG/SA-MP-MySQL/issues/83 )
Quote:
mysql_tquery is better because it won't block your server's thread (noticeable at huge/bad queries). It will send the query to the MySQL server and will receive the result, then the MySQL Plugin will automatically call the callback for you to manipulate the result, and after the callback was finished, the MySQL Plugin automatically deletes it for you (so your problem from 1 will be gone). The disadvantage of tquery is that, unlike pquery, it will execute only on one connection, so if another big dirty ugly query is slowly executed, it will block that new query from being executed, but still not your main server's thread ! You will receive the result when it has it. If you use pquery you can send more than one unoptimised query and you will receive the results when they are received from each query. (I don't recommend you to use it just because you don't know how to make some queries optimised, you still have to optimise them)
pquery = parallel query, the queries can be executed in parallel, think of it like multiple tquery connections, it won't have just one queue for it, but it will have more, the value you can specify in mysql_set_option with the POOL_SIZE parameter.

Doing something like
Code:
mysql_pquery( ..., "SELECT * FROM `accounts`", "Callback1" );
mysql_pquery( ..., "SELECT 1", "Callback2" );
won't guarantee that the first query's callback will be executed first, they can be executed in any order. tquery keeps the order to call the callback (because they wait in the same queue, so they get executed one after one, in the order you sent them in your script), while pqueries can be executed in any order, as they have different queues.
__________________


IstuntmanI is offline   Reply With Quote
Old 17/11/2017, 08:29 PM   #3
Meller
Banned
 
Join Date: Dec 2016
Posts: 641
Reputation: 132
Default Re: MySQL 40 - (p/t)query.

Quote:
Originally Posted by IstuntmanI View Post
From me: ( https://github.com/pBlueG/SA-MP-MySQL/issues/83 )


pquery = parallel query, the queries can be executed in parallel, think of it like multiple tquery connections, it won't have just one queue for it, but it will have more, the value you can specify in mysql_set_option with the POOL_SIZE parameter.

Doing something like
Code:
mysql_pquery( ..., "SELECT * FROM `accounts`", "Callback1" );
mysql_pquery( ..., "SELECT 1", "Callback2" );
won't guarantee that the first query's callback will be executed first, they can be executed in any order. tquery keeps the order to call the callback (because they wait in the same queue, so they get executed one after one, in the order you sent them in your script), while pqueries can be executed in any order, as they have different queues.
So basically, threaded queries stack up on eachother and wait for eachother to run as in order as they were ran in code, and parallel queries will go at possibly different orders?
Meller is offline   Reply With Quote
Old 17/11/2017, 08:35 PM   #4
IstuntmanI
High-roller
 
IstuntmanI's Avatar
 
Join Date: Mar 2009
Location: Romania
Posts: 1,863
Reputation: 370
Default Re: MySQL 40 - (p/t)query.

Yes, but mysql_tquery itself runs on another thread, so if you send a huge query it won't block server's thread like mysql_query does, it will still have one queue (well, mysql_query has no queue at all, it is "instant"), so if you send a huge query then a small query, the small query will be executed really late because the huge query is still being executed, but at least the main server thread won't be locked for a while (like mysql_query does). For multiple threads/queues you need to use parallel query.

I'd suggest to use mysql_pquery as much as possible, with at least a pool size of 4 (4 queues/connections for the mysql_pquery function). It will be fine as long as you don't think that it gets executed in order. Keep in mind that.
__________________


IstuntmanI is offline   Reply With Quote
Old 17/11/2017, 08:37 PM   #5
Meller
Banned
 
Join Date: Dec 2016
Posts: 641
Reputation: 132
Default Re: MySQL 40 - (p/t)query.

Quote:
Originally Posted by IstuntmanI View Post
Yes, but mysql_tquery itself runs on another thread, so if you send a huge query it won't block server's thread like mysql_query does, it will still have one queue (well, mysql_query has no queue at all, it is "instant"), so if you send a huge query then a small query, the small query will be executed really late because the huge query is still being executed, but at least the main server thread won't be locked for a while (like mysql_query does). For multiple threads/queues you need to use parallel query.

I'd suggest to use mysql_pquery as much as possible, with at least a pool size of 4 (4 queues/connections for the mysql_pquery function). It will be fine as long as you don't think that it gets executed in order. Keep in mind that.
Alright, thank you.
Meller is offline   Reply With Quote
Old 18/11/2017, 01:33 AM   #6
wallee
Huge Clucker
 
Join Date: Jan 2017
Posts: 284
Reputation: 40
Default Re: MySQL 40 - (p/t)query.

I was also curious about this but never asked. The last time i was reading about "pquery" i remember somebody said something like:

"it can seriously mess up your server if you don't know what you're doing"

They didn't provide an example so if one could be provided that would be great. :P
wallee is offline   Reply With Quote
Old 18/11/2017, 02:16 PM   #7
IstuntmanI
High-roller
 
IstuntmanI's Avatar
 
Join Date: Mar 2009
Location: Romania
Posts: 1,863
Reputation: 370
Default Re: MySQL 40 - (p/t)query.

Quote:
Originally Posted by Meller View Post
Alright, thank you.
You are welcome.

-------------

Quote:
Originally Posted by wallee View Post
I was also curious about this but never asked. The last time i was reading about "pquery" i remember somebody said something like:

"it can seriously mess up your server if you don't know what you're doing"

They didn't provide an example so if one could be provided that would be great. :P
Well, it can mess up some things if you expect the results to come in order. You may need to use mysql_tquery instead of mysql_pquery if you really need the results in order. For example, something like:
PHP Code:
RegisterPlayerplayerid )
{
    
mysql_pquery1"INSERT INTO `users` VALUES( MY_KEY_ID, ... )""RegisteredPlayerInsert"playerid );
    
mysql_pquery1"SELECT * FROM `users` WHERE `id` = MY_KEY_ID""SelectInsertedValues"playerid );
}

function 
RegisteredPlayerInsertplayerid )
{
    
// blahblahblah
}

function 
SelectInsertedValuesplayerid )
{
    
// do things with user's row

won't be fine, as "SELECT * FROM `users` WHERE `id` = MY_KEY_ID" could be actually executed before the insert (so the result will be empty, no rows, but you may expect one row EVERYTIME ! if the SELECT is executed before INSERT, there won't be any row, otherwise it will have the full row you requested), not after it like you see in the code, for that you have to use mysql_tquery. Ideally, you can still keep using mysql_pquery, but do it in this way:
PHP Code:
RegisterPlayerplayerid )
{
    
mysql_pquery1"INSERT INTO `users` VALUES( 0, ... )""RegisteredPlayerInsert"playerid );
}

function 
RegisteredPlayerInsertplayerid )
{
    
// blahblahblah
    
formatlString128"SELECT * FROM `users` WHERE `id` = %d"cache_insert_id( ) );
    
mysql_pquery1lString"SelectInsertedValues"playerid );
}

function 
SelectInsertedValuesplayerid )
{
    
// do things with user's row

Not the best real life example, but some other things may look similar to this. Most of the code can be made perfectly compatible with the mysql_pquery function, you just have to think how they may mess up if they won't get executed in order.
__________________


IstuntmanI is offline   Reply With Quote
Old 18/11/2017, 03:40 PM   #8
wallee
Huge Clucker
 
Join Date: Jan 2017
Posts: 284
Reputation: 40
Default Re: MySQL 40 - (p/t)query.

It cleared things up, thanks.
wallee is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MySQL R39-3] MySQL losing connection during query MEW273 Scripting Help 2 13/04/2015 08:10 AM
MySQL query only updates first letter of column. (BlueG MySQL) Jimmy0wns Scripting Help 1 06/04/2015 12:04 PM
[MYSQL] MySQL Query unsuccesfull toofast Scripting Help 8 04/07/2013 04:19 PM
[Help][MySQL]MySQL query unsuccessful. Timmi Scripting Help 14 01/01/2012 02:01 PM
Failed to exeute query. Lost connection to MySQL server during query. *IsBack Help Archive 10 06/01/2011 08:50 AM


All times are GMT. The time now is 07:38 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.