17

Jan

Filed in Code, Curse, How-To's, MySQL |

We ran across a fun gotcha today while performance testing some code. The issue was originally discovered when we noticed a 300ms query taking 20 seconds. After about an hour of debugging we found the issue to be with aliases in table locks.

Here’s what our original SQL looked like:

LOCK TABLES files_versiondownloadcount WRITE;
 
UPDATE files_version AS version, files_versiondownloadcount AS count
SET version.downloads = version.downloads + count.downloads, count.downloads = 0
WHERE version.id = count.version_id;
 
UNLOCK TABLES;

Our first idea was to add locking into the files_version table as well. Doing this caused MySQL to error due to syntax. So we then added the aliases to both tables. Voila, back to a 300ms query.

We run one more queryset almost identical to this, and found out that mislabeling the table alias in the lock (not using the same alias in the query) caused the same performance impact we had seen originally, with the 20s query.

Here is the SQL with the updated LOCK statement:

LOCK TABLES files_versiondownloadcount as count WRITE,
files_version as version WRITE;
 
UPDATE files_version AS version, files_versiondownloadcount AS count
SET version.downloads = version.downloads + count.downloads, count.downloads = 0
WHERE version.id = count.version_id;
 
UNLOCK TABLES;
  • http://www.air-jordan-10.com/ air jordan 10

    Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://spoon8.net/

  • Jade

    Thanks very much ,I am really benefit !good things are meant to be shared! I have recently find an game card site called http://www.r4fords.co.uk/ in where owns the best cards,such as acekard 2i , r4 and r4 sdhc .They are cheaper and various. it also gives you pleasures while making your eye-opening. What’s more, it is very likable! Sharing..

blog comments powered by Disqus