Need a row count after SELECT statement: what's the optimal SQL approach?

后端 未结 10 1093
悲哀的现实
悲哀的现实 2020-12-05 02:19

I\'m trying to select a column from a single table (no joins) and I need the count of the number of rows, ideally before I begin retrieving the rows. I have come to two app

10条回答
  •  悲哀的现实
    2020-12-05 02:46

    Here are some ideas:

    • Go with Approach #1 and resize the array to hold additional results or use a type that automatically resizes as neccessary (you don't mention what language you are using so I can't be more specific).
    • You could execute both statements in Approach #1 within a transaction to guarantee the counts are the same both times if your database supports this.
    • I'm not sure what you are doing with the data but if it is possible to process the results without storing all of them first this might be the best method.

提交回复
热议问题