Sometimes, if we don't have a right tool, no matter how efficient we try to become it may not be possible.
I would seriously consider a well developed RDBMS (like oracle). They have invested several thousand man years of research to optimize joins and sorts and may not be worth to reinvent the wheel.
There are specific perl
books
written on this subject. (I have not read the book, so can't comment)