Search code examples
sqlsql-serverdatabase-performancesqlperformance

SQL Server How to properly create a view upon dependent tables?


Given the following tables (see below) I need to create a View that shows each code (SOC, HLGT, HLT, PT AND LLT) from table MedDRARelations and his respective value (Soc_Value, HLGT_value, HLT_Value, PT_Value and LLT_Value) from the table MedDRANames.

I've tried by joining multiple times the MedDRANames table and creating a scalar function which returns value given the code (key).

Table definitions:

CREATE TABLE dbo.MedDRANames 
(
    [Key]   INT NOT NULL,
    [Value] VARCHAR(100) NULL,
    CONSTRAINT [PK_MedDRANames] PRIMARY KEY CLUSTERED ([Key])
);

CREATE TABLE dbo.MedDRARelations 
(
    [SOC]  INT NOT NULL,
    [HLGT] INT NOT NULL,
    [HLT]  INT NOT NULL,
    [PT]   INT NOT NULL,
    [LLT]  INT NOT NULL,    
    CONSTRAINT [PK_MedDRARelations] 
        PRIMARY KEY CLUSTERED ([SOC] ASC, [HLGT] ASC, [HLT] ASC, [PT] ASC, [LLT] ASC),
    CONSTRAINT [FK_MedDRANameSOC] 
        FOREIGN KEY ([SOC]) REFERENCES dbo.MedDRANAmes([Key]),
    CONSTRAINT [FK_MedDRANameHLGT] 
        FOREIGN KEY ([HLGT]) REFERENCES dbo.MedDRANAmes([Key]),
    CONSTRAINT [FK_MedDRANameHLT] 
        FOREIGN KEY ([HLT]) REFERENCES dbo.MedDRANAmes([Key]),
    CONSTRAINT [FK_MedDRANamePT] 
        FOREIGN KEY ([PT]) REFERENCES dbo.MedDRANAmes([Key]),
    CONSTRAINT [FK_MedDRANameLLT] 
        FOREIGN KEY ([LLT]) REFERENCES dbo.MedDRANAmes([Key])
);

Views:

This view executes in 22.1 s (SD 1.25) and affects 156867 rows

SELECT 
    [X].[SOC] AS [Código SOC],
    [SOC].[Value] AS [Término SOC],
    [X].[HLGT] AS [Código HLGT],
    [HLGT].[Value] AS [Término HLGT],
    [X].[HLT] AS [Código HLT],
    [HLT].[Value] AS [Término HLT],
    [X].[PT] AS [Código PT],
    [PT].[Value] AS [Término PT],
    [X].[LLT] AS [Código LLT],
    [LLT].[Value] AS [Término LLT]
FROM 
    dbo.MedDRARelations AS [X]
INNER JOIN 
    dbo.MedDRANames AS [SOC] ON [X].[SOC] = [SOC].[Key]
INNER JOIN 
    dbo.MedDRANames AS [HLGT] ON [X].[HLGT] = [HLGT].[Key]
INNER JOIN 
    dbo.MedDRANames AS [HLT] ON [X].[HLT] = [HLT].[Key]
INNER JOIN 
    dbo.MedDRANames AS [PT] ON [X].[PT] = [PT].[Key]
INNER JOIN 
    dbo.MedDRANames AS [LLT] ON [X].[LLT] = [LLT].[Key]

This view executes in 35.3 s (SD 2.1) and affects 156867 rows

SELECT 
    [X].[SOC] AS [Código SOC],
    dbo.FindMedDRA(x.SOC) AS [Término SOC],
    [X].[HLGT] AS [Código HLGT],
    dbo.FindMedDRA(x.HLGT) AS [Término HLGT],
    [X].[HLT] AS [Código HLT],
    dbo.FindMedDRA(x.HLT) AS [Término HLT],
    [X].[PT] AS [Código PT],
    dbo.FindMedDRA(x.PT) AS [Término PT],
    [X].[LLT] AS [Código LLT],
    dbo.FindMedDRA(x.LLT) AS [Término LLT]
FROM 
    dbo.MedDRARelations AS [X]

Scalar functions:

CREATE FUNCTION [dbo].[FindMedDRA]
    (@code INT)
RETURNS VARCHAR(100)
AS
BEGIN
    DECLARE @returning VARCHAR(100)

    SELECT @returning = dbo.MedDRANames.Value 
    FROM dbo.MedDRANames 
    WHERE dbo.MedDRANames.[Key] = @code

    RETURN @returning
END

I would like to know what is the proper way to achieve this because creating a joined table elapses 12.2 s (SD 0.1) but costs 5 times original space.

Thanks in advance.

UPDATE: Execution plans

  1. Using function: https://www.brentozar.com/pastetheplan/?id=rkso0aKnV
  2. Before Ixs creations: https://www.brentozar.com/pastetheplan/?id=HkFlyRth4
  3. After Ixs creations (as suggested by Laughing Vergil): https://www.brentozar.com/pastetheplan/?id=B13ZW0t3V

Solution

  • The biggest issue here is that there is no index on the values in MedRARelations. NOTE: A common misconception is that creating a Foreign Key creates an index. It does not.

    So, try running this:

    CREATE INDEX Idx_MedDRARelations_SOC ON MedDRARelations (SOC);
    CREATE INDEX Idx_MedDRARelations_HGLT ON MedDRARelations (HGLT);
    CREATE INDEX Idx_MedDRARelations_HLT ON MedDRARelations (HLT);
    CREATE INDEX Idx_MedDRARelations_PT ON MedDRARelations (PT);
    CREATE INDEX Idx_MedDRARelations_LLT ON MedDRARelations (LLT);
    

    Then try your view and other methods again.