Question

I am having trouble using a derived table in MySQL. Does using a derived table inherently slow down the processing of a query?

Here is the query I am trying to run. It won't execute and just times out.

It does succeed. Really, I have isolated the problem to the last join. When I take out the last join it works fine. But when I add the last join back in it refuses to execute.

SELECT cr.COMMUNICATIONS_ID AS ANSWER_ID, 
       cr.CONSUMER_ID as VIEWER_ID, 
       cr.ACTION_LOG_ID, 
       nc.PARENT_COMMUNICATIONS_ID AS QUESTION_ID, 
       nc.SENDER_CONSUMER_ID AS REPLIER_ID, 
       ces.EXPERT_SCORE AS REPLIER_EXPERTISE, 
       cim.CONSUMER_INTEREST_EXPERT_ID AS DOMAIN
    FROM (SELECT 234 AS CONSUMER_ID, 
    ACTION_LOG_ID, 
    COMMUNICATIONS_ID 
    FROM consumer_action_log 
    WHERE COMM_TYPE_ID=4) AS cr
JOIN network_communications AS nc ON 
cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID
JOIN communication_interest_mapping AS cim ON 
nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID
JOIN consumer_expert_score AS ces ON 
nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
        AND cim.CONSUMER_INTEREST_EXPERT_ID=ces.CONSUMER_EXPERT_ID;
Was it helpful?

Solution

Hope this helps... Here's some mysql CREATE INDEX statements. Basically, if you can add indexes, make sure there's an index that covers each of your columns that connect 2 or more tables.

CREATE INDEX idx_nc
ON network_communications(COMMUNICATIONS_ID);

CREATE INDEX idx_cim
ON communication_interest_mapping(COMMUNICATION_ID);

CREATE INDEX idx_ces
ON consumer_expert_score(CONSUMER_ID, CONSUMER_EXPERT_ID);

Derived tables aren't inherently bad, but in this case (see below) you're pulling all the records from consumer_action_log that have a comm_type_id of 4. There doesn't seem to be a connection back to the other tables. That might be the cause of the sql never returning.

SELECT cr.COMMUNICATIONS_ID, 
           cr.CONSUMER_ID, 
           cr.ACTION_LOG_ID, 
           nc.PARENT_COMMUNICATIONS_ID, 
           nc.SENDER_CONSUMER_ID, 
           ces.EXPERT_SCORE, 
           cim.CONSUMER_INTEREST_EXPERT_ID

    FROM (SELECT 234 AS CONSUMER_ID, 
             ACTION_LOG_ID, 
             COMMUNICATIONS_ID 
             FROM consumer_action_log 
             WHERE COMM_TYPE_ID=4) AS cr

JOIN network_communications AS nc ON 
         cr.COMMUNICATIONS_ID=nc.COMMUNICATIONS_ID

JOIN communication_interest_mapping AS cim ON 
         nc.PARENT_COMMUNICATIONS_ID=cim.COMMUNICATION_ID

JOIN consumer_expert_score AS ces ON 
         nc.SENDER_CONSUMER_ID=ces.CONSUMER_ID
        AND cim.CONSUMER_INTEREST_EXPERT_ID=ces.CONSUMER_EXPERT_ID;

OTHER TIPS

Aside from the indexes that should exist on your lookup tables as pointed out in anser by John, I would ensure you have an index on COMM_TYPE_ID in your consumer_action_log table too.

Then, add one keyword to your clause... I've always seen great results when a query is well organized instead of relying on the query engine to optimze...see another sample here

SELECT STRAIGHT_JOIN
         cr.COMMUNICATIONS_ID AS ANSWER_ID,
         cr.CONSUMER_ID as VIEWER_ID,  
         etc... rest of your query...

It might be the optimizer is trying to look at other tables to figure out what to get. See comments in other StackOverflow answer I've provided link to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top