Jump to content

[11284]Prepared statements for MySQL [part 1]


Auntie Mangos

Recommended Posts

Ok, now thanks to Undergarun and kero99, I have some nice stats to share with you :)

This is the MySQL Server statistics for current mangos server, which is not using prepared statements. Taken from http://getmangos.eu/community/topic/15925/11045patch-multiple-mysql-connections-to-database/:

24mysqlworkload.png

And here are the stats from MySQL for mangos with prepared statements:

25kmysqlstatistics.png

42% of DB requests are using prepared statements now (look at the 'stmt execute' graph!) :) And this is achieved by only 90 requests out of total 900 implemented in mangos core.

Another interesting point is that SELECTs dramatically dropped to only 7% of total requests in comparison to up to 28.3% on regular mangos server. This might be due to lowered "Player.SaveInterval" value in config, I'll confirm that with tester.

Anyway, fingers crossed on upcoming small patch from fgenesis which will be able to get profiling data on per-DB-request basis. And in turn we will be able to move more queries which are critical to server performance on using prepared statements.

P.S. Correct me in case I did wrong interpretation of 'stmt execute' graph. In case of prepared statements, aren't the 'select/delete/insert/update' graphs updated too? Anyone?

Cheers to everyone :)

Link to comment
Share on other sites

  • 41 years later...

Hi, everyone :)

Here is the first part of prepared statement work I'm doing for the Core. If you are not familiar with prepared statements, here are the benefits they give:

1) more speed - your DBMS is not spending time on reparsing your SQL requests all the time, it just does it once;

2) less bandwidth with DBMS - sending data in binary format is very space efficient comparing with plain string requets;

3) more security - no SQL injection is possible.

This part of a patch is intended to implement prepared statements support for INSERT+DELETE+UPDATE queries - such requests cover ~70% of all DBMS tasks. The SELECT statement support will be the next part of this work.

Currently I've rewritten all SQL requests which are invoked from Player::SaveToDB() function. Also, if someone has any profiling data about what SQL requests are used most of the time - I'll greatly appreciate it :)

NOTE: For PostgreSQL users there will be a compatibility fix, which will use current plain SQL requests - I don't have time to implement native support for it, sry =/

WARNING: BACKUP YOUR DATABASES!!!! And I would greatly appreciate if you can test this patch with mtmaps installed ;)

repo: https://github.com/Ambal/mangos/tree/prepared_statements

update #1: PostgreSQL support is added. Major code refactoring.

update #2: ODBC-style API for parameter binding. Fix reported compilation issue. Support rev [11235].

update #3: More item queries are using prepared statements. Compile errors on *nix are resolved with kero99 help.

update #4: Fix broken SQL syntax for some prepared statements. Thanks to Undergarun for reporting.

update #5: CRITICAL - rewrite some code due to serious issues with function static variable initialization in multithreaded environment. Thanks to Vinolentus. Expect slight increase in CPU usage.

update #6: resolve issues caused by improper usage of static local variables. Should restore CPU usage to normal.

Link to comment
Share on other sites

Hi, everyone.

kero99, thank you for pointing to compilation issue - I'll fix it definitely today.

P.S. I think I'll change API a bit also to make it more ODBC-style compatible aka:

SqlStatement stmt = db.CreateStatement();
stmt.addInt();
stmt.addBool();
...
stmt.Execute();

Exposing function parameters to client as it is done in current code is not very nice idea, IMO...

Cheers :)

Link to comment
Share on other sites

does this patch work like an anticheat?

No,it is not an anticheat - this patch is intended to speed up DB interaction. Current SQL requests in mangos are already protected from SQL injection attacks, prepared statements simply do not require any additional code to do this.

Link to comment
Share on other sites

CRITICAL: please, update to the latest patch version due to found critical issues with function static variable initialization in multi-threaded environment! And report by how much your server CPU usage has increased (shouldn't be more than 5-6%).

Link to comment
Share on other sites

Some information about prepared statements tests:

PlayerSave.Interval = 300000 (Save characters every 5 minutes)

PlayerSave.Stats.MinLevel = 0 (No stats storage)

Query Cache Hit rate = 0 (MySQL Query Cache disabled, Tweaking query_cache_size and query_cache_limit in your my.cnf should improve Query Cache Hit rate)

World of Warcraft Armory by Shadez (Increase MySQL workload seriously and the armory_characters_stats is wasting resources so it should be improved in other way because the blob is really heavy and bad way.)

MySQL SERVER Htop

25kmysqlworkload.png

3k+ ppl MySQL dedicated server workload

Best Regards to everyone and special thanks to Ambal for its georgeous work.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use