I could try to publish and explain the exact request that I am trying to run, but I follow the old saying: "Give a man fish and he will eat during the day, teach a man fish and he will eat for the rest of his life." SQL optimization seems to be very specific to queries, and even if you could solve this one specific query for me, I would need to write many more queries in the future, and I would like to get an education on how indexes work in general.
However, here is a brief description of my current problem. I have a query that joins three tables and works in 0.2 seconds. Awesome. I add the sentence "order by" and works after 4 minutes and 30 seconds. Sucky. I am denormalizing one table, so there is another join, add indexes everywhere, and now the query runs in ... 20 minutes. What the heck? Finally, I do not use the connection at all, but rather a subquery with "where id in (...) order by", and now it works after 1.5 seconds. Pretty decent. What happens by the name of God? It seems to me that if I really understood what indexes were doing, I could write really good SQL.
Does anyone know good lessons? Thank!
source
share