segmentio / ksuid

K-Sortable Globally Unique IDs
https://segment.com/blog/a-brief-history-of-the-uuid/
MIT License
4.82k stars 174 forks source link

How it works with MySQL #41

Closed shwetasynoverge closed 5 years ago

shwetasynoverge commented 5 years ago

I want to know how we can use KSUID with MySQL ? Also if yes how indexing would work

achille-roussel commented 5 years ago

We've used KSUIDs as primary keys in MySQL database, ether using their base62 representation or their binary format. The column type should be varbinary(27) or varbinary(20) depending on the approach you take. The advantage of using the base62 representation is it enables copy/pasting values when composing queries manually.

Let me know if that helps answer your question.

shwetasynoverge commented 5 years ago

Thanks for your prompt reply. And can you please explain me that how indexing for ksuid works in MySQL?

shwetasynoverge commented 5 years ago

Also one more question : Is it possible to generate KSUID directly through MYSQL ? like for example we have "SELECT UUID()" ?

achille-roussel commented 5 years ago

It'll work just like indexing other byte sequences, a lexicographic comparison will work just fine with KSUID (in both base62 and binary representations).

I'm not aware of any MySQL function that generate a KSUID, we generate them client-side when constructing queries.

shwetasynoverge commented 5 years ago

Thanks a lot