Minor Lag Caused By db_query (SQLite)
#1

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 Код:
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 Код:
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.
Reply
#2

<24 Hour Bump>.
Reply
#3

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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)