I have customers and visits table, and I would like to know which of these two queries has the better performance: (I have indexes defined for those columns)
Query 1
SELECT
customers.id as id,
COALESCE(v.count, 0) as visits
FROM
customers
LEFT OUTER JOIN (
SELECT customer_id, count(*)
FROM visits
GROUP BY customer_id
) as v on visits.customer_id = customers.id
EXPLAIN ANALYZE Result
Query 2
SELECT
customers.id as id,
(
SELECT count(*)
FROM visits
WHERE
visits.customer_id=customers.id
) as visits
FROM
customers
EXPLAIN ANALYZE Result
As you see in the above examples the second query has a lower cost, but the excecution time is higher than the first query.
I'm quite confused about it. I assume that depends on the filter options. Can you help me to understand that, and if you have a better query, please let me know.
explain(analyze, verbose, buffers, settings)
for your SQL statements? (all in plain text as an update to your question)