Loading stats with MySQL
#1

I was just wondering which is more efficient for loading MySQL stats

1) Using the method used to load player stats upon login found in Moderntopia and many other login systems

example taken from Moderntopia 1.0 script:

Код:
...
// The rcnt values here represent the order of the columns in the characters table, so don't mess with them
// If you add a column to the table, just add a new line with a +1 rcnt to the block below
// Start it at 3 to skip the first few records we don't need (id, player name, password)
if (rcnt == 3) PlayerInfo[playerid][pLevel] = strval(Field);
if (rcnt == 4) PlayerInfo[playerid][pAdmin] = strval(Field);
if (rcnt == 5) PlayerInfo[playerid][pDonateRank] = strval(Field);
...
If I understand this correctly, Moderntopia 1.0 fetches a giant string containing all the data for a player's account in one single query.
Then sorts it out by tokenizing (strtok) the string into little strings which are set into their appropriate variables via "rcnt"'s.

2) My method would look like this:

Код:
...
PlayerInfo[playerid][pLevel] = MySQLGetInt(playerSQLID, playerDatabase, "pLevel");
PlayerInfo[playerid][pAdmin] = MySQLGetInt(playerSQLID, playerDatabase, "pAdmin");
PlayerInfo[playerid][pDonateRank] = MySQLGetInt(playerSQLID, playerDatabase, "pDonateRank");
...
Код:
MySQLGetInt(sqlid, database[], property[])
{
	new
		query[128],
		strid[32],
		value;
		
	format(query, sizeof(query), "SELECT %s FROM %s WHERE ID = %d LIMIT 1", property, database, sqlid);
	mysql_query(query);
	mysql_store_result();
	if(mysql_num_rows() == 1)
	{
		mysql_fetch_row(strid);
		value = strval(strid);
		mysql_free_result();
		//printf("[Get] %s = %d", property, value);
		return value;
	}
	else
	{
	  mysql_free_result();
	  //printf("Property \"%s\" not found.", property);
	  return 0;
	}
}
As you can see, my method makes one query for every variable loaded. No strtok, no giant strings, but a load of queries, lol.

Although the method I have chosen is easier (imho), especially when adding and removing stats, I wonder whether it makes my code significantly less efficient.
Reply
#2

Bump

*waves to krisk*
Reply
#3

Definitely the first one where you loop through a row from a result, rather than having a dozen queries to fetch every single bit you need from the table.
Reply
#4

And here I was thinking I was a genius. Ah well.
Reply
#5

Quote:
Originally Posted by ledzep
And here I was thinking I was a genius. Ah well.
You could probably do it somehow by fetching the row and splitting it up in that function of yours, but having lots of calls to mysql is never a good idea!
Reply
#6

actually, when you reads huge variables from database with many queryes - server can start lag....
so only working desigion is
get
SELECT * From table etc
then getfieldvalue in for or while cycle...
so in mysql_samp exists function samp_mysql_strtok...
use it... it's good thing
Reply
#7

Yeah, I rewrote my playerLoad() function with Sneaky's example in mind.
I was noticing delays with my old method (described in original post), hopefully this method goes much faster.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)