How would you optimize a database query or a Linux shell script to process millions of rows of transaction data?