Log in

No account? Create an account
entries friends calendar profile My Website Previous Previous Next Next
Mark Atwood
MySQL UUID() function, so fast it makes me nervous
So I was hacking around in the MySQL server code this weekend, and took at look at the implementation of the UUID() function.

Instead of using Theodore Tso uuid library, which now ships by default on all Linux and MacOS machines (I dont know about BSD and Solaris), we implemented our own from scratch. With a global mutex lock on generation, which will utterly blow on a multicore machine.

So I pulled down a branch of the server source code from Launchpad, and then branched it again twice locally. In those two branches, I replaced the implementation with a call to libuuid.so uuid_generate_random() and with a call to libuuid.so uuid_generate_time(). Then I built all three trees, and benchmarked 10 million calls to UUID (SELECT BENCHMARK(10000000,UUID());).

I was surprised at the results.

implementationtime in seconds

How is the builtin one so fast?!

Something is either very wrong with the Tso implementation, that nobody has cared about because they aren't using it for thousands of calls per second, or else something is very wrong with the MySQL internal implementation, and we're not safely generating properly unique UUIDs.

My money is on the second guess...


2 comments or Leave a comment
tanjent From: tanjent Date: July 15th, 2008 01:42 am (UTC) (Link)
~2 seconds * ~3 ghz / 10 million = 600 cycles per uuid.

That's fast but not ridiculous - generating the random 48-bit part of the UUID would only take a few hundred cycles at most even with a strong PRNG.

I don't know anything about what the call tree looks like prior t calling UUID() or the others, but perhaps the others are slow due to the overhead of calling a non-builtin function? Perhaps they're not using a cached copy of the current time and have to call into the kernel or whatnot?
From: xaprb Date: July 15th, 2008 01:43 am (UTC) (Link)


Is Item_cache kicking in here? Is UUID() really being called in each of those iterations? It *shouldn't* be (that would be a bug) but it's suspicious to me.
2 comments or Leave a comment