I tested it on my collection and it took like 38 seconds in both cases.
I'll guess it's correlated to the size of your collection.

But using the code in the first post it took like two hours!

Could be a good thing to change the code there trixi, since it actually gives the wrong result and a better code exists!
Even faster sql? I'll see what I can do...

/Bex