【问题标题】:How can I optimize/modify this SQL query to have the same results but with better performance?如何优化/修改此 SQL 查询以获得相同的结果但性能更好?
【发布时间】:2021-11-25 16:29:21
【问题描述】:

这个 SQL 查询需要时间来执行,我不知道如何优化它/或修改它以获得相同的结果和更好的性能

SELECT equipment, relationType, client, IIF([_status] = 'D', -1, 0) as removed
FROM synchro_my3dshp_equipmentClient
WHERE [_destination] = 5
AND ([_status] IN ('M', 'D') 
OR equipment in (
SELECT distinct synchroKey
FROM synchro_my3dshp_clientAssembly
WHERE [_destination] = 5
AND ([_status] IN ('M', 'D') 
OR synchroKey in (
SELECT distinct equipment
FROM synchro_my3dshp_equipmentClient
WHERE [_destination] = 5 AND [_status] IN ('M', 'D'))))
)

【问题讨论】:

  • 这些是来自远程数据源的 MS Access 表还是链接表?如果是后者,是哪个数据库系统?
  • 它们是 MS Access 表

标签: sql ms-access


【解决方案1】:

我这样重写了我的查询,而且速度非常快

SELECT EC.equipment, EC.relationType, EC.client, IIF(EC.[_status] = 'D', -1, 0) AS removed
FROM synchro_my3dshp_equipmentClient AS EC
WHERE EC.[_destination] = 5 AND EC.[_status] IN('M', 'D')
UNION
SELECT EC.equipment, EC.relationType, EC.client, IIF(EC.[_status] = 'D', -1, 0) AS removed
FROM synchro_my3dshp_equipmentClient AS EC
INNER JOIN synchro_my3dshp_clientAssembly AS CA ON CA.synchroKey = EC.equipment 
WHERE CA.[_destination] = 5 AND CA.[_status] IN('M', 'D') AND EC.[_destination] = 5

如果我错了,请纠正我

【讨论】:

  • 看起来还不错,确实如果它及时返回了您期望的结果。如果是,请将其标记为答案。
  • 看起来确实更理智。不过,这里没有人能够保证它的正确性。
【解决方案2】:

由于我们没有您的数据,因此无法重现您的问题,我们只能给出“试试这个,看看是否能改善您的情况”的答案。我个人不喜欢这样的答案,但我们能做的就这些了。

冒着说明显而易见的风险:检查您的索引。根据您的查询,应该存在以下索引:

  • synchro_my3dshp_equipmentClient:_destination_status 上的组合索引。
  • synchro_my3dshp_equipmentClient:_destinationequipment 上的组合索引。
  • synchro_my3dshp_clientAssembly:_destination_status 上的组合索引。
  • synchro_my3dshp_clientAssembly:_destinationsynchroKey 上的组合索引。

如果它们不存在,请添加它们。

【讨论】:

  • 是的,我有索引,我最终重写了我的查询,现在速度非常快。您可以查看我的答案吗?谢谢
【解决方案3】:

因为您的第二个子查询似乎与顶部查询相同:

SELECT 
    equipment, 
    relationType, 
    client, 
    ([_status] = 'D') as removed
FROM 
    synchro_my3dshp_equipmentClient
WHERE 
    [_destination] = 5
    AND 
    ([_status] IN ('M', 'D') 
    OR 
    equipment in 

        (SELECT distinct 
            synchroKey
        FROM 
            synchro_my3dshp_clientAssembly
        WHERE 
            [_destination] = 5
            AND 
            ([_status] IN ('M', 'D') 
            OR 
            synchroKey in 

                (SELECT distinct 
                    equipment
                FROM 
                    synchro_my3dshp_equipmentClient
                WHERE 
                [_destination] = 5 
                AND 
                [_status] IN ('M', 'D')))))

它也许可以简化为(并从distinct 减轻):

SELECT 
    equipment, 
    relationType, 
    client, 
    ([_status] = 'D') AS removed
FROM 
    synchro_my3dshp_equipmentClient
WHERE 
    [_destination] = 5
    AND 
    ([_status] IN ('M', 'D') 
    OR 
    equipment IN 

        (SELECT  
            synchroKey
        FROM 
            synchro_my3dshp_clientAssembly
        WHERE 
            [_destination] = 5
            AND 
            [_status] IN ('M', 'D')))

但是,我可能遗漏了一些东西,没有您的数据,并且不确定您的目标是什么。

【讨论】:

  • 您能查看我的回答吗?谢谢
猜你喜欢
  • 2015-01-08
  • 2014-11-19
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2021-12-06
相关资源
最近更新 更多