NedStarkOfWinterfell
NedStarkOfWinterfell

Reputation: 5203

Using custom sort function in MySQL

MySQL uses quicksort to sort the result-set when the user asks for it. Now on average, quicksort has an efficiency of O(Nlog N), which is acceptable (even though its worst case may sometimes reach O(N^2). Now that is fine for most cases, but imagine I have a column, say, pin-number, which always has 6 digits. And a particular query fetches millions of rows and sort them based on that key. In such cases, won't the radix-sort be a better option, giving a linear order? Is there any way (perhaps writing a plugin or something) I can introduce a new MySQL function, say myorderby, which will sort the result-set based on a given key through the custom radix-sort I have defined? And secondly, will this tweak be worthwhile?

Upvotes: 0

Views: 402

Answers (1)

GolezTrol
GolezTrol

Reputation: 116160

You could grab the MySQL source and inject your own sorting function. If it is indeed faster, you could even commit it to the community.

If it's worth it, depends on the amount of effort it takes. I think it's quite some work to get MySQL running with such a modification, and you'll want to be able to update easily too. So unless you really need the speed gain and/or you are able to make your sorting the default for future versions, I think it isn't worth it. I've never experienced the sorting to be the bottleneck.

Upvotes: 1

Related Questions