We have a PosgtreSQL DB table with lots of data. I wonder what type of query is faster / has a better performance and why?
select * from tableselect count (*) from table
While both queries do iterate over the entire table, the first query will perform generally much worse at an application level versus the second one. This is because the select * query will need to send the entire table across the network to the application which is executing the query. The count(*) query, on the other hand, only needs to send across a single integer count.
ROW_COUNTvariable. In the first case the app can read the row count associated with the result, in the second it can read the result. If you need both the count and the actual data, the first query can get them both at once. If you only need the row count, the contents of the table would just waste IO, weigh and slow things down, socount(*)is enough.countat first and thenselectis better than doingselectat the beggining. If theselecthas a worse performance, I'll usecount