SA-MP Forums

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

Thread Tools Display Modes
Old 10/06/2014, 01:35 PM   #1
Join Date: Dec 2011
Posts: 73
Reputation: 49
Post Lag Caused By db_query (SQLite)

I noticed some lag which occurred in a command, and traced it to the saving proportion of my script. The issue is 34 queries (run at the same time with other queries used to save player data and information; amount also dependent on how many vehicles a player has that I'm saving information for).

pawn Code:
new string[128];
for(new l = 0; l < 17; l ++)
    format(string, sizeof(string), "UPDATE `VEHICLES` SET `MOD%d` = '%d' WHERE `ID` = '%d'", l, PlayerVehicleInformation[playerid][i][Mod][l], <ID>);
    db_query(database, string);

The proportion of code shown above was previously something similar (but not exact) to:

pawn Code:
new string[512];
format(string, sizeof(string), "UPDATE `VEHICLES` SET `MOD1`, `MOD2, `MOD3`, `MOD4` WHERE `ID` = '%d", mod[0], mod[1], mod[2], mod[3], etc);

However I decided to use a loop instead as it would be shorter. When retrieving information, these loops (and db_get_field_assoc()) do not cause an issue. Again, the lag is minor, but is there something I should be doing better to prevent db_query from causing this issue (aside from what I initially had)? Thanks.

Last edited by DrakeX; 10/06/2014 at 11:48 PM.
DrakeX is offline   Reply With Quote
Old 11/06/2014, 05:00 PM   #2
Join Date: Dec 2011
Posts: 73
Reputation: 49
Default Re: Minor Lag Caused By db_query (SQLite)

<24 Hour Bump>.
DrakeX is offline   Reply With Quote
Old 11/06/2014, 06:47 PM   #3
Big Clucker
DanLore's Avatar
Join Date: Jan 2013
Posts: 99
Reputation: 19
Default Re: Minor Lag Caused By db_query (SQLite)

Shorter is not the best way. I have several suggestions, which may not be perfect as I'm sure there are more knowledgeable users when it comes to queries. My first suggestion would be to put your database updates on a separate thread. This would then allow your GM to continue working, even if there's a backlog on the database.

My second suggestion would be to create a queue of updates. It would assign all of the queries to an array on your server's memory, then process them 1 by 1 every few seconds. This might take longer to update everything though, but it would prevent lag spikes.

If you can, use 1 query, it is always going to be more efficient and faster.
DanLore is offline   Reply With Quote

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
[Include] db_query error reporting Slice Includes 6 11/05/2014 08:03 PM
db_query() miltiple statements Baltazar Scripting Help 12 10/04/2014 12:02 AM
Warnings caused by YSI Amel_PAtomAXx Scripting Help 11 07/08/2013 04:55 PM
db_query questions... Edvin Scripting Help 3 06/03/2012 05:44 AM
db_query crashes the server Lorenc_ Help Archive 0 20/08/2011 03:26 AM

All times are GMT. The time now is 06:16 PM.

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