MySQL长查询,选择包含最后一条消息的对话框和每个对话框的用户

时间:2016-03-01 07:45:48

标签: mysql sql indexing

添加更多逻辑以获取匿名消息后,sql开始运行缓慢,因此我为字段添加了复合索引:

alter table dialog add index anonym_ftr1(from_anonym_id, to_anonym_id, removed_uid1);
alter table dialog add index anonym_tfr2(to_anonym_id, from_anonym_id, removed_uid2);

SQL查询如下所示:

SELECT
    dialog.id AS dialog_id,
    users.id AS uid,
    users.purchases,
    name,
    number,
    avatar,
    avatar_date,
    driver,
    vehicle,
    messages.id AS mid,
    uid2,
    mid2,
    messages.msg,
    messages.removed,
    messages.from_anonym_id,
    messages.to_anonym_id,
    (messages.date DIV 1000) AS date,
    from_id = 1 AS outbox,
    (
        SELECT
            COUNT(*)
        FROM
            messages FORCE KEY (idx_ufsr)
        WHERE
            from_id = uid && user_id = 1 && read_state = 0 && removed = 0
    ) AS not_read
FROM
    dialog,
    messages,
    users
WHERE
    messages.id = dialog.mid && users.removed != 1 && (
        (
            uid1 = 1 && users.id = uid2 && removed_uid1 = 0
        ) || (
            users.id = uid1 && uid2 = 1 && removed_uid2 = 0
        )
        OR (
            dialog.from_anonym_id = 3
            AND users.anonym_id = dialog.to_anonym_id && removed_uid1 = 0
        )
        OR (
            dialog.to_anonym_id = 3
            AND users.anonym_id = dialog.from_anonym_id && removed_uid2 = 0
        )
    )
ORDER BY
    dialog.mid DESC
LIMIT 0,21

显示对话框中的索引:

 +--------+------------+----------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table  | Non_unique | Key_name       | Seq_in_index | Column_name    | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +--------+------------+----------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | dialog |          0 | PRIMARY        |            1 | id             | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid1           |            1 | uid1           | A         |      379381 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid1           |            2 | uid2           | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid2           |            1 | uid2           | A         |     1896905 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | idx_mid        |            1 | mid            | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | from_anonym_id |            1 | from_anonym_id | A         |          18 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | to_anonym_id   |            1 | to_anonym_id   | A         |          18 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | mid_uid        |            1 | mid            | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | mid_uid        |            2 | uid2           | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | mid_uid        |            3 | uid1           | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | mid2           |            1 | mid2           | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid_removed    |            1 | uid1           | A         |          18 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid_removed    |            2 | uid2           | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid_removed    |            3 | removed_uid1   | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | uid_removed    |            4 | removed_uid2   | A         |     5690715 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_ftr1    |            1 | from_anonym_id | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_ftr1    |            2 | to_anonym_id   | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_ftr1    |            3 | removed_uid1   | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_tfr2    |            1 | to_anonym_id   | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_tfr2    |            2 | from_anonym_id | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    | dialog |          1 | anonym_tfr2    |            3 | removed_uid2   | A         |         200 |     NULL | NULL   |      | BTREE      |         |               |
    +--------+------------+----------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

解释延伸:

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: dialog
         type: index_merge
possible_keys: uid1,uid2,idx_mid,from_anonym_id,to_anonym_id,mid_uid,uid_removed,anonym_ftr1,anonym_tfr2
          key: uid1,uid2,from_anonym_id,to_anonym_id
      key_len: 4,4,4,4
          ref: NULL
         rows: 50
     filtered: 100.00
        Extra: Using sort_union(uid1,uid2,from_anonym_id,to_anonym_id); Using where; Using temporary; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: users
         type: range
possible_keys: PRIMARY,removed
          key: removed
      key_len: 1
          ref: NULL
         rows: 398596
     filtered: 100.00
        Extra: Using where; Using join buffer
*************************** 3. row ***************************
           id: 1
  select_type: PRIMARY
        table: messages
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: anumbers.dialog.mid
         rows: 1
     filtered: 100.00
        Extra:
*************************** 4. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: messages
         type: ref
possible_keys: idx_ufsr
          key: idx_ufsr
      key_len: 10
          ref: const,func,const,const
         rows: 3
     filtered: 100.00
        Extra: Using index
4 rows in set, 2 warnings (0.00 sec)

我对联盟有一些想法,但是在中途排序会有问题。 此外,可以拆分2个查询并对它们进行排序。

来这里了解一下,如果有可能更快地运行1个查询? 请帮我解决一个问题。真正帮助解决这个问题的人 - 将获得批准并指出等等。

PS抱歉,我不知道如何使用索引调整表格。

0 个答案:

没有答案