使用JOIN或相关子查询与TEMPORARY TABLE进行MySQL查找

时间:2017-12-13 04:31:07

标签: mysql database inner-join temp-tables

我有两个主要表,providers’ and depots`。每个提供者都有一个必须的第一个位置。通过添加额外的“仓库”来提供其他位置。 'etc'表示与提供者帐户相关的大量附加变量。为简化起见,此处省略了额外的表格信息和过滤器。

providers
+--------+------------+-----+
| id     | location   | etc |
+--------+------------+-----+
| 1      | POINT(1,1) | ... |
| 2      | POINT(1,2) | ... |
| 3      | POINT(1,3) | ... |
+--------+------------+-----+

depots
+---------+------------+------------+
| depotId | providerId | location   |
+---------+------------+------------+
| 1       | 1          | POINT(2,1) |
| 2       | 1          | POINT(2,2) |
| 3       | 1          | POINT(2,3) |
| 4       | 2          | POINT(2,4) |
| 5       | 2          | POINT(2,5) |
+---------+------------+------------+

提供商可能拥有零个或多个附加软件仓库。这些“位置”用于计算来自每个提供者的传入“作业”的距离。传统上我使用UNION加入providersdepots表来形成一个表,我将其称为provDeps

SELECT id,location,0 AS depotId FROM providers UNION SELECT p.id,d.location d.id AS depotId FROM providers p,depots d

让我们假设这是一个视图,现在要求效率和索引。它有望降低查询的视觉复杂性。

provDeps
+--------+------------+---------+-----+
| id     | location   | depotId | etc |
+--------+------------+---------+-----+
| 1      | POINT(1,1) | 0       | ... |
| 1      | POINT(2,1) | 1       | ... |
| 1      | POINT(2,2) | 2       | ... |
| 1      | POINT(2,3) | 3       | ... |
| 2      | POINT(1,2) | 0       | ... |
| 2      | POINT(2,4) | 4       | ... |
| 2      | POINT(2,5) | 5       | ... |
| 3      | POINT(1,3) | 0       | ... |
+--------+------------+---------+-----+

然后我使用provDeps执行其他查找。这里的想法是计算作业与每个仓库的距离。这是通过存储过程执行的。

SELECT loc.*, degToMeter(st_distance(jobLocation, location)) AS distanceToJob FROM provDeps;

+--------+------------+---------+---------------+-----+
| id     | location   | depotId | distanceToJob | etc |
+--------+------------+---------+---------------+-----+
| 1      | POINT(1,1) | 0       | 8234          | ... |
| 1      | POINT(2,1) | 1       | 7334          | ... |
| 1      | POINT(2,2) | 2       | 6434          | ... |
| 1      | POINT(2,3) | 3       | 5534          | ... |
| 2      | POINT(1,2) | 0       | 4634          | ... |
| 2      | POINT(2,4) | 4       | 3734          | ... |
| 2      | POINT(2,5) | 5       | 2834          | ... |
| 3      | POINT(1,3) | 0       | 1934          | ... |
+--------+------------+---------+---------------+-----+

我现在需要减少此列表,仅保留最近的库,按提供者ID分组。结果将包括每个提供者,但每个只有一个仓库 - 主要位置,仓库'0'或最近的仓库的ID。这是理想的结果: -

+--------+------------+---------+---------------+-----+
| id     | location   | depotId | distanceToJob | etc |
+--------+------------+---------+---------------+-----+
| 1      | POINT(2,3) | 3       | 5534          | ... |
| 2      | POINT(2,5) | 5       | 2834          | ... |
| 3      | POINT(1,3) | 0       | 1934          | ... |
+--------+------------+---------+---------------+-----+

我尝试了很多方法,但每种方式我都遇到了不同的问题。我最接近成功的是使用临时表: -

DROP TEMPORARY TABLE IF EXISTS locTemp;
CREATE TEMPORARY TABLE locTemp AS
    SELECT depots.*, st_distance(jobLocation, location) AS distanceToJob
    FROM provDeps

然后我尝试使用相关子查询,但这会导致在单次查找中尝试访问临时表两次出错: -

SELECT * FROM locTemp
WHERE distanceToJob = (SELECT MIN(distanceToJob) FROM locTemp AS lt WHERE lt.id = locTemp.id);

这会导致错误“无法重新打开表:'locTemp'”。我也尝试过执行连接,但后来我从子查询中得到分组错误或无法访问临时表本身: -

SELECT * FROM
(
    SELECT id, depotId, MIN(distanceToJob) as minDist
    FROM locTemp GROUP BY id
) AS res
INNER JOIN locTemp AS lt on lt.id = res.id and lt.minDist = res.distanceToJob;

任何指针,或更好的解决方案,将不胜感激! :)

1 个答案:

答案 0 :(得分:1)

我认为您不一定需要临时表或视图。您的最终查询在此处完全正常,我在下面进行了修改。我看到的唯一问题是您选择了带有GROUP BY的非聚合列,并且使用临时表时可能存在问题。

SELECT
    t1.id, t1.location, t1.depotId,
    degToMeter(st_distance(t1.jobLocation, t1.location)) AS distanceToJob
FROM provDeps t1
INNER JOIN
(
    SELECT
        id,
        MIN(degToMeter(st_distance(t1.jobLocation, t1.location))) AS minDistanceToJob
    FROM provDeps
    GROUP BY id
) t2
    ON t1.id = t2.id AND
       degToMeter(st_distance(t1.jobLocation, t1.location)) = t2.minDistanceToJob;