Imho, your idea is good, but, why code it into perl when database engine is able to support it technically? If you use all features of the specific implementation of indexes, you can force the same effect and your sql queries will stay clear...
I know that this theme is off topic and I don't want flame, I only advice to observe and to use features of sql engine before modifying perl code.