Может кто-нибудь помочь мне оптимизировать этот SP, так как его запуск занимает более 3 минут, а затем выдает ошибку Timeout. «Время ожидания истекло. Время ожидания истекло до завершения операции или сервер не отвечает». Я новичок и хотел бы оптимизировать приведенный ниже SP, потому что для получения результатов требуется вечность, а также ошибка тайм-аута. Я попытался поместить индексирование во временные таблицы, но безуспешно. Справка !!!!
ALTER PROCEDURE GetCompanyByCandidatetest
@USER_ID VARCHAR(50) = NULL
AS
BEGIN
declare @statenew varchar(1000) = Null
declare @countnew varchar(1000) = Null
declare @STRSQLNew varchar(max)
declare @STRSQL4 varchar(max)
declare @state1 varchar(1000) = Null ,@ind varchar(1000) = Null,@Distinct varchar(1000) = Null,@Dist1 varchar(1000) = Null,
@EQuit_ID varchar(1000) = Null,@Degree_ID varchar(100) = Null,@Isguest varchar(100) = Null,@verified varchar(100) = Null,
@keyword varchar(100) = Null,@workexperience varchar(100) = Null,@prefcity varchar(100) = Null,@addcity varchar(100) = Null,
@Key_skill varchar(100) = Null,@CURRENT_DEPARTMENT varchar(100),@PREF_DEPARTMENT varchar(100),@CURRENT_DESIGNATION varchar(100),@PREF_DESIGNATION varchar(100),@indcurr varchar(100)
select @Isguest = isnull(guest_login,0), @verified = isnull(VERIFIED,0) from mtb_frinds where user_id = @USER_ID
select @statenew = isnull(PREF_STATE,''),@state1 = isnull(ADD_STATE,''),@Distinct = isnull(PREF_DISTRICT,''),@Distinct1 = isnull(add_dist,''),
@ind = isnull(PREF_INDUSTRY,''),@indcurr=isnull(CURRENT_INDUSTRY,'') ,@EQuit_ID = isnull(EQ_ID,'') ,@keyword=isnull(key_word,'') ,@workexperience=isnull(WORK_EXPYEARS,''),@CURRENT_DEPARTMENT=isnull(CURRENT_DEPARTMENT,''), @PREF_DEPARTMENT=isnull(PREF_DEPARTMENT,''),
@prefcity=isnull(PREF_CITY,'') ,@addcity=ADD_CITY,@Key_skill=isnull(Key_skill,''),@CURRENT_DESIGNATION=isnull(CURRENT_DESIGNATION,''),@PREF_DESIGNATION=isnull(PREF_DESIGNATION,'')
from mtb_frinds where USER_ID = @USER_ID
if(@statenew!='' and @Key_skill !='' and @state1 !='' and @keyword !='' )
begin
print 'ind' +@ind
print '@indcurr' +@indcurr
print '@PREF_DEPARTMENT'+ @PREF_DEPARTMENT
print '@CURRENT_DEPARTMENT'+@CURRENT_DEPARTMENT
print '@keyword'+ @keyword
set @STRSQLNew='
select ej_id, EQ_ID, JOB_STATUS ,job_desc,
active, USER_ID, JOB_TITLE, CREATED_ON,
REQUIRED_MP, WORK_EXPYEARS, SALARY, IND_ID,
Key_skill, STATE_ID, DIST_ID, LOCALITY_ID,
DEPT_ID,1 as Priority, EXPIRATION_DATE
into #dummy from mtb_jobs Ek
'
IF @EQuit_ID IS NOT NULL AND @EQuit_ID != ''
BEGIN
print 1
SET @STRSQLNew = @STRSQLNew + ' CROSS APPLY dbo.Split(EQ_ID,'','') AS A
CROSS APPLY dbo.Split('''+@EQuit_ID+''','','') AS B'
set @STRSQL4 ='(((A.items = B.items) or '
END
IF @keyword IS NOT NULL AND @keyword != ''
BEGIN
print 2
SET @STRSQLNew = @STRSQLNew + ' CROSS APPLY dbo.Split(keywords,'','') AS e
CROSS APPLY dbo.Split('''+@keyword+''','','') AS f'
set @STRSQL4 =@STRSQL4+'(e.items =f.items)) and '
END
IF @statenew IS NOT NULL AND @statenew != ''
BEGIN
print 3
SET @STRSQLNew = @STRSQLNew + ' CROSS APPLY dbo.Split(STATE_ID,'','') AS i
CROSS APPLY dbo.Split('''+@statenew+''','','') AS j'
set @STRSQL4 = @STRSQL4+'((i.items =j.items) '
END
IF @state1 IS NOT NULL AND @state1 != ''
BEGIN
print 4
SET @STRSQLNew = @STRSQLNew + ' CROSS APPLY dbo.Split(STATE_ID,'','') AS k
CROSS APPLY dbo.Split('''+@state1+''','','') AS l'
set @STRSQL4 =@STRSQL4+' or (k.items =l.items)) and '
END
IF @Key_skill IS NOT NULL AND @Key_skill != ''
BEGIN
print 5
SET @STRSQLNew = @STRSQLNew + ' CROSS APPLY dbo.Split(Key_skill,'','') AS g
CROSS APPLY dbo.Split('''+@Key_skill+''','','') AS h'
set @STRSQL4 =@STRSQL4+' ((g.items =h.items) or
'
END
set @STRSQLNew = @STRSQLNew+ ' where '+ @STRSQL4 +'(DEPT_ID in (select items from dbo.split('''+@CURRENT_DEPARTMENT+''','','')) or DEPT_ID in (select items from dbo.split('''+@PREF_DEPARTMENT+''','','')))))
or(e.items =f.items and ((k.items =l.items) or (i.items =j.items) ))
'
set @STRSQLNew =@STRSQLNew+ ' select * into #temp from ( select max(ej_id) as ej_id , max(EQ_ID) as EQ_ID, max(JOB_STATUS) as JOB_STATUS , max(job_desc) as job_desc,max(active) as active, max(USER_ID) as USER_ID ,max(JOB_TITLE) as JOB_TITLE ,max(CREATED_ON) as CREATED_ON ,max(REQUIRED_MP) as REQUIRED_MP ,max(WORK_EXPYEARS) as WORK_EXPYEARS ,
max(SALARY) as SALARY ,max(IND_ID) as IND_ID ,max(Key_skill) as Key_skill , max(STATE_ID) as STATE_ID ,max(DIST_ID) as DIST_ID,max(LOCALITY_ID) as LOCALITY_ID ,max(DEPT_ID) as DEPT_ID,2 as Priority,max(EXPIRATION_DATE) as EXPIRATION_DATE from #dummy where EXPIRATION_DATE >0 group by ej_id
) as x '
set @STRSQLNew =@STRSQLNew+' select * into #Temp2 from
(
select a.ej_id, isnull(a.JOB_TITLE,'''') as JOB_TITLE, isnull(b.ENT_NAME,'''') as ENT_NAME, CASE
WHEN DATEDIFF(SECOND, a.CREATED_ON, GETDATE()) < 60 THEN ''Just Posted''
WHEN DATEDIFF(MINUTE, a.CREATED_ON, GETDATE()) < 60 THEN CAST(DATEDIFF(MINUTE, a.CREATED_ON, GETDATE()) AS VARCHAR(10)) + '' Minutes ago''
WHEN DATEDIFF(MINUTE, a.CREATED_ON, GETDATE()) < 24 * 60 THEN CAST(FLOOR(DATEDIFF(MINUTE, a.CREATED_ON, GETDATE())/60) AS VARCHAR(10)) + '' Hours ago''
ELSE CAST(FLOOR(DATEDIFF(HOUR, a.CREATED_ON, GETDATE())/24) AS VARCHAR(10)) + ''Days ago''
END AS POSTED_ON,a.REQUIRED_MP,''1'' as Issuitable,a.job_desc, a.created_on,DATEDIFF(DAY,GETDATE(),a.EXPIRATION_DATE) as EXPIRATION_DATE
,isnull('''+@Isguest+''',''0'') as Isguest,isnull('''+@verified+''',''0'') as Isverified
,isnull(cnt,0) as Isapplied,substring(
(
Select distinct '', ''+ ST1.StateName AS [text()]
From MTB_STATESNEW ST1
Where ST1.StateId in (select items from dbo.split (a.state_id,'',''))
For XML PATH ('''')
), 2, 1000000) [StateName],
substring(
(
Select distinct '', ''+ min(convert(varchar(1000),ST1.MIN_EXP)) AS [text()]
From dbo.MTB_WORKEXP ST1
Where ST1.EXP_ID in (select items from dbo.split (a.WORK_EXPYEARS,'',''))
For XML PATH ('''')
), 2, 1000000) [min_exp],
substring(
(
Select distinct '',''+ max(convert(varchar(1000),ST1.MAX_EXP)) AS [text()]
From dbo.MTB_WORKEXP ST1
Where ST1.EXP_ID in (select items from dbo.split (a.WORK_EXPYEARS,'',''))
For XML PATH ('''')
), 2, 1000000) [max_exp],
substring(
(
Select distinct '', ''+ min(convert(varchar(1000),ST1.SR_MIN)) AS [text()]
From dbo.MTB_SALARYRANGE ST1
Where ST1.SR_ID in (select min(items) from dbo.split (a.SALARY,'',''))
For XML PATH ('''')
), 2, 1000000) [min_Salary],
substring(
(
Select distinct '',''+ max(convert(varchar(1000),ST1.sr_max)) AS [text()]
From dbo.MTB_SALARYRANGE ST1
Where ST1.SR_ID in (select items from dbo.split (a.SALARY,'',''))
For XML PATH ('''')
), 2, 1000000) [max_Salary],
substring(
(
Select distinct '',''+ (convert(varchar(1000),ST1.KEY_SKILL)) AS [text()]
From MTB_KEYSKILL ST1
Where ST1.KEYSKILL_ID in (select items from dbo.split (a.Key_skill,'',''))
For XML PATH ('''')
), 2, 1000000) [KEY_SKILL]
,
substring (
(Select distinct '',''+ (convert(varchar(1000),ST1.KEYWORDS)) AS [text()]
From mtb_keywords ST1 where st1.KEYWORD_ID in ( select items from dbo.split('''+@keyword+''','',''))
For XML PATH ('''')
), 2, 1000000) [KEY_Word],
substring (
(Select distinct '',''+ (convert(varchar(1000),ST1.DIST_NAME)) AS [text()]
From MTB_DISTSNEW ST1 where st1.DIST_ID in ( select items from dbo.split(a.DIST_ID,'',''))
For XML PATH ('''')
), 2, 1000000) [dist_name],
substring(
(select distinct '',''+(convert(varchar(1000),ST1.EQ_NAME)) as [text()]
from MTB_EDUQUALIFICATIONS ST1 where st1.EQ_ID in (select items from dbo.split(a.EQ_ID,'',''))
For XML PATH('''')
),2,1000000) [EQ_Name],
substring(
(select distinct '',''+(convert(varchar(1000),ST1.IND_NAME)) as [text()]
from mtb_industry ST1 where st1.ind_id in (select items from dbo.split(a.ind_id,'',''))
For XML PATH('''')
),2,1000000) [IND_NAME]
from #temp as a inner join MTB_ENTERPRISES as b on a.user_id = b.user_id
left join (select count (ej_id) as cnt,ej_id from TTB_JOBAPPLICATIONS where USER_ID = '''+@USER_ID+''' group by EJ_ID) as ER on ER.EJ_ID = a.EJ_ID
where( a.JOB_STATUS = 1 and a.active = 1 )
) as x'
set @STRSQLNew =@STRSQLNew+' SELECT *,isnull(min_exp,'''') +''-''+ isnull(max_exp,'''') as WORK_EXP,isnull(min_Salary,'''') +''-''+ isnull(max_salary,'''') as Salary FROM #Temp2 '
print @STRSQLNew
exec (@STRSQLNew)
select 1 as ispara1, 'Sucess' as Msg ,1 as n
end
else
begin
select top 0* from MTB_ENTERPRISES
select 0 as ispara2, 0 as n,'Some fields are Incomplete' as Msg
end
END
Ошибка: - истекло время ожидания выполнения. Время ожидания истекло до завершения операции, или сервер не отвечает.