Mysql Expert

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

This week we’ve been having a pretty big problem with one of the projects I’m working on. We’ve been getting so many hits on the server that it’s being overloaded. Not a bad problem to have. However, I didn’t think the server should be overloaded so Dan and I looked at the problem for quite a while and had basically narrowed it down to one mysql query. We weren’t sure this was the problem, but everything else we had tried had failed and we thought this was the problem.

We had a query that was consistently showing up in the mysql slow-queries log that didn’t seem like it should be slow. The query was:

SELECT keywordId, keyword FROM Keywords ORDER BY keywordId LIMIT 800, 10

We wanted to pull out 10 rows starting from row 800 (the 800 number changed with every query and ranged anywhere from 10 to 4,000). keywordId is a primary key so it has an index on it. For it to be a slow query didn’t make sense to me.

After exhausting all my resources, I asked Neal to put me in contact with Sacha Pachev, a mysql expert. He worked for mysql for a while and did some core development on the database. When I called him he knew immediatly what the problem was and explained it to me very well.

It turns out that when mysql has a LIMIT clause where you specify which row to start with, it goes through each row of the table before getting to the starting row. I’m not sure why (I think it may have something to do with the order clause and that mysql has to order the table before it finds the starting row), but I know that it was certainly slowing down our server. On queries where the starting row was around 3,000 it was taking over 700 seconds to execute the query. I don’t know why…but it was slow.

Either way, I have to totally thank Sasha Pachev for helping me out. That guy’s a genious.

29 comments on this post.
  1. Alegro:

    ----

  2. Alegro:

    ----

  3. Ervin Haagensen:

    ----

  4. Perry Terwilliger:

    ----

  5. Frieda Phebus:

    ----

  6. Nana Headman:

    ----

  7. Freda Zanueta:

    ----

  8. Casimira Piedrahita:

    ----

  9. Frankie Sheesley:

    ----

  10. Lajuana Gouthier:

    ----

  11. Cathleen Hermie:

    ----

  12. Alpha Ager:

    ----

  13. Jacqualine Pfieffer:

    ----

  14. Alysa Mccabe:

    ----

  15. Loyce Laclair:

    ----

  16. Shaunta Quinoes:

    ----

  17. Nola Fennessy:

    ----

  18. Mina Trana:

    ----

  19. Darby Vanruiten:

    ----

  20. Hydraulika siłowa:

    ----

  21. Szybkozłącza:

    ----

  22. Gladis Bloomstrom:

    ----

  23. Georgiann Gobble:

    ----

  24. Mercy Torok:

    ----

  25. Hilma Cilenti:

    ----

  26. Dong Ripa:

    ----

  27. Vonnie Buckreis:

    ----

  28. Allen Stawarz:

    ----

  29. Nicky Macneal:

    ----

Leave a comment






− one = 3