How can I speed up my MySQL UUID v4 stored function? -



How can I speed up my MySQL UUID v4 stored function? -

i'm attempting write mysql stored function generate v4 uuids described in rfc 4122's section 4.4 ( http://www.ietf.org/rfc/rfc4122.txt ). initial naive effort after few tweaks following:

create function uuid_v4() returns binary(16) reads sql info begin set @uuid = concat( lpad( hex( floor( rand() * 4294967296 ) ), 8, '0' ), lpad( hex( floor( rand() * 4294967296 ) ), 8, '0' ), lpad( hex( floor( rand() * 4294967296 ) ), 8, '0' ), lpad( hex( floor( rand() * 4294967296 ) ), 8, '0' ) ); set @uuid = concat( substr( @uuid 1 12 ), '4', substr( @uuid 14 3 ), substr( 'ab89' floor( 1 + rand() * 4 ) 1 ), substr( @uuid 18 ) ); homecoming unhex(@uuid); end

the above function quite slow: 100 times slower built-in uuid(), according mysql's benchmark() feature. short of writing udf using mysql's c api, there improvements can create here to, say, shave off order of magnitude runtime?

if there existing, well-regarded uuid udf or stored procedure, i'd happy hear that, too.

i didn't test correctness or performance. thought of doing 1 concatenation in instead of two.

create function uuid_v4() returns binary(16) begin set @h1 = lpad(hex(floor(rand() * 4294967296)), 8, '0'); set @h2 = lpad(hex(floor(rand() * 4294967296)), 8, '0'); set @h3 = lpad(hex(floor(rand() * 4294967296)), 8, '0'); set @h4 = lpad(hex(floor(rand() * 4294967296)), 8, '0'); set @uuid = concat( @h1, substr(@h2 1 4), '4', substr(@h2 6), substr('ab89' floor(1 + rand() * 4) 1 ), substr(@h3 2), @h4 ); homecoming unhex(@uuid); end ;

also why utilize reads sql data in function?

mysql stored-procedures uuid

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -