时间:2019-06-19 14:22:23

标签: sql-server

请考虑以下表格

日志(LOG)

Id,CorrId,FlowId,FlowName,AppId,ReqTimestamp,源,目标,步骤,CustomField

消息(MESSAGE)

Id,网址,方法,标头,MsgBlob,CreatedDatetime

链接日志(LOG_LOGS)

Id,CorrId,LinkedCorId

创建表并插入语句

    CREATE TABLE [dbo].[LOG](
        [ID] [uniqueidentifier] NOT NULL,
        [CORRELATION_ID] [uniqueidentifier] NULL,
        [ERROR_CODE] [nvarchar](15) NULL,
        [FLOW_ID] [nvarchar](10) NOT NULL,
        [APPLICATION_ID] [nvarchar](35) NOT NULL,
        [LOG_LEVEL] [nvarchar](35) NOT NULL,
        [LOG_TIMESTAMP] [datetime] NOT NULL,
        [EVENT_ID] [uniqueidentifier] NOT NULL,
        [FLOW_NAME] [nvarchar](50) NOT NULL,
        [PROCESS] [nvarchar](256) NOT NULL,
        [REQUEST_TIMESTAMP] [datetime] NOT NULL,
        [SOURCE] [nvarchar](256) NOT NULL,
        [TARGET] [nvarchar](256) NOT NULL,
        [STEP] [nvarchar](50) NOT NULL,
        [DESCRIPTION] [nvarchar](max) NULL,
        [LOG_SEQUENCE] [int] NOT NULL,
        [CREATED_DATETIME] [datetime] NOT NULL,
        [CUSTOM_FIELD] [nvarchar](60) NULL)

    CREATE TABLE [dbo].[MESSAGE](
        [ID] [uniqueidentifier] NOT NULL,
        [URL] [nvarchar](256) NULL,
        [METHOD] [nvarchar](35) NULL,
        [MSGBLOB] [varchar](max) NULL,
        [HEADERS] [nvarchar](max) NULL,
        [CREATED_DATETIME] [datetime] NOT NULL)

    CREATE TABLE [dbo].[LOG_LOGS](
        [CorrelationId] [uniqueidentifier] NOT NULL,
        [LinkedCorrelationId] [uniqueidentifier] NOT NULL,
        [CreatedDatetime] [datetime] NULL)

    --Log

    INSERT [dbo].[LOG]
        ([ID], [CORRELATION_ID], [ERROR_CODE], [FLOW_ID], [APPLICATION_ID], [LOG_LEVEL], [LOG_TIMESTAMP], [EVENT_ID], [FLOW_NAME], [PROCESS], [REQUEST_TIMESTAMP], [SOURCE], [TARGET], [STEP], [DESCRIPTION], [LOG_SEQUENCE], [CREATED_DATETIME], [CUSTOM_FIELD])
    VALUES
        ('C56BBFBE-5308-4242-ACD0-383F016C0AD1', '8C519F41-1F06-41E5-9647-25554315E1C7', 'NULL', 'TRIGGER', 'appIdString', 'DEBUG', '2019-04-26 14:08:23.660', 'C0F40CA0-682C-11E9-AB6D-0298370A6804', 'trigger', 'imp-trigger', '2019-04-26 14:08:23.660', 'SomeSource', 'SomeTarget', 'START', 'Some description', '1', '2019-04-26 14:08:24.677', 'NULL');
    INSERT [dbo].[LOG]
        ([ID], [CORRELATION_ID], [ERROR_CODE], [FLOW_ID], [APPLICATION_ID], [LOG_LEVEL], [LOG_TIMESTAMP], [EVENT_ID], [FLOW_NAME], [PROCESS], [REQUEST_TIMESTAMP], [SOURCE], [TARGET], [STEP], [DESCRIPTION], [LOG_SEQUENCE], [CREATED_DATETIME], [CUSTOM_FIELD])
    VALUES
        ('273F4E0A-CA7A-40B5-AF54-F2A53A776541', '8C519F41-1F06-41E5-9647-25554315E1C7', 'NULL', 'TRIGGER', 'appIdString', 'DEBUG', '2019-04-26 14:08:25.693', 'C0F40CA0-682C-11E9-AB6D-0298370A6804', 'trigger-melto', 'imp-trigger', '2019-04-26 14:08:23.660', 'SomeSource', 'SomeTarget', 'OUTBOUND', 'Some description', '4', '2019-04-26 14:08:29.110', 'NULL');
    INSERT [dbo].[LOG]
        ([ID], [CORRELATION_ID], [ERROR_CODE], [FLOW_ID], [APPLICATION_ID], [LOG_LEVEL], [LOG_TIMESTAMP], [EVENT_ID], [FLOW_NAME], [PROCESS], [REQUEST_TIMESTAMP], [SOURCE], [TARGET], [STEP], [DESCRIPTION], [LOG_SEQUENCE], [CREATED_DATETIME], [CUSTOM_FIELD])
    VALUES
        ('7AF12EBE-93AC-443F-82CD-9079C2851C9E', '8C519F41-1F06-41E5-9647-25554315E1C7', 'NULL', 'TRIGGER', 'appIdString', 'DEBUG', '2019-04-26 14:08:25.690', 'C0F40CA0-682C-11E9-AB6D-0298370A6804', 'trigger-melto', 'imp-trigger', '2019-04-26 14:08:23.660', 'SomeSource', 'SomeTarget', 'INBOUND', 'Some description', '3', '2019-04-26 14:08:29.270', 'NULL');
    INSERT [dbo].[LOG]
        ([ID], [CORRELATION_ID], [ERROR_CODE], [FLOW_ID], [APPLICATION_ID], [LOG_LEVEL], [LOG_TIMESTAMP], [EVENT_ID], [FLOW_NAME], [PROCESS], [REQUEST_TIMESTAMP], [SOURCE], [TARGET], [STEP], [DESCRIPTION], [LOG_SEQUENCE], [CREATED_DATETIME], [CUSTOM_FIELD])
    VALUES
        ('E0F5018E-8876-4929-828B-E1DB26CF7F30', '8C519F41-1F06-41E5-9647-25554315E1C7', 'NULL', 'TRIGGER', 'appIdString', 'DEBUG', '2019-04-26 14:08:25.477', 'C0F40CA0-682C-11E9-AB6D-0298370A6804', 'trigger-melto', 'imp-trigger', '2019-04-26 14:08:23.660', 'SomeSource', 'SomeTarget', 'OUTBOUND', 'Some description', '2', '2019-04-26 14:08:29.447', 'NULL');
    INSERT [dbo].[LOG]
        ([ID], [CORRELATION_ID], [ERROR_CODE], [FLOW_ID], [APPLICATION_ID], [LOG_LEVEL], [LOG_TIMESTAMP], [EVENT_ID], [FLOW_NAME], [PROCESS], [REQUEST_TIMESTAMP], [SOURCE], [TARGET], [STEP], [DESCRIPTION], [LOG_SEQUENCE], [CREATED_DATETIME], [CUSTOM_FIELD])
    VALUES
        ('8B7A0A86-14D8-4E72-8047-21A73ECF6157', '8C519F41-1F06-41E5-9647-25554315E1C7', '504', 'TRIGGER', 'appIdString', 'DEBUG', '2019-04-26 14:08:35.833', 'C0F40CA0-682C-11E9-AB6D-0298370A6804', 'trigger-melto', 'imp-trigger', '2019-04-26 14:08:23.660', 'SomeSource', 'SomeTarget', 'ERROR', 'Some description', '5', '2019-04-26 14:08:37.500', 'NULL');

    -- Message

    INSERT INTO [dbo].[MESSAGE]
        ([ID] ,[URL] ,[METHOD] ,[MSGBLOB] ,[HEADERS] ,[CREATED_DATETIME])
    VALUES
        ('C56BBFBE-5308-4242-ACD0-383F016C0AD1', 'https://localhost:1234/api/trigger', 'POST', 'U29tZSBtZXNzYWdlIGJsb2IgdG8gZGVjb2Rl', '{"Content-Type":"application/xml"}', '2019-04-26 14:08:25.577');
    INSERT INTO [dbo].[MESSAGE]
        ([ID] ,[URL] ,[METHOD] ,[MSGBLOB] ,[HEADERS] ,[CREATED_DATETIME])
    VALUES
        ('273F4E0A-CA7A-40B5-AF54-F2A53A776541', 'NULL', 'NULL', 'U29tZSBtZXNzYWdlIGJsb2IgdG8gZGVjb2Rl', 'null', '2019-04-26 14:08:29.813');
    INSERT INTO [dbo].[MESSAGE]
        ([ID] ,[URL] ,[METHOD] ,[MSGBLOB] ,[HEADERS] ,[CREATED_DATETIME])
    VALUES
        ('E0F5018E-8876-4929-828B-E1DB26CF7F30', 'NULL', 'NULL', 'U29tZSBtZXNzYWdlIGJsb2IgdG8gZGVjb2Rl', 'null', '2019-04-26 14:08:29.613');
    INSERT INTO [dbo].[MESSAGE]
        ([ID] ,[URL] ,[METHOD] ,[MSGBLOB] ,[HEADERS] ,[CREATED_DATETIME])
    VALUES
        ('8B7A0A86-14D8-4E72-8047-21A73ECF6157', 'NULL', 'NULL', 'U29tZSBtZXNzYWdlIGJsb2IgdG8gZGVjb2Rl', 'null', '2019-04-26 14:08:37.817');

    -- Log_Logs

    INSERT [dbo].[LOG_LOGS]
        ([CorrelationId], [LinkedCorrelationId], [CreatedDatetime])
    VALUES
        ('8C519F41-1F06-41E5-9647-25554315E1C7', '972039B5-346B-4BC8-AE9B-2A45D51DA310', '2019-06-19 10:54:33.023');
    INSERT [dbo].[LOG_LOGS]
        ([CorrelationId], [LinkedCorrelationId], [CreatedDatetime])
    VALUES
        ('8C519F41-1F06-41E5-9647-25554315E1C7', '972039B5-346B-4BC8-AE9B-2A45D51DA310', '2019-06-19 10:54:33.023');
    INSERT [dbo].[LOG_LOGS]
        ([CorrelationId], [LinkedCorrelationId], [CreatedDatetime])
    VALUES
        ('8C519F41-1F06-41E5-9647-25554315E1C7', '972039B5-346B-4BC8-AE9B-2A45D51DA310', '2019-06-19 10:54:33.023');
    INSERT [dbo].[LOG_LOGS]
        ([CorrelationId], [LinkedCorrelationId], [CreatedDatetime])
    VALUES
        ('8C519F41-1F06-41E5-9647-25554315E1C7', '972039B5-346B-4BC8-AE9B-2A45D51DA310', '2019-06-19 10:54:33.023');
    INSERT [dbo].[LOG_LOGS]
        ([CorrelationId], [LinkedCorrelationId], [CreatedDatetime])
    VALUES
        ('8C519F41-1F06-41E5-9647-25554315E1C7', '972039B5-346B-4BC8-AE9B-2A45D51DA310', '2019-06-19 10:54:33.023');

我需要什么(按10,20或50条记录分页)

  • 按Log.CorrId分组
  • Log.ReqTimestamp(对于所有Log.CorId应该相同)
  • Log.Source(对于所有Log.CorId应该相同)
  • Log.Target(对于所有Log.CorId应该相同)
  • Log.FlowName(对于所有Log.CorId都不相同:因此其中Log.Step = 'OUTBOUND',如果不可用Log.Step ='END',如果不可用 可以)
  • CanResubmit:SUM(当l.STEP ='START'并且DATALENGTH(m.MSGBLOB)> 0然后1 else 0结束时的情况)
  • 有错误:SUM(如果l.STEP ='ERROR'则1否则0结束)
  • 已结束:SUM(如果l.STEP ='END'则为1,否则为0结束)
  • HasLinkedLog

这是我到目前为止所拥有的。


    CREATE PROCEDURE [dbo].[sp_SearchFlowCorrelationIds]
        @FlowIdList AS dbo.FlowIdList READONLY,
        @CorrelationIdList AS dbo.CorrelationIdList READONLY,
        @FlowName nvarchar(50) = null,
        @Source nvarchar(256) = null,
        @Target nvarchar(256) = null,
        @ApplicationId nvarchar(35) = null,
        @CustomField nvarchar(60) = null,
        @Step nvarchar(50) = null,
        @DateFrom datetime = null,
        @DateTo datetime = null,
        @SortField nvarchar(50) = 'RequestTimestamp',
        @SortOrder nvarchar(4) = null,
        @Take int = 10,
        @Skip int = 0
    AS

    SET NOCOUNT ON

    DECLARE @DateBoundaryFrom int = 1;
    DECLARE @DateBoundaryTo int = 13;

    SELECT result.CORRELATION_ID as CorrelationId
        ,   result.REQUEST_TIMESTAMP as RequestTimestamp
        ,   result.FLOW_NAME as FlowName
        ,   result.SOURCE as Source
        ,   result.TARGET as Target
        ,   result.CanResubmit
        ,   result.HasError
        ,   result.HasEnd
        ,   result.LinkedCorrelationId as LinkedCorrelationId

    FROM 
        (SELECT  lastLogged.CORRELATION_ID
                , lastLogged.REQUEST_TIMESTAMP
                , lastLogged.SOURCE
                , lastLogged.TARGET
                , lastLogged.FLOW_NAME
                , l2.CanResubmit as CanResubmit
                , l2.HasError as HasError
                , l2.HasEnd as HasEnd
                , ll.LinkedCorrelationId
                , ROW_NUMBER() OVER (PARTITION BY lastLogged.CORRELATION_ID ORDER BY lastLogged.LOG_TIMESTAMP DESC) as dest_rank
        FROM LOG as lastLogged

        INNER JOIN (
            select CORRELATION_ID
                ,   SUM(case when l.STEP = 'START' and DATALENGTH(m.MSGBLOB) > 0 then 1 else 0 end) as CanResubmit
                ,   SUM(case when l.STEP = 'ERROR' then 1 else 0 end) as HasError
                ,   SUM(case when l.STEP = 'END' then 1 else 0 end) as HasEnd
            from LOG l
            left join MESSAGE m on l.ID = m.ID
            Where l.CREATED_DATETIME >= ISNULL(DATEADD(DAY, -@DateBoundaryFrom, @DateFrom), l.CREATED_DATETIME)
            AND l.CREATED_DATETIME <= ISNULL(DATEADD(DAY, @DateBoundaryTo, @DateTo), l.CREATED_DATETIME)
            group by l.CORRELATION_ID

        ) as l2 on lastLogged.CORRELATION_ID = l2.CORRELATION_ID

        LEFT JOIN LOG_LOGS ll on lastLogged.CORRELATION_ID = ll.CorrelationId

        WHERE 
            lastLogged.CREATED_DATETIME >= ISNULL(DATEADD(DAY, -@DateBoundaryFrom, @DateFrom), lastLogged.CREATED_DATETIME)
            AND lastLogged.CREATED_DATETIME <= ISNULL(DATEADD(DAY, @DateBoundaryTo, @DateTo), lastLogged.CREATED_DATETIME)
            AND lastLogged.REQUEST_TIMESTAMP >= ISNULL(@DateFrom, lastLogged.REQUEST_TIMESTAMP)
            AND lastLogged.REQUEST_TIMESTAMP <= ISNULL(@DateTo, lastLogged.REQUEST_TIMESTAMP)
            AND (lastLogged.FLOW_ID in (SELECT Flow_Id FROM @FlowIdList) OR NOT EXISTS (select 1 from @FlowIdList))
            AND (lastLogged.CORRELATION_ID in (SELECT ID FROM @CorrelationIdList) OR NOT EXISTS (select 1 from @CorrelationIdList))
            AND lastLogged.FLOW_NAME = ISNULL(@FlowName, lastLogged.FLOW_NAME)
            AND lastLogged.SOURCE = ISNULL(@Source, lastLogged.SOURCE)
            AND lastLogged.TARGET = ISNULL(@Target, lastLogged.TARGET)
            AND lastLogged.APPLICATION_ID = ISNULL(@ApplicationId, lastLogged.APPLICATION_ID)
            AND lastLogged.STEP = ISNULL(@Step, lastLogged.STEP)        
            AND (lastLogged.CUSTOM_FIELD = @CustomField OR @CustomField IS NULL)
        ) AS result

    WHERE result.dest_rank = 1

    ORDER BY 
    -- ASCENDING
        CASE
            WHEN @SortOrder <> 'ASC' then cast(null as date)
            WHEN @SortField = 'RequestTimestamp' then result.REQUEST_TIMESTAMP
        end ASC
    ,   CASE
            WHEN @SortOrder <> 'ASC' then cast(null as uniqueidentifier)
            WHEN @SortField = 'CorrelationId' then result.CORRELATION_ID
        end ASC
    ,   CASE
            WHEN @SortOrder <> 'ASC' then ''
            WHEN @SortField = 'FlowName' then result.FLOW_NAME
        end ASC
    ,   CASE
            WHEN @SortOrder <> 'ASC' then ''
            WHEN @SortField = 'Source' then result.SOURCE
        end ASC
    ,   CASE
            WHEN @SortOrder <> 'ASC' then ''
            WHEN @SortField = 'Target' then result.TARGET
        end ASC

    -- DESCENDING
    ,   CASE
            WHEN @SortOrder <> 'DESC' then cast(null as date)
            WHEN @SortField = 'RequestTimestamp' then result.REQUEST_TIMESTAMP
        end DESC
    ,   CASE
            WHEN @SortOrder <> 'DESC' then cast(null as uniqueidentifier)
            WHEN @SortField = 'CorrelationId' then result.CORRELATION_ID
        end DESC
    ,   CASE
            WHEN @SortOrder <> 'DESC' then ''
            WHEN @SortField = 'FlowName' then result.FLOW_NAME
        end DESC
    ,   CASE
            WHEN @SortOrder <> 'DESC' then ''
            WHEN @SortField = 'Source' then result.SOURCE
        end DESC
    ,   CASE
            WHEN @SortOrder <> 'DESC' then ''
            WHEN @SortField = 'Target' then result.TARGET
        end DESC

    OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY

问题

现在的问题是,如何根据以下条件在其中获取FlowName:

  • 其中Log.Step ='OUTBOUND',如果不可用,Log.Step ='END',如果不可用,则可以)

(如果有任何性能改进的提示,那么它们总是很受欢迎的,因为这对7ml记录大约需要8秒钟或更长时间)

0 个答案:

没有答案