How can I speed up MySQL query with multiple joins

前端 未结 7 1472
再見小時候
再見小時候 2021-02-07 11:25

Here is my issue, I am selecting and doing multiple joins to get the correct items...it pulls in a fair amount of rows, above 100,000. This query takes more than 5mins when the

相关标签:
7条回答
  • 2021-02-07 12:04

    Try adding indexes on the fields that you join. It may or may not improve the performance.

    Moreover it also depends on the engine that you are using. If you are using InnoDB check your configuration params. I had faced a similar problem, as the default configuration of innodb wont scale much as myisam's default configuration.

    0 讨论(0)
  • 2021-02-07 12:04

    It sounds like you should think about delivering subsets (paging) or limit the results some other way unless there is a reason that the users need every row possible all at once. Typically 100K rows is more than the average person can digest.

    0 讨论(0)
  • 2021-02-07 12:05

    As everyone says, make sure you have indexes.

    You can also check if your server is set up properly so it can contain more of, of maybe the entire, dataset in memory.

    Without an EXPLAIN, there's not much to work by. Also keep in mind that MySQL will look at your JOIN, and iterate through all possible solutions before executing the query, which can take time. Once you have the optimal JOIN order from the EXPLAIN, you could try and force this order in your query, eliminating this step from the optimizer.

    0 讨论(0)
  • 2021-02-07 12:07

    Make sure your date columns and all the columns you are joining on are indexed.

    Doing an unequivalence operator on your dates means it checks every row, which is inherently slower than an equivalence.

    Also, using DISTINCT adds an extra comparison to the logic that your optimizer is running behind the scenes. Eliminate that if possible.

    0 讨论(0)
  • 2021-02-07 12:21

    At present, your query is returning all matching rows on table2-table5, just to establish whether t5.store = 2. If any of table2-table5 have a significantly higher row count than table1, this may be greatly increasing the number of rows processed - consequently, the following query may perform significantly better:

    SELECT DISTINCT t1.first_name, t1.last_name, t1.email 
    FROM table1 AS t1 
    WHERE t1.subscribe =1 
    AND t1.Cdate >= $startDate
    AND t1.Cdate <= $endDate
    AND EXISTS
    (SELECT NULL FROM table2 AS t2
    INNER JOIN table3 AS t3 ON t2.O_ref = t3.I_oref 
    INNER JOIN table4 AS t4 ON t3.I_pid = t4.P_id 
    INNER JOIN table5 AS t5 ON t4.P_cat = t5.C_id AND t5.store =2
    WHERE t1.CU_id = t2.O_cid);
    
    0 讨论(0)
  • 2021-02-07 12:22

    Well, first, make a subquery to decimate table1 down to just the records you actually want to go to all the trouble of joining...

    SELECT DISTINCT t1.first_name, t1.last_name, t1.email  
    FROM (  
    SELECT first_name, last_name, email, CU_id FROM table1 WHERE  
    table1.subscribe = 1  
    AND table1.Cdate >= $startDate  
    AND table1.Cdate <= $endDate  
    ) AS t1  
    INNER JOIN table2 AS t2 ON t1.CU_id = t2.O_cid  
    INNER JOIN table3 AS t3 ON t2.O_ref = t3.I_oref  
    INNER JOIN table4 AS t4 ON t3.I_pid = t4.P_id  
    INNER JOIN table5 AS t5 ON t4.P_cat = t5.C_id  
    WHERE t5.store = 2
    

    Then start looking at modifying the directionality of the joins.

    Additionally, if t5.store is only very rarely 2, then flip this idea around: construct the t5 subquery, then join it back and back and back.

    0 讨论(0)
提交回复
热议问题