One thing to consider is focusing on index optimization for both id_planoconta and id_registro. If id_planoconta is a foreign key and id_registro is the primary key in table2, creating a composite index on these fields could potentially speed up your query significantly. This would help improve performance, especially when counting records in table2.