in reply to
How to increase the efficiency of the code wen working with DB?
My first reaction is try with GROUP BY instead of DISTINCT - it gives many RDBMSs the chance to use a faster internal algorithm. Also it doesn't look so much like you're doing it on the face of it, but just in case: avoid reconnecting to the database between queries - maintain a single DBI database handle unless you are doing parallel processing. Consider using multiple database handles and forks (updated and see also Parallel::ForkManager) if the opportunity for parallel processing exists to save elapsed time, especially if there are many such iterations to parallelise.