简体   繁体   English

SQL-如何优化查询

[英]SQL - How to optimize the query

I have wrote the following query which extracts all users which are in child departments of current user's department. 我编写了以下查询,该查询提取了当前用户部门的子部门中的所有用户。 Current user cames from client app, but I Decalred it here in SQL for tests reason. 当前用户来自客户端应用程序,但出于测试原因,我在SQL中将其删除。

DECLARE @UserID INT = 72

SELECT * 
FROM users
WHERE Department_Id IN ( 
                        SELECT DISTINCT Id    /*, idp*/
                        FROM departments
                        WHERE idp IN (
                                        SELECT Department_Id 
                                        FROM users 
                                        WHERE Id = @UserID
                                        )
                        )

OR Department_Id IN (
                        SELECT DISTINCT idp
                        FROM departments
                        WHERE idp IN (
                                        SELECT Department_Id 
                                        FROM users 
                                        WHERE Id = @UserID
                                        )
                        )

I wanted to select the Id and the idp from departments to have a short query, but when i use this way it returns me an SQL error : 我想从部门中选择Ididp进行简短查询,但是当我使用这种方式时,它会向我返回一个SQL错误:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. 如果未使用EXISTS引入子查询,则只能在选择列表中指定一个表达式。

This is because my list should contain only one column, but not 2. 这是因为我的列表应该只包含一列,而不是2。

Please advice me any way to reduce this query, especially the second part (after OR ) which is a copy-paste of first (before OR ) 请咨询我任何方式,以减少该查询,尤其是第二部分( 之后),这是第一次的复制粘贴( 之前)

Thank you. 谢谢。

Try using EXISTS like this 尝试像这样使用EXISTS

SELECT * 
FROM   users u
WHERE  EXISTS(  SELECT *
                FROM   departments
                WHERE  idp IN (SELECT Department_Id FROM users WHERE Id = @UserID)
                       AND (id = u.Department_Id
                           OR idp = u.Department_Id)    )

A few thoughts... 一些想法...

  1. Nested IN subqueries are unlikely to be friendly. 嵌套的IN子查询不太可能友好。
  2. You don't need DISTINCT when using IN 使用IN时不需要DISTINCT

I'd use GROUP BY to deal with the 1:many relationships, but as your answer is using an alternative structure, I'll try to keep close to what you have... 我会使用GROUP BY处理1:1:1关系,但是由于您的答案是使用其他结构,因此我将尽量保持与您的联系...

DECLARE @UserID INT = 72

SELECT
  *
FROM
  users AS associates
WHERE
  EXISTS (
    SELECT 
      *
    FROM
      users
    INNER JOIN      
      departments
        ON departments.idp = users.Department_Id
    WHERE
      users.id = @user_id
      AND (   departments.id  = associates.department_id
           OR departments.idp = associates.department_id)
  )

If you did use the GROUP BY approach, you avoid sub-queries and correlated-sub-queries all together... 如果确实使用了GROUP BY方法,则可以同时避免子查询和相关子查询...

DECLARE @UserID INT = 72

SELECT
  associates.id
FROM
  users
INNER JOIN      
  departments
    ON departments.idp = users.Department_Id
INNER JOIN
  users AS associates
    ON associates.department_id = departments.id
    OR associates.department_id = departments.idp
WHERE
  users.id = @user_id
GROUP BY
  associates.id

If there any other fields in associates that you need, just add them to the SELECT and the GROUP BY. 如果您需要associates中的任何其他字段,只需将它们添加到SELECT GROUP BY中。

SELECT * 
FROM users
WHERE Department_Id IN ( 
                    SELECT myId FROM 
                     ( SELECT Id AS myId
                       FROM departments
                       UNION ALL
                       SELECT idp AS myId
                       FROM departments
                     ) A 
                    WHERE A.myId IN (
                                    SELECT Department_Id 
                                    FROM users 
                                    WHERE Id = @UserID
                                    )
                    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM