【问题标题】:Optimized query to get min/max优化查询以获得最小值/最大值
【发布时间】:2010-08-09 14:17:51
【问题描述】:

我有数据

员工:

id  Name
--------
1   xyz
2   abc
3   qaz

Employee_A:(Eid - 员工表,title - 职务表)

eid    active    type    title
------------------------------
1     1         1        1
1     1         2        2
1     1         4        3
2     0         3        4
2     1         2        2
2     0         6        5
3     0         1        1
3     0         6        5
3     0         4        3

标题:

id  rank title_name
--------------------
1   1     Manager
2   1     Asst Manager
3   2     Staff1
4   3     Staff2
5   4     Staff3

我的查询看起来像

SELECT name, t.title_name
FROM Employee e
INNER JOIN Employee_A a ON e.id = a.eid
INNER JOIN title t on a.title = t.id
WHERE a.type in (1,2) and active = 1
and t.rank = /* step 1 */
(select min(rank) FROM Employee_A a1 INNER JOIN title t1 ON a1.title = t1.id 
 WHERE a1.type in (1,2) and active = 1 AND a1.eid = e.id)
AND a.type = /* step 2 */
    ( select min(type) FROM Employee_A a2 INNER JOIN title t2 on a2.title = t2.id
      WHERE a2.type in (1,2) and active = 1 and a2.eid = e.id
     and t2.rank = 
         (select min(rank) FROM Employee_A a3 INNER JOIN title t3 ON a3.title = t3.id 
          WHERE a3.type in (1,2) and active = 1 AND a3.eid = e.id)
    )

我有超过 40 万条记录,我正在尝试查询具有最低排名的最小类型(2 个标题具有相同的排名,因此需要在它们之间选择最小标题)。我的查询需要更多时间,我想对其进行优化,有没有办法可以避免步骤 1、2 并让它更简单、更快速?

输出:

Name   title_name
-----------------
xyz    Manager
abc    Asst Manager

【问题讨论】:

  • 也许也请添加您想要的输出
  • 你能发布你的执行计划吗?
  • 对不起,我不能,我没有实际查询中使用的架构之一的权限。

标签: sql sql-server sql-server-2005 tsql stored-procedures


【解决方案1】:

这能满足你的需要吗?

;WITH cte AS
(
SELECT name, t.title_name, 
  row_number() over (partition by e.id order by rank, type) rn
FROM Employee e
INNER JOIN Employee_A a ON e.id = a.eid
INNER JOIN title t on a.title = t.id
WHERE a.type in (1,2) and active = 1
)

select name,  title_name from cte
where rn=1

或者其他选择

SELECT e.name, t.title_name
FROM Employee e 
CROSS APPLY 
(
SELECT TOP (1) title_name 
FROM Employee_A a 
INNER JOIN title t on a.title = t.id
WHERE a.type in (1,2) and active = 1 AND e.id = a.eid
order by rank, type
) t

【讨论】:

    【解决方案2】:

    我可能过于简单了,但请试一试:

    select emp.name, MIN(a.title), MIN(t.rank)
    from Employee emp
    join Employee_A a on emp.id = a.eid
                      AND a.type in(1,2)
                      and a.active = 1
    join title t on a.title = t.id
    where not exists (select 1 from Employee_A where eid = emp.id and title < a.title and active = 1)
    group by emp.name
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多