Why SELECT * is always not a right choice?

SELECT * might be the most famous (or used) mysql/postgres command out there. But is it that simple under the hood?

Test on not-so-big table between SELECT and SELECT COUNT( *)

Consider a table person having 627 rows (quite small, eh?)

Let's test the time taken (planning and execution)

  • SELECT *

    The planning time is the time it takes for the planner/optimizer to create an optimal execution plan for a query. This might look small but using same for joins might affect the performance as well.

  • SELECT COUNT(*)

    See the drop in timing? Now consider a case where we just wanted to know whether there exist a person from country "India" or "China" in person table and you went on using SELECT *

    Thus, if you're working on some very big database then going from just SELECT * to SELECT COUNT(*) might improve your performance on a very large scale