title: SQL Query Optimization
body: <p>I am trying to speed up this django app (note: I didn't design this... just stuck maintaining it) and the biggest bottle neck seems to be these queries that are being generated by the admin. We have a content class that 4-5 other sub-classes inherit from and anytime the master list is pulled up in the admin a query like this is generated:</p>
  
  <pre><code>SELECT `content_content`.`id`,
         `content_content`.`issue_id`,
         `content_content`.`slug`,
         `content_content`.`section_id`,
         `content_content`.`priority`,
         `content_content`.`group_id`,
         `content_content`.`rotatable`,
         `content_content`.`pub_status`,
         `content_content`.`created_on`,
         `content_content`.`modified_on`,
         `content_content`.`old_pk`,
         `content_content`.`content_type_id`,
         `content_image`.`content_ptr_id`,
         `content_image`.`caption`,
         `content_image`.`kicker`,
         `content_image`.`pic`,
         `content_image`.`crop_x`,
         `content_image`.`crop_y`,
         `content_image`.`crop_side`,
         `content_issue`.`id`,
         `content_issue`.`special_issue_name`,
         `content_issue`.`web_publish_date`,
         `content_issue`.`issue_date`,
         `content_issue`.`fm_name`,
         `content_issue`.`arts_name`,
         `content_issue`.`comments`,
         `content_section`.`id`,
         `content_section`.`name`,
         `content_section`.`audiodizer_id`
    FROM `content_image`
   INNER
    JOIN `content_content`
      ON `content_image`.`content_ptr_id` = `content_content`.`id`
   INNER
    JOIN `content_issue`
      ON `content_content`.`issue_id` = `content_issue`.`id`
   INNER
    JOIN `content_section`
      ON `content_content`.`section_id` = `content_section`.`id`
   WHERE NOT ( `content_content`.`pub_status` = -1 )
   ORDER BY `content_issue`.`issue_date` DESC LIMIT 30
  </code></pre>
  
  <p>I ran an EXPLAIN on this and got the following:</p>
  
  <pre><code>+----+-------------+-----------------+--------+-------------------------------------------------------------------------------------------------+---------+---------+--------------------------------------+-------+---------------------------------+
  | id | select_type | table           | type   | possible_keys                                                                                   | key     | key_len | ref                                  | rows  | Extra                           |
  +----+-------------+-----------------+--------+-------------------------------------------------------------------------------------------------+---------+---------+--------------------------------------+-------+---------------------------------+
  |  1 | SIMPLE      | content_image   | ALL    | PRIMARY                                                                                         | NULL    | NULL    | NULL                                 | 40499 | Using temporary; Using filesort |
  |  1 | SIMPLE      | content_content | eq_ref | PRIMARY,issue_id,content_content_issue_id,content_content_section_id,content_content_pub_status | PRIMARY | 4       | content_image.content_ptr_id         |     1 | Using where                     |
  |  1 | SIMPLE      | content_section | eq_ref | PRIMARY                                                                                         | PRIMARY | 4       | content_content.section_id           |     1 |                                 |
  |  1 | SIMPLE      | content_issue   | eq_ref | PRIMARY                                                                                         | PRIMARY | 4       | content_content.issue_id             |     1 |                                 |
  +----+-------------+-----------------+--------+-------------------------------------------------------------------------------------------------+---------+---------+--------------------------------------+-------+---------------------------------+
  </code></pre>
  
  <p>Now, from what I've read, I need to somehow figure out how to make the query to content_image not be terrible; however, I'm drawing a blank on where to start.  </p>