(Contractor-supplied code; trying to convince contractor to up his game now. "Joins, m'dear, might want to look into them"... Trouble is, on his little test setup with the web server and database server on the same unloaded machine, it's not such an obvious bottleneck - then when deployed, it's a complete dog.)