/*
Missing Index Details from Regressed Queries [RedApp]
The Query Processor estimates that implementing the following index could improve the query cost by 22.5659%.
*/

USE [RedApp]
GO

IF INDEXPROPERTY(OBJECT_ID('IND_MeddraLowLevelTermDes'), 'NC_IX_IND_MeddraLowLevelTermDes__term_FK__Lang' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE NONCLUSTERED INDEX NC_IX_IND_MeddraLowLevelTermDes__term_FK__Lang
    ON [dbo].[IND_MeddraLowLevelTermDes] ([MeddraLowLevelTerm_FK],[Lang])
    INCLUDE ([Descr]);
    PRINT 'created NC_IX_IND_MeddraLowLevelTermDes__term_FK__Lang'
END
GO

IF INDEXPROPERTY(OBJECT_ID('WF_BmOrder'), 'NC_IX_WF_BmOrder__db_status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_WF_BmOrder__db_status ON [RedApp].[dbo].[WF_BmOrder] ([Db_Status]);
    PRINT 'created NC_IX_WF_BmOrder__db_status'
END

IF INDEXPROPERTY(OBJECT_ID('ART_ArtLnk'), 'NC_IX_ART_ArtLnk__CoLnk_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_ART_ArtLnk__CoLnk_FK ON [RedApp].[dbo].[ART_ArtLnk] ([CoLnk_FK], [WithdrawnDate], [FromDate], [ToDate], [Db_Status]) INCLUDE ([Art_FK], [Linked_Art_FK]);
    PRINT 'created NC_IX_ART_ArtLnk__CoLnk_FK'
END

IF INDEXPROPERTY(OBJECT_ID('Q_ART_ArtSta'), 'NC_IX_Q_ART_ArtSta__Art_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_Q_ART_ArtSta__Art_FK ON [RedApp].[dbo].[Q_ART_ArtSta] ([Art_FK], [WithdrawnDate], [FromDate], [Db_Status]);
    PRINT 'created NC_IX_Q_ART_ArtSta__Art_FK'
END

IF INDEXPROPERTY(OBJECT_ID('WF_BmOrder'), 'NC_IX_WF_BmOrder__Editor' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_WF_BmOrder__Editor ON [RedApp].[dbo].[WF_BmOrder] ([Editor]) INCLUDE ([CoOrderType_FK], [Db_Status])
    PRINT 'created NC_IX_WF_BmOrder__Editor'
END

IF INDEXPROPERTY(OBJECT_ID('PAC_PacStructure'), 'NC_IX_PAC_PacStructure__Descendant_Pac_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PAC_PacStructure__Descendant_Pac_FK ON [RedApp].[dbo].[PAC_PacStructure] ([Descendant_Pac_FK], [LevelPath], [Db_Status]);
    PRINT 'created NC_IX_PAC_PacStructure__Descendant_Pac_FK'
END

IF INDEXPROPERTY(OBJECT_ID('PRD_PrdAdrInducer'), 'NC_IX_PRD_PrdAdrInducer__Prd_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PRD_PrdAdrInducer__Prd_FK ON [RedApp].[dbo].[PRD_PrdAdrInducer] ([Prd_FK], [Db_Status]);
    PRINT 'created NC_IX_PRD_PrdAdrInducer__Prd_FK'
END

IF INDEXPROPERTY(OBJECT_ID('Q_WF_TaskRemark'), 'NC_IX_Q_WF_TaskRemark__TcTaskRemarkType_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_Q_WF_TaskRemark__TcTaskRemarkType_FK ON [RedApp].[dbo].[Q_WF_TaskRemark] ([TcTaskRemarkType_FK], [DropDt]) INCLUDE ([Task_FK]);
    PRINT 'created NC_IX_Q_WF_TaskRemark__TcTaskRemarkType_FK'
END

-- IX_ART_Art__Stat__wIncludes
IF INDEXPROPERTY(OBJECT_ID('dbo.ART_Art'), 'IX_ART_Art__Stat__wIncludes' , 'IndexID' ) IS NOT NULL BEGIN;
    DROP INDEX dbo.ART_Art.IX_ART_Art__Stat__wIncludes;
    PRINT 'drop IX_ART_Art__Stat__wIncludes'
END;
IF INDEXPROPERTY(OBJECT_ID('dbo.ART_Art'), 'IX_ART_Art__Stat__wIncludes' , 'IndexID' ) IS NULL BEGIN;
    CREATE INDEX IX_ART_Art__Stat__wIncludes ON [RedApp].[dbo].[ART_Art] ([Db_Status]) INCLUDE ([Prd_FK], [CoCat_FK], [Pharmacode], [IsOfficial], [PubScope_BIN], [CreateDt], [LastModifiedDt], [Db_InsDt], [Db_InsUser], [Db_UpdDt], [Db_UpdUser], [Db_Timestamp]);;
    PRINT 'created IX_ART_Art__Stat__wIncludes';
END;
GO

IF INDEXPROPERTY(OBJECT_ID('Ia_Sb'), 'NC_IX_Ia_Sb__PinPt' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_Ia_Sb__PinPt ON [RedApp].[dbo].[Ia_Sb] ([PinPt]) INCLUDE ([IaId], [SbId], [IaCoRatingId]);
    PRINT 'created NC_IX_Ia_Sb__PinPt'
END

IF INDEXPROPERTY(OBJECT_ID('BM_BmCoding'), 'NC_IX_BM_BmCoding__MeddraLowLevelTerm_FK_IsIndication' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_BM_BmCoding__MeddraLowLevelTerm_FK_IsIndication ON [RedApp].[dbo].[BM_BmCoding] ([MeddraLowLevelTerm_FK], [IsIndication]) INCLUDE ([BmRev_FK]);
    PRINT 'created NC_IX_BM_BmCoding__MeddraLowLevelTerm_FK_IsIndication'
END

IF INDEXPROPERTY(OBJECT_ID('PRD_PrdDeclarationOfConformity'), 'NC_IX_PRD_PrdDeclarationOfConformity__WithdrawnDate_ExpirationDate' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PRD_PrdDeclarationOfConformity__WithdrawnDate_ExpirationDate ON [RedApp].[dbo].[PRD_PrdDeclarationOfConformity] ([WithdrawnDate], [ExpirationDate], [FromDate], [Db_Status]) INCLUDE ([Prd_FK], [RevisionDate], [EuFlag]);
    PRINT 'created NC_IX_PRD_PrdDeclarationOfConformity__WithdrawnDate_ExpirationDate'
END


IF INDEXPROPERTY(OBJECT_ID('SbLink'), 'NC_IX_SbLink__LinkTypeId_IsDeleted' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_SbLink__LinkTypeId_IsDeleted ON [RedApp].[dbo].[SbLink] ([LinkTypeId], [IsDeleted]) INCLUDE ([SbId], [LinkedSbId]);
    PRINT 'created NC_IX_SbLink__LinkTypeId_IsDeleted'
END

-- NC_IX_PAC_PacStructure__Descendant_Pac_FK
IF INDEXPROPERTY(OBJECT_ID('dbo.PAC_PacStructure'), 'NC_IX_PAC_PacStructure__Descendant_Pac_FK' , 'IndexID' ) IS NOT NULL BEGIN;
    DROP INDEX dbo.PAC_PacStructure.NC_IX_PAC_PacStructure__Descendant_Pac_FK;
    PRINT 'drop NC_IX_PAC_PacStructure__Descendant_Pac_FK'
END;
IF INDEXPROPERTY(OBJECT_ID('PAC_PacStructure'), 'NC_IX_PAC_PacStructure__Descendant_Pac_FK' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PAC_PacStructure__Descendant_Pac_FK ON [RedApp].[dbo].[PAC_PacStructure] ([Descendant_Pac_FK], [Db_Status])
    INCLUDE([LevelPath]);
    PRINT 'created NC_IX_PAC_PacStructure__Descendant_Pac_FK'
END

IF INDEXPROPERTY(OBJECT_ID('BM_BmVerContent'), 'NC_IX_BM_BmVerContent__Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_BM_BmVerContent__Db_Status ON [RedApp].[dbo].[BM_BmVerContent] ([Db_Status]) INCLUDE ([BmVer_FK], [BmContent_FK]);
    PRINT 'created NC_IX_BM_BmVerContent__Db_Status'
END

IF INDEXPROPERTY(OBJECT_ID('PRD_PrdSta'), 'NC_IX_PRD_PrdSta__FromDate' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PRD_PrdSta__FromDate ON [RedApp].[dbo].[PRD_PrdSta] ([FromDate]) INCLUDE ([CoSta_FK]);
    PRINT 'created NC_IX_PRD_PrdSta__FromDate'
END

IF INDEXPROPERTY(OBJECT_ID('IND_MeddraPreferredTermDes'), 'NC_IX_IND_MeddraPreferredTermDes__MeddraPreferredTerm_FK_Lang' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_IND_MeddraPreferredTermDes__MeddraPreferredTerm_FK_Lang ON [RedApp].[dbo].[IND_MeddraPreferredTermDes] ([MeddraPreferredTerm_FK], [Lang]) INCLUDE ([Descr]);
    PRINT 'created NC_IX_IND_MeddraPreferredTermDes__MeddraPreferredTerm_FK_Lang'
END


IF INDEXPROPERTY(OBJECT_ID('CM_Cm'), 'NC_IX_CM_Cm__Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_CM_Cm__Db_Status ON [RedApp].[dbo].[CM_Cm] ([Db_Status]) INCLUDE ([CMNr_Ext], [CMRevNr_Ext]);
    PRINT 'created NC_IX_CM_Cm__Db_Status'
END

-- IX_Sb__IsSwiss_IsDel
IF INDEXPROPERTY(OBJECT_ID('dbo.Sb'), 'IX_Sb__IsSwiss_IsDel' , 'IndexID' ) IS NOT NULL BEGIN;
    DROP INDEX dbo.Sb.IX_Sb__IsSwiss_IsDel;
END;
IF INDEXPROPERTY(OBJECT_ID('Sb'), 'IX_Sb__IsSwiss_IsDel' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX IX_Sb__IsSwiss_IsDel ON [RedApp].[dbo].[Sb] ([IsSwissSb], [IsDeleted]) INCLUDE ([SbNr]);
    PRINT 'created IX_Sb__IsSwiss_IsDel'
END


IF INDEXPROPERTY(OBJECT_ID('CM_CmPrd'), 'NC_IX_CM_CmPrd__Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_CM_CmPrd__Db_Status ON [RedApp].[dbo].[CM_CmPrd] ([Db_Status]) INCLUDE ([Cm_FK], [Prd_FK], [ToDate]);
    PRINT 'created NC_IX_CM_CmPrd__Db_Status'
END


IF INDEXPROPERTY(OBJECT_ID('PRD_AtcDes'), 'NC_IX_PRD_AtcDes__Lang_Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_PRD_AtcDes__Lang_Db_Status ON [RedApp].[dbo].[PRD_AtcDes] ([Lang], [Db_Status]) INCLUDE ([Atc_FK], [Descr]);
    PRINT 'created NC_IX_PRD_AtcDes__Lang_Db_Status'
END


IF INDEXPROPERTY(OBJECT_ID('GRP_GrpDes'), 'NC_IX_GRP_GrpDes__Lang' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_GRP_GrpDes__Lang ON [RedApp].[dbo].[GRP_GrpDes] ([Lang]) INCLUDE ([Grp_FK], [Descr]);
    PRINT 'created NC_IX_GRP_GrpDes__Lang'
END

IF INDEXPROPERTY(OBJECT_ID('CPT_CptCalcCompositionSb'), 'NC_IX_CPT_CptCalcCompositionSb__Sb_Fk_Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_CPT_CptCalcCompositionSb__Sb_Fk_Db_Status ON [RedApp].[dbo].[CPT_CptCalcCompositionSb] ([Sb_FK], [Db_Status]);
    PRINT 'created NC_IX_CPT_CptCalcCompositionSb__Sb_Fk_Db_Status'
END

IF INDEXPROPERTY(OBJECT_ID('BM_Dos'), 'NC_IX_BM_Dos__BmRev_FK_Db_Status' , 'IndexID' ) IS NULL 
BEGIN;
    CREATE INDEX NC_IX_BM_Dos__BmRev_FK_Db_Status ON [RedApp].[dbo].[BM_Dos] ([BmRev_FK], [Db_Status]);
    PRINT 'created NC_IX_BM_Dos__BmRev_FK_Db_Status'
END

IF INDEXPROPERTY(OBJECT_ID('CDS_CdsDose'), 'NC_IX_CDS_CdsDose__Cds_FK_Db_Status' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_CDS_CdsDose__Cds_FK_Db_Status ON [RedApp].[dbo].[CDS_CdsDose] ([Cds_FK], [Db_Status]);
    PRINT 'created NC_IX_CDS_CdsDose__Cds_FK_Db_Status'
END


