3

我正在使用 MySQL5,我目前有一个查询可以获取我需要的信息,但我觉得它可以在性能方面得到改进。

这是我构建的查询(大致遵循本指南):

SELECT d.*, dc.date_change, dc.cwd, h.name as hub
FROM livedata_dom AS d
      LEFT JOIN ( SELECT dc1.*
        FROM livedata_domcabling as dc1
        LEFT JOIN livedata_domcabling AS dc2
        ON dc1.dom_id = dc2.dom_id AND dc1.date_change < dc2.date_change
        WHERE dc2.dom_id IS NULL
        ORDER BY dc1.date_change desc) AS dc ON (d.id = dc.dom_id)
      LEFT JOIN livedata_hub AS h ON (d.id = dc.dom_id AND dc.hub_id = h.id)
WHERE d.cluster = 'localhost'
GROUP BY d.id;

编辑:使用 ORDER BY + GROUP BY 来避免获得多个 dom 条目,以防“domcabling”有一个带有空 date_change 的条目和另一个带有相同“dom”日期的条目。

我觉得我正在用火箭筒杀死一只老鼠。此查询耗时 3 秒以上,“livedata_dom”和“livedata_domcabling”中只有大约 5k 个条目。此外,EXPLAIN 告诉我使用了 2 个文件排序:

+----+-------------+------------+--------+-----------------------------+-----------------------------+---------+-----------------+------+----------------------------------------------+
| id | select_type | table      | type   | possible_keys               | key                         | key_len | ref             | rows | Extra                                        |
+----+-------------+------------+--------+-----------------------------+-----------------------------+---------+-----------------+------+----------------------------------------------+
|  1 | PRIMARY     | d          | ALL    | NULL                        | NULL                        | NULL    | NULL            |    3 | Using where; Using temporary; Using filesort |
|  1 | PRIMARY     | <derived2> | ALL    | NULL                        | NULL                        | NULL    | NULL            |    3 |                                              |
|  1 | PRIMARY     | h          | eq_ref | PRIMARY                     | PRIMARY                     | 4       | dc.hub_id       |    1 |                                              |
|  2 | DERIVED     | dc1        | ALL    | NULL                        | NULL                        | NULL    | NULL            |    4 | Using filesort                               |
|  2 | DERIVED     | dc2        | ref    | livedata_domcabling_dc592d9 | livedata_domcabling_dc592d9 | 4       | live.dc1.dom_id |    2 | Using where; Not exists                      |
+----+-------------+------------+--------+-----------------------------+-----------------------------+---------+-----------------+------+----------------------------------------------+ 

如何更改此查询以使其更有效?

使用虚拟数据(如下提供),这是预期的结果:

+-----+-------+---------+--------+----------+------------+-----------+---------------------+------+-----------+
| id  | mb_id | prod_id | string | position | name       | cluster   | date_change         | cwd  | hub       |
+-----+-------+---------+--------+----------+------------+-----------+---------------------+------+-----------+
| 249 | 47    | 47      |     47 |       47 | SuperDOM47 | localhost | NULL                | NULL | NULL      |
| 250 | 48    | 48      |     48 |       48 | SuperDOM48 | localhost | 2014-04-16 05:23:00 | 32A  | megahub01 |
| 251 | 49    | 49      |     49 |       49 | SuperDOM49 | localhost | NULL                | 22B  | megahub01 |
+-----+-------+---------+--------+----------+------------+-----------+---------------------+------+-----------+

基本上,每个“dom”条目都需要 1 行,其中

  1. 具有最高 date_change 的“domcabling”记录
    • 如果记录不存在,我需要空字段
    • 每个 dom 一个条目可能有一个空的 date_change 字段(空的日期时间字段被认为比任何其他日期时间都早)
  2. '集线器'的名称,当找到'domcabling'条目时,否则为null

为 3 个表创建 TABLE + dummy INSERT:

livedata_dom(大约 5000 个条目)

CREATE TABLE `livedata_dom` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mb_id` varchar(12) NOT NULL,
  `prod_id` varchar(8) NOT NULL,
  `string` int(11) NOT NULL,
  `position` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  `cluster` varchar(9) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mb_id` (`mb_id`),
  UNIQUE KEY `prod_id` (`prod_id`),
  UNIQUE KEY `name` (`name`),
  UNIQUE KEY `livedata_domgood_string_7bff074107b0e5a0_uniq` (`string`,`position`,`cluster`)
) ENGINE=InnoDB AUTO_INCREMENT=5485 DEFAULT CHARSET=latin1;

INSERT INTO `livedata_dom` VALUES (251,'49','49',49,49,'SuperDOM49','localhost'),(250,'48','48',48,48,'SuperDOM48','localhost'),(249,'47','47',47,47,'SuperDOM47','localhost');

livedata_domcabling(大约 10000 个条目并且增长缓慢)

CREATE TABLE `livedata_domcabling` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dom_id` int(11) NOT NULL,
  `hub_id` int(11) NOT NULL,
  `cwd` varchar(3) NOT NULL,
  `date_change` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `livedata_domcabling_dc592d9` (`dom_id`),
  KEY `livedata_domcabling_4366aa6e` (`hub_id`),
  CONSTRAINT `dom_id_refs_id_73e89ce0c50bf0a6` FOREIGN KEY (`dom_id`) REFERENCES `livedata_dom` (`id`),
  CONSTRAINT `hub_id_refs_id_179c89d8bfd74cdf` FOREIGN KEY (`hub_id`) REFERENCES `livedata_hub` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5397 DEFAULT CHARSET=latin1;

INSERT INTO `livedata_domcabling` VALUES (1,251,1,'22B',NULL),(2,250,1,'33A',NULL),(6,250,1,'32A','2014-04-16 05:23:00'),(5,250,1,'22B','2013-05-22 00:00:00');

livedata_hub(大约 100 个条目)

CREATE TABLE `livedata_hub` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(14) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=latin;

INSERT INTO `livedata_hub` VALUES (1,'megahub01');
4

2 回答 2

3

尝试这种重写(在SQL-Fiddle中测试:

SELECT 
    d.*, dc.date_change, dc.cwd, h.name as hub
FROM 
    livedata_dom AS d
  LEFT JOIN 
    livedata_domcabling as dc
        ON dc.id =
           ( SELECT id
             FROM livedata_domcabling AS dcc
             WHERE dcc.dom_id = d.id 
             ORDER BY date_change DESC 
               LIMIT 1
          ) 
  LEFT JOIN 
    livedata_hub AS h 
        ON dc.hub_id = h.id
  WHERE 
     d.cluster = 'localhost' ;

并且索引(dom_id, date_change)将有助于提高效率。

我不确定d.cluster = 'localhost'(表中有多少行livedata_dom与此条件匹配?)的选择性,但添加索引(cluster)也可能会有所帮助。

于 2012-10-19T22:01:04.300 回答
1
set @rn := 0, @dom_id := 0;
select d.*, dc.date_change, dc.cwd, h.name as hub
from
    livedata_dom d
    left join (
        select
            hub_id, date_change, cwd, dom_id,
            if(@dom_id = dom_id, @rn := @rn + 1, @rn := 1) as rn,
            @dom_id := dom_id as dm_id
        from
            livedata_domcabling
        order by dom_id, date_change desc
    ) dc on d.id = dc.dom_id
    left join
    livedata_hub h on h.id = dc.hub_id
where rn = 1 or rn is null
order by dom_id

您发布的数据没有 dom_id 249。#250 有一个空日期,所以它排在第一位。所以你的结果不能反映我对你的问题的理解。

于 2012-10-19T21:38:48.347 回答