I'm having a hard time following your code since I have virtually nil experience of C/C++. Nonetheless I think I get the idea. Is your approach executing each SQL statement ('fully') twice thus doubling runtime? ('fully' i.e. without LIMITing the number of records returned). This grabbed my attention: > printf ("Rows retrieved: %d\n", ctx->count); Is the value of <i>ctx</i> on reaching this statement always 1 or is it the number of rows returned by the SQL statement? I didn't think there was a way of retrieving the number of rows in a result except by reiteration. Looks like >ctx = sps_execute2 (sps, "emp-by-last","%son"); is returning the row count with <i>sps_execute2</i> reiterating the result. Is that correct?