일단 테이블에 대해 간단하게 설명드리면
테이블명: account
컬럼: user_id(pk), user_name(unique index), user_no(unique index), ....
입니다.
현재 제가 짜는 프로시저는 각각 @user_id, @user_name, @user_no을 파라미터로 받아 where절에서 사용하고 각각의 파라미터가
기본값(0 또는 '')일 경우는 조건문에서 사용하지 않는 쿼리입니다.
동적 쿼리로 짜긴했는데 혹시 정적 쿼리로 짜면서 정상적으로 인덱스를 타는 쿼리가 있을까 싶어서 질문남깁니다~
제가 짠 쿼리는
select *
from account
where ((@user_id <> 0 and user_id = @user_id) or @user_id = 0)
and ((@user_name<> '' and user_name= @user_name) or @user_name = '')
and ((@user_no<> 0 and user_no= @user_no) or @user_no= 0)
조언 부탁드립니다~
Comment 2
-
처리짱
2020.01.02 19:29
create proc user_id (@user_id ) select * from account where user_id = @user_idcreate proc user_name (@user_name ) select * from account where user_name = @user_namecreate proc user_no (@user_no ) select * from account where user_no = @user_no각각의 프로시져를 만드신다음에create proc select_test(@user_id@user_name@user_no)IF user_id != ''exec proc_user_id @user_idELSE IF user_name != ''exec proc_user_name @user_nameELSE IF user_no != ''exec proc_user_no @user_no위와같이 만드시는것이 좋을거 같습니다. -
약은 중
2020.03.03 18:18
전제조건 :1)user_no가 user_name보다 선택도가 좋다고 본다.2)인덱스명은 아래와 같다pk_account = user_iduk1_account = user_name + ....uk2_account = user_no + ....쿼리 :select *from account A with(index=pk_account)where user_id = @user_idand (@user_name = '' or user_name = @user_name)and (@user_no = 0 or user_no= @user_no)and @user_id <> 0 /*user_Id가 들어왔다*/union allselect *from account A with(index=uk2_account)where user_no= @user_noand (@user_name = '' or user_name = @user_name)and @user_id = 0 /*user_id가 안들어왔다*/and @user_no <>0 /*@user_no이 들어왔다*/union allselect *from account A with(index=uk1_account)where user_name= @user_nameand @user_id = 0 /*user_Id가 안들어왔다*/and @user_no = 0 /*@user_no도 안들어왔다*/and @user_name <> '' /*user_name만 들어왔다*/