%3CLINGO-SUB%20id%3D%22lingo-sub-1960750%22%20slang%3D%22en-US%22%3EEnhance%20performance%20techniques%3Ause%20index%20on%20computed%20column%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1960750%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20want%20to%20share%20the%20following%20example%20on%20how%20you%20can%20use%20the%20%22computed%20column%22%20in%20some%20cases%2C%20to%20enhance%20your%20queries%20performance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20AdventureWorks%202019%20sample%20database%2C%20there%20are%20two%20tables%20%3CSTRONG%3ETransactionHistory%3C%2FSTRONG%3E%26nbsp%3B%20and%20%3CSTRONG%3ETransactionHistoryArchive%3C%2FSTRONG%3E%20(I%20changed%20the%20type%20of%20productID%20on%20%3CSTRONG%3ETransactionHistoryArchive%20%3C%2FSTRONG%3Efrom%20int%20to%20varchar)%2C%20the%20tables%20definitions%20are%20%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20TABLE%20%5BProduction%5D.%5BTransactionHistory%5D(%0A%20%5BTransactionID%5D%20%5Bint%5D%20IDENTITY(100000%2C1)%20NOT%20NULL%2C%0A%20%5BProductID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BReferenceOrderID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BReferenceOrderLineID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BTransactionDate%5D%20%5Bdatetime%5D%20NOT%20NULL%2C%0A%20%5BTransactionType%5D%20%5Bnchar%5D(1)%20NOT%20NULL%2C%0A%20%5BQuantity%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BActualCost%5D%20%5Bmoney%5D%20NOT%20NULL%2C%0A%20%5BModifiedDate%5D%20%5Bdatetime%5D%20NOT%20NULL%2C%0ACONSTRAINT%20%5BPK_TransactionHistory_TransactionID%5D%20PRIMARY%20KEY%20CLUSTERED%20%0A(%0A%20%5BTransactionID%5D%20ASC%0A)%0A%0A%0ACREATE%20TABLE%20%5BProduction%5D.%5BTransactionHistoryArchive%5D(%0A%20%5BTransactionID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BProductID%5D%20%5Bvarchar%5D(10)%20NOT%20NULL%2C%0A%20%5BReferenceOrderID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BReferenceOrderLineID%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BTransactionDate%5D%20%5Bdatetime%5D%20NOT%20NULL%2C%0A%20%5BTransactionType%5D%20%5Bnchar%5D(1)%20NOT%20NULL%2C%0A%20%5BQuantity%5D%20%5Bint%5D%20NOT%20NULL%2C%0A%20%5BActualCost%5D%20%5Bmoney%5D%20NOT%20NULL%2C%0A%20%5BModifiedDate%5D%20%5Bdatetime%5D%20NOT%20NULL%2C%0ACONSTRAINT%20%5BPK_TransactionHistoryArchive_TransactionID%5D%20PRIMARY%20KEY%20CLUSTERED%20%0A(%0A%20%5BTransactionID%5D%20ASC%0A)%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELike%20in%20some%20situation%20in%20real%20environments%2C%20the%20ProductID%20column%20type%20in%20one%20of%20the%20tables%20is%20different%20than%20the%20other%2C%20usually%20due%20to%20a%20bad%20design.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20changed%20the%20ProductID%20from%20int%20to%20varchar%20to%20simulate%20the%20issue%2C%20and%20on%20both%20tables%20I%20created%20the%20index%20that%20covers%20the%20ProductID%20column%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ecreate%20index%20ix_transaction_history_productid%20on%20%5BProduction%5D.%5Btransactionhistory%5D%20(productid)%0A%0Acreate%20index%20ix_transaction_historyArchive_productid%20on%20%5BProduction%5D.%5Btransactionhistoryarchive%5D%20(productid)%20include%20(transactiondate)%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20though%2C%20if%20you%20are%20running%20an%20inner%20join%20query%20like%20the%20one%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20H.ProductID%2C%20a.TransactionDate%0A%20%20FROM%20%20%5BProduction%5D.%5BTransactionHistory%5D%20H%20%20inner%20join%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0A%20on%20H.productid%3D%20A.productid%20where%20h.productid%20%3D%202%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20execution%20plan%20is%20using%20index%20scan%20and%20not%20index%20seek.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture1.png%22%20style%3D%22width%3A%20515px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238112iB0A7975E283F6384%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture1.png%22%20alt%3D%22Picture1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3ESQL%20Server%20parse%20and%20compile%20time%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%20CPU%20time%20%3D%200%20ms%2C%20elapsed%20time%20%3D%200%20ms.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3E(450%20rows%20affected)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3ETable%20'TransactionHistory'.%20Scan%20count%2010%2C%20logical%20reads%2020%2C%20physical%20reads%200%2C%20page%20server%20reads%200%2C%20read-ahead%20reads%200%2C%20page%20server%20read-ahead%20reads%200%2C%20lob%20logical%20reads%200%2C%20lob%20physical%20reads%200%2C%20lob%20page%20server%20reads%200%2C%20lob%20read-ahead%20reads%200%2C%20lob%20page%20server%20read-ahead%20reads%200.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3ETable%20'TransactionHistoryArchive'.%20Scan%20count%201%2C%20logical%20reads%20641%2C%20physical%20reads%200%2C%20page%20server%20reads%200%2C%20read-ahead%20reads%200%2C%20page%20server%20read-ahead%20reads%200%2C%20lob%20logical%20reads%200%2C%20lob%20physical%20reads%200%2C%20lob%20page%20server%20reads%200%2C%20lob%20read-ahead%20reads%200%2C%20lob%20page%20server%20read-ahead%20reads%200.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3ESQL%20Server%20Execution%20Times%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3B%20CPU%20time%20%3D%2031%20ms%2C%26nbsp%3B%20elapsed%20time%20%3D%2030%20ms.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnother%20example%2C%20%26nbsp%3Bif%20you%20run%20an%20inner%20join%20without%20the%20where%20clause%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20H.ProductID%2C%20a.TransactionDate%0A%20%20FROM%20%20%5BProduction%5D.%5BTransactionHistory%5D%20H%20%20inner%20join%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0A%20on%20H.productid%3D%20A.productid%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ethere%20will%20be%20index%20scans%20for%20both%20tables%20(Index%20Scan%20and%20Hash%20Join%2C%20hash%20is%20used%20usually%20when%20the%20two%20tables%20are%20Huge%20and%20%3CU%3Eunsorted%3C%2FU%3E.)%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture2.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238113i29990000B3294A5E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture2.png%22%20alt%3D%22Picture2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20select%20the%20table%20by%20two%20different%20values%20(different%20types)%20%2C%20numeric%20and%20text%20values%2C%20the%20query%20optimizer%20will%20choose%20a%20scan%20operation%20if%20the%20parameter%20type%20of%20the%20predicate%20%26nbsp%3Bis%20different%20than%20the%20column%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20A.TransactionDate%0A%20%20FROM%20%20%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0Awhere%20A.productid%20%3D%202%0A%0ASELECT%20A.TransactionDate%0A%20%20FROM%20%20%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0Awhere%20A.productid%20%3D%20'2'%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture3.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238114iB6212173621B1824%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture3.png%22%20alt%3D%22Picture3.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EObviously%2C%20the%20solution%20is%20to%20change%20the%20data%20type%20of%20column%20ProductID%20in%20one%20of%20the%20tables%20to%20avoid%20table%20scans%20and%20type%20conversion.%3C%2FP%3E%0A%3CP%3EBut%20you%20must%20put%20in%20consideration%20the%20following%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20impact%20of%20the%20change%20on%20application%20and%20ETL%20packages.%20Some%20will%20fail%20and%20some%20will%20need%20to%20be%20updated%20or%20become%20having%20a%20slower%20performance.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EA%20small%20change%20on%20database%20side%20may%20cause%20a%20lot%20of%20code%20modifications%20and%20updates%20on%20Applications.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20database%20design%2C%20like%20table%20relations%20and%20foreign%20keys..etc.%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture4.png%22%20style%3D%22width%3A%20462px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238116i89B6A601E9EF6CBD%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture4.png%22%20alt%3D%22Picture4.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-60px%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20bad%20design%20and%20overall%20performance%20issues%20will%20appear%20more%20when%20the%20application%5Cdatabase%20is%20live%20and%20tables%20are%20growing%20and%20become%20huge%20in%20size%2C%20in%20testing%20phases%20it%20may%20not%20appear.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20you%20may%20consider%20another%20solution%2C%20it%20will%20require%20a%20small%20update%20on%20database%20side%20and%20mostly%20requires%20no%20code%20update%20on%20TSQL%20scripts%20or%20on%20application%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%26nbsp%3BCreate%20a%20new%20persisted%20computed%20column%20on%20the%20table%3A%26nbsp%3B%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Ealter%20table%20%5BProduction%5D.%5Btransactionhistoryarchive%5D%20add%20productid_CC%20as%20convert(int%2C%20productid)%20%20persisted%E2%80%8B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3CLI%3ECreate%20an%20index%20on%20the%20new%20computed%20column%3A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E%E2%80%8Bcreate%20index%20ix_transaction_historyArchive_productid_CC%20on%20%5BProduction%5D.%5Btransactionhistoryarchive%5D%20(productid_CC)%20%20include%20(transactiondate)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewith%20the%20help%20of%20the%20computed%20column%20and%20it%20is%20index%2C%20lets%20try%20the%20same%20queries%20without%20doing%20any%20changes%20on%20them%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20H.ProductID%2C%20a.TransactionDate%0A%20%20FROM%20%20%5BProduction%5D.%5BTransactionHistory%5D%20H%20%20inner%20join%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0A%20on%20H.productid%3D%20A.productid%20where%20h.productid%20%3D%202%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ethe%20execution%20plan%2C%20the%20query%20optimized%20is%20using%20index%20seek%20for%20both%20tables%2C%20and%20number%20of%20logical%20reads%20decreased%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture5.png%22%20style%3D%22width%3A%20600px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238117i9B5029697EE4C472%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture5.png%22%20alt%3D%22Picture5.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3ESQL%20Server%20parse%20and%20compile%20time%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3E%26nbsp%3B%26nbsp%3B%20CPU%20time%20%3D%200%20ms%2C%20elapsed%20time%20%3D%200%20ms.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3E(450%20rows%20affected)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3ETable%20'TransactionHistory'.%20Scan%20count%2010%2C%20logical%20reads%2020%2C%20physical%20reads%200%2C%20page%20server%20reads%200%2C%20read-ahead%20reads%200%2C%20page%20server%20read-ahead%20reads%200%2C%20lob%20logical%20reads%200%2C%20lob%20physical%20reads%200%2C%20lob%20page%20server%20reads%200%2C%20lob%20read-ahead%20reads%200%2C%20lob%20page%20server%20read-ahead%20reads%200.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3ETable%20'TransactionHistoryArchive'.%20Scan%20count%201%2C%20logical%20reads%203%2C%20physical%20reads%200%2C%20page%20server%20reads%200%2C%20read-ahead%20reads%200%2C%20page%20server%20read-ahead%20reads%200%2C%20lob%20logical%20reads%200%2C%20lob%20physical%20reads%200%2C%20lob%20page%20server%20reads%200%2C%20lob%20read-ahead%20reads%200%2C%20lob%20page%20server%20read-ahead%20reads%200.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3E%26nbsp%3BSQL%20Server%20Execution%20Times%3A%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%20color%3D%22%233366FF%22%3E%26nbsp%3B%26nbsp%3B%20CPU%20time%20%3D%200%20ms%2C%26nbsp%3B%20elapsed%20time%20%3D%200%20ms.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20the%20query%20optimizer%20is%20using%20index%20seeks%3B%20%26nbsp%3B%3CSTRONG%3Eit%20is%20smart%20enough%20to%20implicitly%20use%20the%20index%20created%20on%20the%20computed%20column%20even%20if%20the%20computed%20column%20ProductID_CC%20is%20not%20explicitly%20mentioned%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3EIt%20will%20be%20used%20for%20queries%20that%20have%20%3CSTRONG%3EproductID%3C%2FSTRONG%3E%20in%20the%20where%20clause%20or%20use%20it%20in%20physical%20operations%20like%20joins.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20again%20If%20you%20select%20the%20table%20by%20two%20different%20values%20(different%20types)%20%2C%20numeric%20and%20text%20values%2C%20the%20query%20optimizer%20will%20choose%20the%20seek%20operation%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20A.TransactionDate%0A%20%20FROM%20%20%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0Awhere%20A.productid%20%3D%202%0A%0ASELECT%20A.TransactionDate%0A%20%20FROM%20%20%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0Awhere%20A.productid%20%3D%20'2'%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture6.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238118i168EB06DB10D2808%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture6.png%22%20alt%3D%22Picture6.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture8.png%22%20style%3D%22width%3A%20543px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238119i580A3D0D562E52E8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture8.png%22%20alt%3D%22Picture8.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20the%20inner%20join%20query%20without%20where%20clause%2C%20now%20is%20using%20adaptive%20join%20instead%20of%20Hash%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20H.ProductID%2C%20a.TransactionDate%0A%20%20FROM%20%20%5BProduction%5D.%5BTransactionHistory%5D%20H%20%20inner%20join%20%5BProduction%5D.%5BTransactionHistoryArchive%5D%20A%20%20%0A%20on%20H.productid%3D%20A.productid%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Picture9.png%22%20style%3D%22width%3A%20624px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F238120iBF18E5C975DB5EC4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Picture9.png%22%20alt%3D%22Picture9.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20that%20to%20create%20an%20index%20on%20computed%20column%2C%20the%20column%20must%20be%20deterministic%3A%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Findexes%2Findexes-on-computed-columns%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Findexes%2Findexes-on-computed-columns%3Fview%3Dsql-server-ver15%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1960750%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1960750%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecomputed%20column%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eenhance%20performance%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

 

I want to share the following example on how you can use the "computed column" in some cases, to enhance your queries performance.

 

On AdventureWorks 2019 sample database, there are two tables TransactionHistory  and TransactionHistoryArchive (I changed the type of productID on TransactionHistoryArchive from int to varchar), the tables definitions are :

 

 

CREATE TABLE [Production].[TransactionHistory](
	[TransactionID] [int] IDENTITY(100000,1) NOT NULL,
	[ProductID] [int] NOT NULL,
	[ReferenceOrderID] [int] NOT NULL,
	[ReferenceOrderLineID] [int] NOT NULL,
	[TransactionDate] [datetime] NOT NULL,
	[TransactionType] [nchar](1) NOT NULL,
	[Quantity] [int] NOT NULL,
	[ActualCost] [money] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_TransactionHistory_TransactionID] PRIMARY KEY CLUSTERED 
(
	[TransactionID] ASC
)


CREATE TABLE [Production].[TransactionHistoryArchive](
	[TransactionID] [int] NOT NULL,
	[ProductID] [varchar](10) NOT NULL,
	[ReferenceOrderID] [int] NOT NULL,
	[ReferenceOrderLineID] [int] NOT NULL,
	[TransactionDate] [datetime] NOT NULL,
	[TransactionType] [nchar](1) NOT NULL,
	[Quantity] [int] NOT NULL,
	[ActualCost] [money] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_TransactionHistoryArchive_TransactionID] PRIMARY KEY CLUSTERED 
(
	[TransactionID] ASC
)

 

 

 

 

Like in some situation in real environments, the ProductID column type in one of the tables is different than the other, usually due to a bad design.

 

I changed the ProductID from int to varchar to simulate the issue, and on both tables I created the index that covers the ProductID column:

 

 

create index ix_transaction_history_productid on [Production].[transactionhistory] (productid)

create index ix_transaction_historyArchive_productid on [Production].[transactionhistoryarchive] (productid) include (transactiondate)

 

 

 

Even though, if you are running an inner join query like the one below:

 

 

SELECT H.ProductID, a.TransactionDate
  FROM  [Production].[TransactionHistory] H  inner join [Production].[TransactionHistoryArchive] A  
 on H.productid= A.productid where h.productid = 2

 

 

 

The execution plan is using index scan and not index seek.

Picture1.png

 

SQL Server parse and compile time:

   CPU time = 0 ms, elapsed time = 0 ms.

 

(450 rows affected)

Table 'TransactionHistory'. Scan count 10, logical reads 20, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.

Table 'TransactionHistoryArchive'. Scan count 1, logical reads 641, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.

 

SQL Server Execution Times:

   CPU time = 31 ms,  elapsed time = 30 ms.

 

Another example,  if you run an inner join without the where clause:

 

 

SELECT H.ProductID, a.TransactionDate
  FROM  [Production].[TransactionHistory] H  inner join [Production].[TransactionHistoryArchive] A  
 on H.productid= A.productid

 

 

there will be index scans for both tables (Index Scan and Hash Join, hash is used usually when the two tables are Huge and unsorted.):

Picture2.png

 

 

 

 

If you select the table by two different values (different types) , numeric and text values, the query optimizer will choose a scan operation if the parameter type of the predicate  is different than the column:

 

 

SELECT A.TransactionDate
  FROM   [Production].[TransactionHistoryArchive] A  
where A.productid = 2

SELECT A.TransactionDate
  FROM   [Production].[TransactionHistoryArchive] A  
where A.productid = '2'

 

 

 

Picture3.png

 

Obviously, the solution is to change the data type of column ProductID in one of the tables to avoid table scans and type conversion.

But you must put in consideration the following:

  • The impact of the change on application and ETL packages. Some will fail and some will need to be updated or become having a slower performance.

A small change on database side may cause a lot of code modifications and updates on Applications.

 

  • The database design, like table relations and foreign keys..etc.:

Picture4.png

 

  • The bad design and overall performance issues will appear more when the application\database is live and tables are growing and become huge in size, in testing phases it may not appear.

 

But you may consider another solution, it will require a small update on database side and mostly requires no code update on TSQL scripts or on application:

  1.  Create a new persisted computed column on the table: 
    alter table [Production].[transactionhistoryarchive] add productid_CC as convert(int, productid)  persisted​
  2. Create an index on the new computed column:
    ​create index ix_transaction_historyArchive_productid_CC on [Production].[transactionhistoryarchive] (productid_CC)  include (transactiondate)

 

with the help of the computed column and it is index, lets try the same queries without doing any changes on them:

 

 

 

SELECT H.ProductID, a.TransactionDate
  FROM  [Production].[TransactionHistory] H  inner join [Production].[TransactionHistoryArchive] A  
 on H.productid= A.productid where h.productid = 2

 

the execution plan, the query optimized is using index seek for both tables, and number of logical reads decreased:

 

Picture5.png

 

SQL Server parse and compile time:

   CPU time = 0 ms, elapsed time = 0 ms.

(450 rows affected)

Table 'TransactionHistory'. Scan count 10, logical reads 20, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.

Table 'TransactionHistoryArchive'. Scan count 1, logical reads 3, physical reads 0, page server reads 0, read-ahead reads 0, page server read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob page server reads 0, lob read-ahead reads 0, lob page server read-ahead reads 0.

 SQL Server Execution Times:

   CPU time = 0 ms,  elapsed time = 0 ms.

 

Now the query optimizer is using index seeks;  it is smart enough to implicitly use the index created on the computed column even if the computed column ProductID_CC is not explicitly mentioned.

It will be used for queries that have productID in the where clause or use it in physical operations like joins.

 

And again If you select the table by two different values (different types) , numeric and text values, the query optimizer will choose the seek operation:

 

 

SELECT A.TransactionDate
  FROM   [Production].[TransactionHistoryArchive] A  
where A.productid = 2

SELECT A.TransactionDate
  FROM   [Production].[TransactionHistoryArchive] A  
where A.productid = '2'

 

 

 

Picture6.png

Picture8.png

 

And the inner join query without where clause, now is using adaptive join instead of Hash:

 

 

SELECT H.ProductID, a.TransactionDate
  FROM  [Production].[TransactionHistory] H  inner join [Production].[TransactionHistoryArchive] A  
 on H.productid= A.productid

 

 

Picture9.png

 

Note that to create an index on computed column, the column must be deterministic:

https://docs.microsoft.com/en-us/sql/relational-databases/indexes/indexes-on-computed-columns?view=s...