Hey all,
I have been developing mafia style text-based games for approximately six years now, and I have found one very common trait amongst most of them. That is: lack of database optimization.
When you have a database, you should not have one table with almost 100 columns in it to be queried each and every time you load a page. That takes a long time to load all of that information just for a small subset of that data. So the immediate solution would be to make the tables much smaller and query all of those tables, right? Well, not necessarily. This is based off of what the particular use and need of those tables are. If say there’s a user’s table with general information on it, then sure, that can be a table. If there’s stats in that table, maybe split that up and have a table just for the user’s stats.
Why am I telling you all of this? Well, it goes with the territory of owning a mafia game. You will be querying the database many, many times throughout the course of the game, and you want those queries to return as fast as possible; however, with every query call to the database, that’s more time than simply using results that you have already received. So, as with everything, you have to find that balance in order to have your game perform at its best. So, in lieu of that, here are a few guidelines for good database optimization:
1) Split up the obvious information differences into their own tables
If say you currently have one table for all of the user’s information (including user’s stats), then the stats should be split up from the other information into its own table.
2) No tables with 50+ columns in them
This is simply an easy guideline to follow. If you have a table that has more than 50 columns, split it up somehow. A query that returns that much information will take a much longer time to run than say two queries of a much smaller data set.
3) Run some performance tests!
If you’re not sure whether your queries are efficient, run a performance test! Check how long it takes to run those queries. PHPMyAdmin and other database management tools have options to run queries on their own in order to check for performance.To do a performance test on your queries, you can do one (or more) of the following:
– Run Queries Separately in PHPMyAdmin
You can do this by going to the “SQL” tab in your database and run the desired query.
– Place microtime(true) functions on either side of the query and subtract the difference
PHP has a microtime(true) function that you can use to time anything from a timestamp to a query. Call the microtime(true) function and set to a variable (say $a) before running a query, run the query, and then set microtime(true) again (to $b this time). Then, all you have to do from there is subtract. A code block is below of an example. With this example, $b at the last line will have the time that you are looking for:
$a = microtime(true);
$result = ("QUERY GOES HERE");
$b = microtime(true);
$b = $b - $a;
echo 'It took ' . $b . 'seconds to run this query.';
Why did I pick microtime? I picked microtime because it will be more accurate than the time() function, as it will most likely take less than a second to run. For more on microtime() function, check out here: http://php.net/manual/en/function.microtime.php.
– Write Triggers (Advanced)
For those advanced SQL users, you can always write a couple triggers (one before accessing data and another after accessing data) that will achieve similar results to the other methods; however, with triggers, you can make triggers apply to specific tables, especially those of which you are more concerned about. With those triggers, you can also always have them give you data logs of information, which you can then use to make your own analysis of the performance of said query(ies). For more information on triggers, check out here: http://dev.mysql.com/doc/refman/5.0/en/triggers.html.
If you have any other comments or suggestions as to how to fully optimize your queries, please leave some feedback in the comments section or contact me! 🙂
That’s all for now, until next time…keep that tech gear ticking.
~Goliath7470
