Everything was working normally until the 'Estabelecimentos' table in the database was corrupted, and I deleted it and created it again. After that, this simple query started to run very slowly:
the cnpj_inteiro column as well as the id in the 'socios' table are properly indexed. But I can't understand why it's taking so long to load. It's taking about 20 seconds to load. I did a test without using 'with' and it returns quickly, but with it it's slow.
Can anyone tell me what's going on?
note: I've already restarted the server and database and it didn't work.
The only observation is that I did not perform the migration of tables with Laravel. I created them directly and manually, they do not have foreign keys, only the index. But as I said, everything was working fine, until I deleted and inserted the "Estabelecimento" and "Empresas" table again.
I suggest running an EXPLAIN on the query to see what and if it is using indexes, my guess is that it doesn't. This might shed some light on your issue.
After removing it, it went back to normal and the query became fast again. The strange thing is that it already existed before. But after deleting the table and inserting it again, it became slow, and after removing this line, everything went back to normal.
I don't understand why, can anyone give an explanation?
Did u try to set cnpj_inteiro as primary kay in Estabelecimentos table? . Another thing to do, Is search if theres a migration where this tablet was create to validate if your actual config Is the same
my guess is that you are breaking eloquent rather than SQL. If there is no primary key on the related model, its perhaps not keying the related model correctly and having to use where() when joining the collections together.
A danger from straying from accepted conventions is that you can run into edge cases not encountered by most.
Já tentou limpar os caches?
Não sei se isso influencia em projetos em produção, caso não esteja pode executar, se estiver por favor leia a documentação do laravel.
Have you tried clearing the caches?
I don't know if this influences projects in production, if not you can run it, if so please read the Laravel documentation.