www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need help optimizing a query

  1. #1
    Join Date
    Jun 2007
    Posts
    400

    Need help optimizing a query

    I am trying to run a "simple" report and it is taking about 2 1/2 minutes to run. Here is the query:

    Code:
    SELECT idx.* FROM (SELECT idxrecentacct.socsec FROM idxrecentacct INNER JOIN blfcustunq USING (socsec) INNER JOIN blfacct USING (class, account) WHERE idxrecentacct.company='BLF' UNION SELECT idxrecentacct.socsec FROM idxrecentacct INNER JOIN aaacustunq USING (socsec) INNER JOIN aaaacct USING (class, account) WHERE idxrecentacct.company='AAA' UNION SELECT idxrecentacct.socsec FROM idxrecentacct INNER JOIN crycustunq USING (socsec) INNER JOIN cryacct USING (class, account) WHERE idxrecentacct.company='CRY') idx 
    
    INNER JOIN (SELECT * FROM aaaviewerblfmailmarketing UNION SELECT * FROM aaaviewercrymailmarketing UNION SELECT * FROM aaaviewerallmailmarketing) aaaviewer ON (idx.socsec = aaaviewer.socsec)
    The top half returns about 42,000 rows in about 2 seconds. I am trying to join it to another part that, on its own, returns about 10,000 rows in less than 1 second. Yet when I make the join, it takes 2 1/2 minutes. I'm assuming this is because they are both derived tables and therefore they are not indexed? Is there any way around this?

    I ran an EXPLAIN EXTENDED and this is what I got. Not really sure how to read it:

    Code:
    +----+--------------+---------------------------+--------+-------------------+-------------------+---------+---------------------------------------------------------------------+-------+--------------------------------+
    | id | select_type  | table                     | type   | possible_keys     | key               | key_len | ref                                                                 | rows  | Extra                          |
    +----+--------------+---------------------------+--------+-------------------+-------------------+---------+---------------------------------------------------------------------+-------+--------------------------------+
    |  1 | PRIMARY      | <derived5>                | ALL    | NULL              | NULL              | NULL    | NULL                                                                |  9771 |                                |
    |  1 | PRIMARY      | <derived2>                | ALL    | NULL              | NULL              | NULL    | NULL                                                                | 42088 | Using where                    |
    |  5 | DERIVED      | aaaviewerblfmailmarketing | ALL    | NULL              | NULL              | NULL    | NULL                                                                |  9768 |                                |
    |  6 | UNION        | aaaviewercrymailmarketing | ALL    | NULL              | NULL              | NULL    | NULL                                                                |     3 |                                |
    |  7 | UNION        | NULL                      | NULL   | NULL              | NULL              | NULL    | NULL                                                                |  NULL | no matching row in const table |
    | NULL | UNION RESULT | <union5,6,7>              | ALL    | NULL              | NULL              | NULL    | NULL                                                                |  NULL |                                |
    |  2 | DERIVED      | idxrecentacct             | ALL    | classaccountindex | NULL              | NULL    | NULL                                                                | 42088 | Using where                    |
    |  2 | DERIVED      | blfcustunq                | ref    | socsecindex       | socsecindex       | 12      | web_dev_test.idxrecentacct.socsec                                   |     1 | Using where; Using index       |
    |  2 | DERIVED      | blfacct                   | eq_ref | PRIMARY           | PRIMARY           | 5       | web_dev_test.idxrecentacct.class,web_dev_test.idxrecentacct.account |     1 | Using index                    |
    |  3 | UNION        | idxrecentacct             | ALL    | classaccountindex | NULL              | NULL    | NULL                                                                | 42088 | Using where                    |
    |  3 | UNION        | aaacustunq                | ref    | socsecindex       | socsecindex       | 12      | web_dev_test.idxrecentacct.socsec                                   |     1 | Using where; Using index       |
    |  3 | UNION        | aaaacct                   | eq_ref | PRIMARY           | PRIMARY           | 5       | web_dev_test.idxrecentacct.class,web_dev_test.idxrecentacct.account |     1 | Using index                    |
    |  4 | UNION        | cryacct                   | index  | PRIMARY           | PRIMARY           | 5       | NULL                                                                |     4 | Using index                    |
    |  4 | UNION        | idxrecentacct             | eq_ref | classaccountindex | classaccountindex | 5       | web_dev_test.cryacct.class,web_dev_test.cryacct.account             |     1 | Using where                    |
    |  4 | UNION        | crycustunq                | ref    | socsecindex       | socsecindex       | 12      | web_dev_test.idxrecentacct.socsec                                   |     1 | Using where; Using index       |
    | NULL | UNION RESULT | <union2,3,4>              | ALL    | NULL              | NULL              | NULL    | NULL                                                                |  NULL |                                |
    +----+--------------+---------------------------+--------+-------------------+-------------------+---------+---------------------------------------------------------------------+-------+--------------------------------+

  2. #2
    Join Date
    Apr 2006
    Posts
    7
    Hello,
    Why don't you join each of aaaviewerblfmailmarketing, aaaviewercrymailmarketing and aaaviewerallmailmarketing with 3 selects of the idx select query

    So in the end you'll have 9 select statements with UNION

    But I guess that will be faster and no Table scan will occur if you have appretiate indexes

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles