FROM table1 t LEFT JOIN ( SELECT regcert_id, count(1) AS num FROM table2 GROUP BY regcert_id ) t1 ON t1.table1_id = t.id
After remodeling
SELECT regcert_id, count(1) AS num FROM table2 GROUP BY regcert_idThe queried data is written to the new table, such as
the optimized statement of table2
FROM table1 t LEFT JOIN table2 t1 ON t1.table1_id = t.id
Note: The premise of this transformation is that the table2 data does not change frequently. If you change it, you need to execute the corresponding script.