0

堆栈溢出神,

在 BMC Remedy 中,没有任何开箱即用的功能可以让您查看一张票在某人的队列中排了多长时间。我创建了一个执行此操作的 SQL 函数,但速度很慢。你能帮我调整一下或者重新考虑我的流程吗?

故事数据看起来像这样http://i.stack.imgur.com/B4Uzu.png

所以基本上我正在做的是计算每个“价值”字段之间的差异(日期在纪元中)。我得到前一行和当前行之间的差异(以秒为单位)然后除以 60 给我分钟

ALTER function [dbo].[getTimeAssigned]
(
    @support_group nvarchar(100),
    @request_id nvarchar(100)
)
returns bigint
as
begin

--Sample data that would be sent to the function
/*
declare @request_id nvarchar(30);
declare @support_group nvarchar(50);
set     @support_group = 'Security Support';
set     @request_id = 'INC000001049252';
*/

--can't do temp tables inside a function so I made it a variable
declare @tempTable table    
                    (
                        request_id nvarchar(30)
                        , assigned_group nvarchar(100)
                        , minutes_assigned bigint
                    )

--need to find if ticket has been closed or not
--if not we want to apply current timestamp to 'resolution_time' because i want to see how long it has been open
declare @resolution_time bigint;
select  @resolution_time = audit_date 
from    HPD_HELPDESK_AUDITLOGSYSTEM_V 
where   ORIGINAL_REQUEST_ID = @request_id 
        and ([LOG] like '%resolution: resolved%' or [LOG] like '%status: Resolved%')

--means that the ticket is still open so apply current timestamp to resolution time     
if (@resolution_time is null) set @resolution_time = DATEDIFF(s, '1970-01-01 00:00:00', GETDATE())+(3600*5)     

--slow part
--need to calculate the time difference between the previous row's assign time and the current rows assign time
--that would give me how long it spent in each queue 
;with story as
(
    select  rownum = ROW_NUMBER() over (order by audit_date)
            , [LOG]
            , ORIGINAL_REQUEST_ID 
            , AUDIT_DATE [value]
    from    HPD_HELPDESK_AUDITLOGSYSTEM_V
    where   ORIGINAL_REQUEST_ID  = @request_id and [LOG] like '%assigned group%'
)

--inserting into temp table to be able to aggregate only the amount assigned to @support_group and return that value
insert into @tempTable  
select      prev.ORIGINAL_REQUEST_ID 
            , case
                --just renames this field to the support group i'm looking for
                when prev.[LOG]  like '%assigned group: ' + @support_group + '%' then @support_group 
                --any other group that i don't care about
                else 'Another Group'
            end [Assigned_Group]
            , (cur.value - prev.value) / 60 [minutes_assigned] --THE RUB
from        story cur
            inner join story prev 
on          prev.rownum = cur.rownum - 1

union all

select      cur.ORIGINAL_REQUEST_ID
            , case
                when cur.[LOG] like '%assigned group: ' + @support_group + '%' then @support_group
                else 'Another Group'
            end [Assigned_Group]
            , (@resolution_time - cur.value)/60 [minutes_assigned] --THE RUB!!!
from        story cur
where       cur.rownum = (select MAX(story.rownum) from story)

declare @return bigint

--aggregating for return
select      @return = SUM(minutes_assigned) 
from        @tempTable  
where       assigned_group = @support_group 
group by    assigned_group, request_id 

return @return 
end

如果我在过去 30 天内针对票运行此操作,则大约需要 1 1/2 小时才能完成。

我哪里错了?它是表变量吗?我可以做些什么来加快速度?

我相信 SQL 2012 有一个内置的 prev/next 回调,但 Remedy 在 2008 年,所以我必须手动执行 - 我对此是否正确?

4

2 回答 2

0

如果您想要的只是前一行,您可以通过相关子查询获得:

select h.*,
       (select top 1 h2.value
        from HPD_HELPDESK_AUDITLOGSYSTEM_V h2
        where h2.original_request_id = h.original_request_id and
              h2.value < h.value
        order by h2.value desc
       ) as last_value
from HPD_HELPDESK_AUDITLOGSYSTEM_V h;

对于您的计算:

select h.*,
       (value -
        (select top 1 h2.value
         from HPD_HELPDESK_AUDITLOGSYSTEM_V h2
         where h2.original_request_id = h.original_request_id and
               h2.value < h.value
         order by h2.value desc
        )
       ) / 60.0 as diff_in_minutes
from HPD_HELPDESK_AUDITLOGSYSTEM_V h;
于 2014-07-16T14:39:11.277 回答
0

你在使用 SLM 吗?你会得到很多关于事件在队列中等待了多长时间的信息,等等,如果你这样做了SLM 事件相关服务目标

否则,我将采用另一种方式并创建一个在“获取条目”上运行的过滤器,该过滤器填充表单上的 a 字段(计算您在 SQL 中尝试的相同值。)。这意味着,如果您在补救表格上创建报告,将显示这些值。尽管 BMC JDBC 驱动程序可能是一种替代方案,但您无法通过 SQL 将其提取出来。

于 2014-07-18T10:00:37.513 回答