我有函数返回员工编号整数
CREATE FUNCTION `getUser`(s CHAR(20)) RETURNS char(50) CHARSET latin1
RETURN @user;
当我用(0.00秒)
执行返回时但它执行此
select e.emp_number from hs_hr_employee e where
hie_code_1 in ( select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser2() and ( U.def_level=1 or U.def_level=4 ) )
or
hie_code_3 in ( select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser2() and U.def_level=2 )
or
hie_code_4 in ( select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=getUser2() and U.def_level=3 )
变得更慢;
但当我硬编码为
时select e.emp_number from hs_hr_employee e where
hie_code_1 in ( select hie_code_1 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=5 and ( U.def_level=1 or U.def_level=4 ) )
or
hie_code_3 in ( select hie_code_3 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=5 and U.def_level=2 )
or
hie_code_4 in ( select hie_code_4 from hs_hr_emp_level L,hs_hr_users U where L.emp_number=U.emp_number and L.emp_number=5 and U.def_level=3 )
这将以(0.00秒)时间返回。 请给出optimiste函数的解决方案;
答案 0 :(得分:1)
你的样品会让我的眼睛受伤,但这是尝试一种可能的选择
SELECT DISTINCT e.emp_number
FROM hs_hr_employee e
INNER JOIN hs_hr_emp_level L
ON L.emp_number = e.emp_number
AND (
e.hie_code_1 = U.hie_code_1
OR e.hie_code_3 = U.hie_code_3
OR e.hie_code_4 = U.hie_code_4
)
INNER JOIN hs_hr_users U
ON U.emp_number = e.emp_number
AND U.def_level IN (1,2,3,4)
-- You stated getUser2() returns paramter @user, so just accessing directly
WHERE e.emp_number = @user
假设如下
hs_hr_employee.emp_number
== hs_hr_emp_level.emp_number
== hs_hr_emp_level.emp_number
hs_hr_employee.hie_code_*n* ==
hs_hr_users.hie_code_ 名词的getUser2()
返回@user,这是一个员工编号上面的语法完成了复杂查询的功能。由于上述情况不正确,因此很难提供建议 - 如果您不提供完整的表格结构/适当的细节,那么我无法进一步帮助。