Separate generating content from printing it out Ferret diverges from Lucene
Sep 19

Recursion gone wrong OR Too Much SQL

Tech Add comments

One action on the admin side of an application that I am working on was really slow. It would take up to 20 seconds to load.

This action is part of existing legacy software, so again I had to dig in and found the bad performer instantly.

The bad code was building a tree of categories, and worked just fine for tens of categories, or maybe even hundreds, but now the system had THOUSANDS of categories.

As I looked into the code that generated the tree, it didn’t take a genius to work out the issue. The function was recursively calling itself as it got to each node in the tree (now build the subtree, now build its subtree, etc etc).

Recursion itself was not the problem, but each time the function was called it was running a new SQL query against the DB to get subtree information. This meant that many, many queries were running against the DB when one could be used.

By changing the action so that it:

  1. gets all of the category info first
  2. write out the tree using this info
  3. It went subsecond.

    Then we migrated from having it ask for the categories from the DB, to grabbing them from cache, and it became insignificant.

2 Responses to “Recursion gone wrong OR Too Much SQL”

  1. chanel replica handbags Says:


  2. vali Says:

    Elementary … but great tip for those that need it. 10x

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What is the number before 3? (just put in the digit)