Home  >  Q&A  >  body text

mysql优化 - MySQL中使用UNION进行两表合并,去重导致效率低下,请问如何优化?

如下SQL语句在MySQL中执行需要2秒左右的时间,如果使用UNION ALL进行不去重合并只需要0.4秒,UNION ALL配合DISTINCT来去重速度又变成2秒了,请问如何进行优化?

SELECT
    a. KEY,
    a. DATA,
    a.date_added
FROM
    (
        (
            SELECT
                CONCAT('customer_', ca. KEY) AS `key`,
                ca. DATA,
                ca.date_added
            FROM
                `cf_customer_activity` ca
        )
        UNION
            (
                SELECT
                    CONCAT('affiliate_', aa. KEY) AS `key`,
                    aa. DATA,
                    aa.date_added
                FROM
                    `cf_affiliate_activity` aa
            )
    ) a
ORDER BY
    a.date_added DESC
LIMIT 0,
 5;

大家讲道理大家讲道理2743 days ago758

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 14:50:54

    See if the key prefixes of the statements you write are different, there will never be duplication. Just union all

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 14:50:54

    Of course, use QEP first

    mysql> explain select * from wp_options limit 1\G;
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: wp_options
       partitions: NULL
             type: ALL
    possible_keys: NULL
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 136
         filtered: 100.00
            Extra: NULL
    1 row in set, 1 warning (0.01 sec)
    
    mysql> show session status like 'Handler_read%';
    +-----------------------+-------+
    | Variable_name         | Value |
    +-----------------------+-------+
    | Handler_read_first    | 3     |
    | Handler_read_key      | 3     |
    | Handler_read_last     | 0     |
    | Handler_read_next     | 0     |
    | Handler_read_prev     | 0     |
    | Handler_read_rnd      | 0     |
    | Handler_read_rnd_next | 209   |
    +-----------------------+-------+
    7 rows in set (0.01 sec)

    As for the specific meaning of each field, check the information yourself.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 14:50:54

    @prolifes is right. Maybe the questioner didn't express it clearly.

    I will add another idea, because you only need 5 pieces of data in the end, take 5 pieces from ca, and then take 5 pieces from aa, for a total of 10 pieces of data to sort, this will be very fast.

    SELECT
        a. KEY,
        a. DATA,
        a.date_added
    FROM
        (
            (
                SELECT
                    CONCAT('customer_', ca. KEY) AS `key`,
                    ca. DATA,
                    ca.date_added
                FROM
                    `cf_customer_activity` ca
                ORDER BY 
                    a.date_added DESC /* 如果date_added有索引的话,这个会比较快 */ 
                LIMIT 0,5
            )
            UNION ALL
                (
                    SELECT
                        CONCAT('affiliate_', aa. KEY) AS `key`,
                        aa. DATA,
                        aa.date_added
                    FROM
                        `cf_affiliate_activity` aa
                    ORDER BY 
                        a.date_added DESC /* 如果date_added有索引的话,这个会比较快 */ 
                    LIMIT 0,5
                )
        ) a
    ORDER BY
        a.date_added DESC
    LIMIT 0, 5;

    reply
    0
  • Cancelreply