How long does it take to run those queries in a shell, i.e. not through your Perl module? I think the issue is likely to be one of the database itself and not one of your client script. First and foremost I would check indices of the relevant tables in that schema. If the query can not be run efficiently because no suitable index can be found then it's going to take forever.