In my opinion, best thing to do is to save all data as char_reg variables, e.g. WoE_Kill += 1;
And then once WoE is finished, run a SQL query to copy data from char_reg_num_db and char_reg_str_db to your custom table. E.g.
OnAgitEnd:
query_sql("TRUNCATE TABLE `woe_rank`;"); // clear old data. this table is only used to hold temporary values for displaying elsewhere
.@count = query_sql("SELECT `char_id`, `value` FROM `char_reg_num_db` WHERE `key` = 'WoE_Kill';", .@CID, .@WoE_Kill);
for (.@i = 0; .@i < .@count; .@i++) {
query_sql(sprintf("INSERT INTO `woe_rank`(`char_id`, `kills`) VALUES(%d, %d);", .@CID, .@WoE_Kill));
sleep(50); // don't perform too many queries at once (20 per second)
}
end;
So then the query_sql() command is only being run under controlled circumstances, and only used once.