Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 有没有办法使此查询在性能方面更高效?_Sql_Sql Server 2008 - Fatal编程技术网

Sql 有没有办法使此查询在性能方面更高效?

Sql 有没有办法使此查询在性能方面更高效?,sql,sql-server-2008,Sql,Sql Server 2008,在MS Sql 2008 DB上运行此查询需要很长时间,数据量为70GB。 如果我单独运行两个where子句,所需时间就会少很多 编辑-之后我需要将“选择*”更改为“删除”,请在回答时记住这一点。谢谢: select * From computers Where Name in ( select T2.Name from ( select Name from computers

在MS Sql 2008 DB上运行此查询需要很长时间,数据量为70GB。 如果我单独运行两个where子句,所需时间就会少很多

编辑-之后我需要将“选择*”更改为“删除”,请在回答时记住这一点。谢谢:

select *
From computers
Where Name in
      (
      select T2.Name
      from 
            (
            select Name
            from computers
            group by Name
            having COUNT(*) > 1
            ) T3 
      join computers T2 on T3.Name = T2.Name 
      left join policyassociations PA on T2.PK = PA.EntityId
      where (T2.EncryptionStatus = 0 or T2.EncryptionStatus is NULL) and 
            (PA.EntityType <> 1 or PA.EntityType is NULL)
      )
OR
      ClientId in
      (
      select substring(ClientID,11,100)
      from computers
      )
或者有时可能优化得很差。在这种情况下,您可以将查询拆分为两个子查询,然后使用union组合它们:

换成EXISTS将有所帮助。 此外,正如戈登的回答:工会的表现可以超过工会


我想看的一些东西是 1.索引是否到位? 2. 'IN'将减慢您的查询速度,请尝试将其替换为联接, 3.您应该使用列名,我猜在这种情况下是'name',而使用count*, 4.通过选择特定列,尝试仅选择所需数据


希望这有帮助

将IN子查询替换为exists子查询t2和t3从未被外部查询引用,并且t2和t3具有相同的用途,它们的乘积是笛卡尔乘积。返回以下错误-Msg 205,级别16,状态1,第1行使用UNION组合的所有查询,INTERSECT或EXCEPT运算符在其目标列表中的表达式数必须相等。@Morrtz已编辑,请立即重试。我很懒,不知道表的结构和使用SELECT*。你应该总是列出你的专栏。我编辑了这篇文章,补充说我需要在之后将选择更改为删除,所以在回答之前请检查,因为我无法使用union。你一次尝试删除多少行?请记住,删除大型数据集将花费更长的时间写入日志文件、锁等。您可能希望查看批处理删除。您的新查询速度成倍提高,我花了大约30分钟的时间,现在只花了不到一分钟。运行时出错-Msg 4104,级别16,状态1,第8行多部分标识符T2.PK无法绑定。我已经编辑了帖子,并补充说我需要在之后将select更改为delete,因此在回答之前请检查,因为我无法使用trhe
select *
From computers
Where Name in
      (
      select T2.Name
      from 
            (
            select Name
            from computers
            group by Name
            having COUNT(*) > 1
            ) T3 
      join computers T2 on T3.Name = T2.Name 
      left join policyassociations PA on T2.PK = PA.EntityId
      where (T2.EncryptionStatus = 0 or T2.EncryptionStatus is NULL) and 
            (PA.EntityType <> 1 or PA.EntityType is NULL)
      )
UNION
select *
From computers
WHERE ClientId in
      (
      select substring(ClientID,11,100)
      from computers
      );
select c.*
From computers c left outer join
     (select c.Name
      from (select c.*, count(*) over (partition by Name) as cnt
            from computers c
           ) c left join
           policyassociations PA
           on T2.PK = PA.EntityId and PA.EntityType <> 1
      where (c.EncryptionStatus = 0 or c.EncryptionStatus is NULL) and
            c.cnt > 1
     ) cpa
     on c.Name = cpa.Name left outer join
     (select substring(ClientID, 11, 100) as name
      from computers
     ) csub
     on c.Name = csub.name 
Where cpa.Name is not null or csub.Name is not null;
SELECT computers.*
FROM   computers
 LEFT
  JOIN policyassociations
    ON policyassociations.entityid = computers.pk
WHERE  (
          computers.encryptionstatus = 0
       OR computers.encryptionstatus IS NULL
       )
AND    (
          policyassociations.entitytype <> 1
       OR policyassociations.entitytype IS NULL
       )
AND    EXISTS (
         SELECT name
         FROM   (
                 SELECT name
                 FROM   computers
                 GROUP
                     BY name
                 HAVING Count(*) > 1
                ) As duplicate_computers
         WHERE  name = computers.name
       )

UNION

SELECT *
FROM   computers As c
WHERE  EXISTS (
         SELECT SubString(clientid, 11, 100)
         FROM   computers
         WHERE  SubString(clientid, 11, 100) = c.clientid
       )
DELETE
FROM   computers
 LEFT
  JOIN policyassociations
    ON policyassociations.entityid = computers.pk
WHERE  (
          computers.encryptionstatus = 0
       OR computers.encryptionstatus IS NULL
       )
AND    (
          policyassociations.entitytype <> 1
       OR policyassociations.entitytype IS NULL
       )
AND    EXISTS (
         SELECT name
         FROM   (
                 SELECT name
                 FROM   computers
                 GROUP
                     BY name
                 HAVING Count(*) > 1
                ) As duplicate_computers
         WHERE  name = computers.name
       )
;

DELETE
FROM   computers As c
WHERE  EXISTS (
         SELECT SubString(clientid, 11, 100)
         FROM   computers
         WHERE  SubString(clientid, 11, 100) = c.clientid
       )
;