I'm trying to improve a (not so much) simple query:
Something like:
SELECT a.*,
(SELECT Count(*)
FROM table_c c
WHERE c.a_id = a.id) AS counter,
b.*
FROM table_a a
LEFT JOIN table_b b
ON b.a_id = a.id
This works, ok, but in reality, I'm just making 2 queries and I need to improve this so it only do one (if, its even possible).
Anyone knows how can I achive that?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `es_temp1` ADD INDEX `es_temp1_idx_a_id` (`a_id`);
ALTER TABLE `table_b` ADD INDEX `table_b_idx_a_id` (`a_id`);
SELECT
a.*,
es_temp1.counter,
b.*
FROM
table_a a
LEFT JOIN
table_b b
ON b.a_id = a.id
LEFT JOIN
es_temp1
ON es_temp1.a_id = a.id