Raised This Month: $51 Target: $400
 12% 

Multiple ordered SQL queries


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
AnIHiL
Member
Join Date: Sep 2009
Old 08-19-2011 , 16:01   Multiple ordered SQL queries
Reply With Quote #1

I checked all topic related to MySQL queries but I didn't find out how to solve my problem.

How can I make 3-4 SQL threaded queries but they should run in exact order:
1. Query 1 -> give results
2. Query 2 -> give results
...

Query 2 should be executed after I get all results from query 1.

I can make something like this:

Code:
SQL_TQuery(hdatabase, Q1Res, query1, userid);

public Q1Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q2Res, query2, userid);
}

public Q2Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q3Res, query3, userid);
}

public Q3Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q4Res, query4, userid);
}

public Q4Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
}
But as you can see this code will be very complicated if I insert more queries there.

Is there any other solution how to solve it?
AnIHiL is offline
Send a message via MSN to AnIHiL
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-19-2011 , 17:05   Re: Multiple ordered SQL queries
Reply With Quote #2

Quote:
Originally Posted by AnIHiL View Post
Is there any other solution how to solve it?
Use SQL_Connect and SQL_Query instead of their threaded equivalents.

Essentially, the threaded versions are there so you don't have to wait for a response from the database before you continue execution.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
FaTony
Veteran Member
Join Date: Aug 2008
Old 08-19-2011 , 17:11   Re: Multiple ordered SQL queries
Reply With Quote #3

IIRC MySQL allows you to chain queries separating them by ;
__________________
FaTony is offline
AnIHiL
Member
Join Date: Sep 2009
Old 08-19-2011 , 17:42   Re: Multiple ordered SQL queries
Reply With Quote #4

Quote:
Originally Posted by Powerlord View Post
Use SQL_Connect and SQL_Query instead of their threaded equivalents.

Essentially, the threaded versions are there so you don't have to wait for a response from the database before you continue execution.
I need to use threads because I have a lot of queries to database and when If I don't use threads I have lags on server.

Quote:
Originally Posted by FaTony View Post
IIRC MySQL allows you to chain queries separating them by ;
I need to query #2 after I get all results from query #1 because those results set some variables which are required for query #2 so separating those queries with ';' won't work for me.
AnIHiL is offline
Send a message via MSN to AnIHiL
psychonic

BAFFLED
Join Date: May 2008
Old 08-19-2011 , 18:43   Re: Multiple ordered SQL queries
Reply With Quote #5

Quote:
Originally Posted by FaTony View Post
IIRC MySQL allows you to chain queries separating them by ;
The implementation needs to be compiled with support for that specifically enabled. SM's mysql extension does not have it enabled afaik.
psychonic is offline
DarkEnergy
SourceMod Donor
Join Date: Apr 2008
Location: Georgia Tech, MSECE
Old 08-19-2011 , 19:46   Re: Multiple ordered SQL queries
Reply With Quote #6

SM MYSQL EXTENSION does NOT do concurrent requests. that is

function(){
threadedquery1
threadedquery2
threadedquery3
threadedquery4
threadedquery5 to a different database
}

Query 1 will always finish before query 2...etc. Even if they are different database connections.
The extension basically has 1 thread, looks at the pending queries, takes the top one, waits for it to return and calls the results callback function on the next game frame.
Rinse and repeat.

The queue has priority, so just keep all your sequential queries the same priority.

MYSQL queries are also limited to a max of 10 / second (arbitrary? limit set by dvander)
__________________
War3:Source Developer
"Your CPU is just a bunch of Muxes"

Last edited by DarkEnergy; 08-19-2011 at 19:50.
DarkEnergy is offline
AnIHiL
Member
Join Date: Sep 2009
Old 08-20-2011 , 03:44   Re: Multiple ordered SQL queries
Reply With Quote #7

So it will alway s be like:

function(){
threadedquery1
callback query1
threadedquery2
callback query2
threadedquery3
callback query3
threadedquery4
callback query4
threadedquery5 to a different database
callback query5
}

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.

Limit is 10/sec not 20/sec ?
AnIHiL is offline
Send a message via MSN to AnIHiL
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-20-2011 , 10:23   Re: Multiple ordered SQL queries
Reply With Quote #8

Quote:
Originally Posted by AnIHiL View Post

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.
Why don't you just use the callback functions ?
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
DarkEnergy
SourceMod Donor
Join Date: Apr 2008
Location: Georgia Tech, MSECE
Old 08-21-2011 , 16:26   Re: Multiple ordered SQL queries
Reply With Quote #9

Quote:
Originally Posted by AnIHiL View Post
So it will alway s be like:

function(){
threadedquery1
callback query1
threadedquery2
callback query2
threadedquery3
callback query3
threadedquery4
callback query4
threadedquery5 to a different database
callback query5
}

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.

Limit is 10/sec not 20/sec ?
function(){
threadedquery1
threadedquery2
threadedquery3
threadedquery4
threadedquery5 to a different database
}

function completes immediately because all these queries are queued into another thread. The whole purpose of threaded queries is that they do not BLOCK the function. threadquery1 callback will happen before threadquery2 can actually start. the callback is called whenever the results are returned.
queries can share callbacks

if your queries rely on the results of the previous query, just SQL_TQuery within the results callback.
__________________
War3:Source Developer
"Your CPU is just a bunch of Muxes"

Last edited by DarkEnergy; 08-21-2011 at 16:28.
DarkEnergy is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 17:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode