Yes, it would work better running the simpler select for each stream one at a time -- you only need to prepare the statement once, just bind one stream at a time to get the result. Alternatively you can send the parameters in a carray or in a temp table or something so that the (sub)query finding the max is executed by itself to guarantee that the minmax optimization is applied.