Select query taking too long MYSQL 5.6.17 I have the below select and can't understand why it's taking about an hour to give me the result.

SELECT
(CASE
    WHEN a.ACODE='000A' THEN c.datfinprel 
    WHEN a.ACODE='000T' THEN c.datiniprel 
    WHEN a.ACODE='000S' &amp;&amp; a.ACODETP=1 THEN c.datfinprel 
    WHEN a.ACODE='000S' &amp;&amp; a.ACODETP=2 THEN c.datiniprel 
END) AS DATA_PREL,

A.`date_doc`, DATE(A.`created`) AS created, A.`stagione`, A.`fabbisogno`, A.`modello`, A.`commessa`,A.`code`, A.`title`, A.`color`, A.`measure`, A.`alt_pz`, A.`dist_base`, A.`qnt`,B.price, A.`cause`
FROM 
    wb_mod_deposit_out A, wb_mod_deposit B, from_generale_java C
WHERE 
    A.`stagione` IN (SELECT DISTINCT STAGIONE FROM wb_mod_deposit_out WHERE CREATED BETWEEN '2017-12-11' AND '2017-12-17')
AND B.ID=A.`deposit_id` 
AND a.`code`=B.`code` 
AND a.`color`=B.`color`
AND a.`stagione`=c.`stagione` 
AND a.`modello`=c.`modello` 
AND TRIM(LEADING '0' FROM a.`commessa`) = TRIM(LEADING '0' FROM c.`commessa`)
AND a.`type`=1;


The extended explain:

id  select_type   table               type    possible_keys                                   key           key_len  ref                    rows  filtered  Extra                               
 1  SIMPLE        C                   index   stagione,generale                               generale      123      (NULL)                 3729    100.00  Using where; Using index            
 1  SIMPLE        A                   ref     stagione,test_OUT,CODE+COLOR,modello+comm+stag  stagione      153      csm_db.C.stagione       261    100.00  Using index condition; Using where  
 1  SIMPLE        &lt;subquery2&gt;         eq_ref  &lt;auto_key&gt;                                      &lt;auto_key&gt;    153      csm_db.A.stagione         1    100.00  (NULL)                              
 1  SIMPLE        B                   eq_ref  PRIMARY,test_depo                               PRIMARY       4        csm_db.A.deposit_id       1    100.00  Using where                         
 2  MATERIALIZED  wb_mod_deposit_out  range   stagione,modello+comm+stag,date_created         date_created  6        (NULL)                15245    100.00  Using index condition; Using MRR
You could try doing your Case selects as a cte. 
The  line: A.'stagione' IN (SELECT DISTINCT STAGIONE FROM wb_mod_deposit_out WHERE CREATED BETWEEN '2017-12-11' AND '2017-12-17')


will also be a memory hog. Do you have an index on your CREATED column?


Also, if you can find a way to take out TRIM(LEADING '0' FROM a.'commessa') = TRIM(LEADING '0' FROM c.'commessa') - maybe do it in an update statement or a third cte.