求救啊,一个简单存储过程的查询速度优化???
现在有一个存储过程查询,因为是查询中嵌套子查询,所以速度很慢,那为高手可以优化一下啊
存储过程如下:
CREATE PROCEDURE Sp_ClickAmTotal
@name char(10),
@rq char(10),
@count int OUTPUT
AS
select count(*) from
(select * from employee where hjbmbh in
(select distinct
ShowDept from Dept where name=@name)) as E,
(select distinct kh COLLATE chinese_PRC_CI_AI as kh from kimkqdata
where rq=@rq and (hs1<11 or (hs1=11 and ms1<15))) as K
where E.kqkh=K.kh
------解决方案--------------------SQL code
--try:
CREATE PROCEDURE Sp_ClickAmTotal
@name char(10),
@rq char(10),
@count int OUTPUT
AS
select count(1) from
(select kqkh from employee where exists(select 1
from Dept where ShowDept=hjbmbh and name=@name)) as E,
(select distinct kh COLLATE chinese_PRC_CI_AI as kh from kimkqdata
where rq=@rq and (hs1 <11 or (hs1=11 and ms1 <15))) as K
where E.kqkh=K.kh