You are not gaining anything by reading a bunch of data into an in-memory data structure and then writing hard-coded logic (in Perl or in any other language) to do what the database is specifically designed to do. Let your program concentrate on the what of what you want, and the database will take care of how to serve-up to you only those records that are required.