Bulk query
#1

pawn Код:
stock NS-><EndSession(pid)> {
    new tmp[64];
    mysql_format(dbhandle, tmp, "UPDATE `%s` SET session_end = NOW() WHERE id = %d", dbtables[TB_SESSIONS], GetPVarInt(pid, "session_id"));
    mysql_function_query(dbhandle, tmp, false, "", "");
   
    //Kill session
    SetPVarInt(pid, "session_end", 0);
    SetPVarInt(pid, "session_start", 0);
   
    return 1;
}
This is my function killing session for single user. But when I kill server, there can be even 500 queries at once, so that's not good.

pawn Код:
stock NS-><EndAllSessions()> {
    //No players connected
    if(!Iter_Count(Player)) return 1;
    #define QLEN (511)
    new tmp[QLEN];
   
    format(tmp, sizeof tmp, "UPDATE `%s` SET session_end = NOW() WHERE id IN (", dbtables[TB_SESSIONS]);
    new j = strlen(tmp), last = Iter_Last(Player), len;
    foreach(new pid : Player) {
        if(GetPVarInt(pid, "session_start")) {
            //Id is bigint(20)
            new uid[21];
            //User id as a string
            valstr(uid, GetPVarInt(pid, "uid"));
            len = strlen(uid);
            new bool:isLast = pid == last;
            //Current index + id length + comma separator >= string length - ')'
            if(j + len + (isLast ? 0 : 1) >= QLEN - 1) return NS-><EndAllSessions()>;
            j += len;
            strcat(tmp, uid);
            if(!isLast) {
                tmp[++j] = ',';
            }
           
            //Kill session
            SetPVarInt(pid, "session_start", 0);
            SetPVarInt(pid, "session_end", 0);
        }
    }
    #undef QLEN
    tmp[j] = ')';
    mysql_function_query(dbhandle, tmp, false, "", "");
    return 1;
}
Can I optimise this any more?
Reply


Messages In This Thread
Bulk query - by Misiur - 25.02.2013, 13:53
Re: Bulk query - by Misiur - 25.02.2013, 14:52
Re: Bulk query - by Vince - 25.02.2013, 15:27
Re: Bulk query - by Misiur - 25.02.2013, 16:27

Forum Jump:


Users browsing this thread: 1 Guest(s)