PostgreSQL provides the means to figure out why a certain query is slow. Behind the scenes, PostgreSQL analyzes tables, collects statistics from them, and builds histograms using auto-vacuuming. Auto-vacuuming, in general, is used to recover disk space, update table statistics, and perform other maintenance tasks, such as preventing transaction-ID wraparound. Table statistics allow PostgreSQL to pick up an execution plan with the least cost. The least cost is calculated by taking into account the I/O and, naturally, the CPU cost. Also, PostgreSQL enables users to see the generated execution plan by providing the EXPLAIN command.
For beginners, it's extremely useful to write the same query in different ways and compare the results. For example, in some cases, the NOT IN construct can be converted to LEFT JOIN or NOT EXIST. Also, the IN construct...