Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a SELECT query begin to return rows?

Suppose the following query:

SELECT * FROM table;

Will the DBMS give me the first row as soon as it fetched it or will it first fetch all the rows (save them in some kind of buffer) and then give me all the rows at once?

If my question was not clear. Suppose that the amount of rows in the table is such that the DBMS will take exactly 60 minutes to fetch all the rows. Will the DBMS return the rows progressively through the 60 minutes, or will I have to wait 60 minutes before receiving any data?

like image 906
André Puel Avatar asked Aug 04 '13 17:08

André Puel


People also ask

What does a SELECT query return?

A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data manipulation language (DML) command. As SQL is a declarative programming language, SELECT queries specify a result set, but do not specify how to calculate it.

Will the SELECT statement return rows from a table?

The SELECT statement is used to select or retrieve the data from one or more tables. You can use this statement to retrieve all the rows from a table in one go, as well as to retrieve only those rows that satisfy a certain condition or a combination of conditions.

Which query will return all the rows?

Answer: SELECT query is used to retrieve data from a table.

Which method returns the first row of data returned by an SQL query?

The first () function is used to return the first row of any table.


1 Answers

In PostgreSQL, the server will indeed return rows to the client as soon as they are available if the query execution plan permits it. This is the case in your simple example. In other cases, if you might have a sort at the end, for example, and will have to wait for that to finish.

But if you use the standard libpq interface, the client library will build up the entire result in memory before it returns it to the client program. To get the results row by row, you need to use the single-row mode in libpq. If you use other interfaces or other languages, results might vary.

like image 128
Peter Eisentraut Avatar answered Oct 05 '22 22:10

Peter Eisentraut