SQL Query ...

Can someone help me make this query more efficient?

It appears to choke up quite a bit the more it is used...
WITH  CategoryTree
		AS (SELECT  r.catID, r.catName, 0 Level, r.parentID,
					CAST(r.catName AS NVarChar(Max)) AS "Path"
			FROM    vwSelectPartCategory r
			WHERE   r.parentID = 0
			
			UNION ALL
			
			SELECT  c.catID, c.catName, p.Level + 1 AS Level, c.parentID,
					CAST((p.path + ',' + c.catName) AS NVarChar(Max)) AS "Path"
			FROM    vwSelectPartCategory c
					INNER JOIN CategoryTree p
					  ON p.catID = c.parentID
		   )
		   
	SELECT [Path]
	FROM    CategoryTree
	Where 1 = 1
	And (@CatID Is Null Or catID = @CatID)
	Order By [Path]

Open in new window


p.s.   It's used to pull a breadcrumb style navigation from a shopping cart db
LVL 25
kevp75Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

derekkrommCommented:
First thing I would do is check your indexing.

Execute the query with an Execution Plan (ctrl+m prior to executing), and check for table scans and/or index scans. Your goal is to change these to Index Seeks by creating the correct indexes on the underlying tables.

If you can provide more information, like table structures and existing indexes, etc., it'll be easier to help.

The code itself looks clean - T-SQL just isn't meant to be used for processor-intensive processing like this (although its something you have to do from time-to-time)
0
LCSandman8301Commented:
do you have an index on the table vwSelectPartCategory.parentid, that includes catid and catname? or the underlying table that the view is built upon. do you have a copy of the execution plan? so we can see where the bottlenecks may be occuring?
0
dwkorCommented:
Get rid of (@CatId is null) or (CatID = @CatID). SQL Server would use scan regardless of @CatId value.  There are some service packs (different for every version 2005/2008/R2) that can alter this behavior with recompile option although it's not consistent and you should not rely on it.

And of course, make sure that indexes are correct.
if @CatId is null
begin
	;WITH  CategoryTree
	AS (
		SELECT  r.catID, r.catName, 0 Level, r.parentID,
				CAST(r.catName AS NVarChar(Max)) AS "Path"
		FROM    vwSelectPartCategory r
		WHERE   r.parentID = 0
				
		UNION ALL
				
		SELECT  c.catID, c.catName, p.Level + 1 AS Level, c.parentID,
				CAST((p.path + ',' + c.catName) AS NVarChar(Max)) AS "Path"
		FROM    vwSelectPartCategory c INNER JOIN CategoryTree p ON 
			p.catID = c.parentID
	)
	SELECT [Path]
	FROM    CategoryTree
	Order By [Path]
end
else begin
	;WITH  CategoryTree
	AS (
		SELECT  r.catID, r.catName, 0 Level, r.parentID,
				CAST(r.catName AS NVarChar(Max)) AS "Path"
		FROM    vwSelectPartCategory r
		WHERE   r.parentID = 0 and r.CatId = @CatId
				
		UNION ALL
				
		SELECT  c.catID, c.catName, p.Level + 1 AS Level, c.parentID,
				CAST((p.path + ',' + c.catName) AS NVarChar(Max)) AS "Path"
		FROM    vwSelectPartCategory c INNER JOIN CategoryTree p ON 
			p.catID = c.parentID
	)
	SELECT [Path]
	FROM    CategoryTree
	Order By [Path]
end

Open in new window

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

kevp75Author Commented:
execution plan XML
<?xml version="1.0" encoding="utf-16"?>
<ShowPlanXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.1" Build="10.50.1600.1" xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan">
  <BatchSequence>
    <Batch>
      <Statements>
        <StmtSimple StatementCompId="1" StatementEstRows="14.8103" StatementId="1" StatementOptmLevel="FULL" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" StatementSubTreeCost="0.07163" StatementText="	Declare @CatID		Int&#xD;&#xA;	&#xD;&#xA;	WITH  CategoryTree&#xD;&#xA;		AS (SELECT  r.catID, r.catName, 0 Level, r.parentID,&#xD;&#xA;					CAST(r.catName AS NVarChar(Max)) AS &quot;Path&quot;&#xD;&#xA;			FROM    vwSelectPartCategory r&#xD;&#xA;			WHERE   r.parentID = 0&#xD;&#xA;			&#xD;&#xA;			UNION ALL&#xD;&#xA;			&#xD;&#xA;			SELECT  c.catID, c.catName, p.Level + 1 AS Level, c.parentID,&#xD;&#xA;					CAST((p.path + ',' + c.catName) AS NVarChar(Max)) AS &quot;Path&quot;&#xD;&#xA;			FROM    vwSelectPartCategory c&#xD;&#xA;					INNER JOIN CategoryTree p&#xD;&#xA;					  ON p.catID = c.parentID&#xD;&#xA;		   )&#xD;&#xA;		   &#xD;&#xA;	SELECT [Path]&#xD;&#xA;	FROM    CategoryTree&#xD;&#xA;	Where 1 = 1&#xD;&#xA;	And (@CatID Is Null Or catID = @CatID)&#xD;&#xA;	Order By [Path]" StatementType="SELECT" QueryHash="0x4EE4ECCD2F342CC8" QueryPlanHash="0xD15B4032C04F63DA">
          <StatementSetOptions ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" NUMERIC_ROUNDABORT="false" QUOTED_IDENTIFIER="true" />
          <QueryPlan CachedPlanSize="32" CompileTime="15" CompileCPU="15" CompileMemory="312">
            <RelOp AvgRowSize="4023" EstimateCPU="0.000193863" EstimateIO="0.0112613" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="14.8103" LogicalOp="Sort" NodeId="0" Parallel="false" PhysicalOp="Sort" EstimatedTotalSubtreeCost="0.07163">
              <OutputList>
                <ColumnReference Column="Recr1019" />
              </OutputList>
              <MemoryFractions Input="1" Output="1" />
              <Sort Distinct="false">
                <OrderBy>
                  <OrderByColumn Ascending="true">
                    <ColumnReference Column="Recr1019" />
                  </OrderByColumn>
                </OrderBy>
                <RelOp AvgRowSize="4023" EstimateCPU="1.63453E-05" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="14.8103" LogicalOp="Filter" NodeId="1" Parallel="false" PhysicalOp="Filter" EstimatedTotalSubtreeCost="0.0601749">
                  <OutputList>
                    <ColumnReference Column="Recr1019" />
                  </OutputList>
                  <Filter StartupExpression="false">
                    <RelOp AvgRowSize="4027" EstimateCPU="1.20186E-07" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="24.0372" LogicalOp="Lazy Spool" NodeId="2" Parallel="false" PhysicalOp="Index Spool" EstimatedTotalSubtreeCost="0.0601586">
                      <OutputList>
                        <ColumnReference Column="Expr1024" />
                        <ColumnReference Column="Recr1015" />
                        <ColumnReference Column="Recr1016" />
                        <ColumnReference Column="Recr1017" />
                        <ColumnReference Column="Recr1018" />
                        <ColumnReference Column="Recr1019" />
                      </OutputList>
                      <Spool Stack="true">
                        <RelOp AvgRowSize="4027" EstimateCPU="2.40372E-08" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="24.0372" LogicalOp="Concatenation" NodeId="3" Parallel="false" PhysicalOp="Concatenation" EstimatedTotalSubtreeCost="0.0591774">
                          <OutputList>
                            <ColumnReference Column="Expr1024" />
                            <ColumnReference Column="Recr1015" />
                            <ColumnReference Column="Recr1016" />
                            <ColumnReference Column="Recr1017" />
                            <ColumnReference Column="Recr1018" />
                            <ColumnReference Column="Recr1019" />
                          </OutputList>
                          <Concat>
                            <DefinedValues>
                              <DefinedValue>
                                <ColumnReference Column="Expr1024" />
                                <ColumnReference Column="Expr1021" />
                                <ColumnReference Column="Expr1023" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Column="Recr1015" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Column="Recr1016" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Column="Recr1017" />
                                <ColumnReference Column="Expr1003" />
                                <ColumnReference Column="Expr1013" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Column="Recr1018" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                              </DefinedValue>
                              <DefinedValue>
                                <ColumnReference Column="Recr1019" />
                                <ColumnReference Column="Expr1004" />
                                <ColumnReference Column="Expr1014" />
                              </DefinedValue>
                            </DefinedValues>
                            <RelOp AvgRowSize="4027" EstimateCPU="2.40372E-07" EstimateIO="0" EstimateRebinds="24.0372" EstimateRewinds="0" EstimateRows="1" LogicalOp="Compute Scalar" NodeId="4" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="2.40372E-07">
                              <OutputList>
                                <ColumnReference Column="Expr1021" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                <ColumnReference Column="Expr1003" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                <ColumnReference Column="Expr1004" />
                              </OutputList>
                              <ComputeScalar>
                                <DefinedValues>
                                  <DefinedValue>
                                    <ColumnReference Column="Expr1021" />
                                    <ScalarOperator ScalarString="(0)">
                                      <Const ConstValue="(0)" />
                                    </ScalarOperator>
                                  </DefinedValue>
                                </DefinedValues>
                                <RelOp AvgRowSize="8049" EstimateCPU="2.1E-06" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="21" LogicalOp="Compute Scalar" NodeId="5" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="0.0183175">
                                  <OutputList>
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                    <ColumnReference Column="Expr1003" />
                                    <ColumnReference Column="Expr1004" />
                                  </OutputList>
                                  <ComputeScalar>
                                    <DefinedValues>
                                      <DefinedValue>
                                        <ColumnReference Column="Expr1003" />
                                        <ScalarOperator ScalarString="(0)">
                                          <Const ConstValue="(0)" />
                                        </ScalarOperator>
                                      </DefinedValue>
                                    </DefinedValues>
                                    <RelOp AvgRowSize="8045" EstimateCPU="5.71E-05" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="21" LogicalOp="Compute Scalar" NodeId="6" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="0.0180413">
                                      <OutputList>
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                        <ColumnReference Column="Expr1004" />
                                      </OutputList>
                                      <ComputeScalar>
                                        <DefinedValues>
                                          <DefinedValue>
                                            <ColumnReference Column="Expr1004" />
                                            <ScalarOperator ScalarString="CONVERT(nvarchar(max),[Staging.Turbo-Kits.com].[dbo].[TblPartCategories].[catName],0)">
                                              <Convert DataType="nvarchar(max)" Length="2147483647" Style="0" Implicit="false">
                                                <ScalarOperator>
                                                  <Identifier>
                                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                                  </Identifier>
                                                </ScalarOperator>
                                              </Convert>
                                            </ScalarOperator>
                                          </DefinedValue>
                                        </DefinedValues>
                                        <RelOp AvgRowSize="4031" EstimateCPU="0.0007851" EstimateIO="0.0171991" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="21" LogicalOp="Clustered Index Scan" NodeId="7" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="0.0179842" TableCardinality="571">
                                          <OutputList>
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                          </OutputList>
                                          <IndexScan Ordered="false" ForcedIndex="false" NoExpandHint="false">
                                            <DefinedValues>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                              </DefinedValue>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                              </DefinedValue>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                              </DefinedValue>
                                            </DefinedValues>
                                            <Object Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Index="[PK_TblPartCategories]" TableReferenceId="1" IndexKind="Clustered" />
                                            <Predicate>
                                              <ScalarOperator ScalarString="[Staging.Turbo-Kits.com].[dbo].[TblPartCategories].[parentID]=(0)">
                                                <Compare CompareOp="EQ">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                  <ScalarOperator>
                                                    <Const ConstValue="(0)" />
                                                  </ScalarOperator>
                                                </Compare>
                                              </ScalarOperator>
                                            </Predicate>
                                          </IndexScan>
                                        </RelOp>
                                      </ComputeScalar>
                                    </RelOp>
                                  </ComputeScalar>
                                </RelOp>
                              </ComputeScalar>
                            </RelOp>
                            <RelOp AvgRowSize="4027" EstimateCPU="2.01913E-06" EstimateIO="0" EstimateRebinds="24.0372" EstimateRewinds="0" EstimateRows="1.14463" LogicalOp="Assert" NodeId="17" Parallel="false" PhysicalOp="Assert" EstimatedTotalSubtreeCost="0.04086">
                              <OutputList>
                                <ColumnReference Column="Expr1023" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                <ColumnReference Column="Expr1013" />
                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                <ColumnReference Column="Expr1014" />
                              </OutputList>
                              <Assert StartupExpression="false">
                                <RelOp AvgRowSize="4027" EstimateCPU="2.01913E-06" EstimateIO="0" EstimateRebinds="24.0372" EstimateRewinds="0" EstimateRows="1.14463" LogicalOp="Inner Join" NodeId="18" Parallel="false" PhysicalOp="Nested Loops" EstimatedTotalSubtreeCost="0.04086">
                                  <OutputList>
                                    <ColumnReference Column="Expr1023" />
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                    <ColumnReference Column="Expr1013" />
                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                    <ColumnReference Column="Expr1014" />
                                  </OutputList>
                                  <NestedLoops Optimized="false">
                                    <OuterReferences>
                                      <ColumnReference Column="Expr1023" />
                                      <ColumnReference Column="Recr1008" />
                                      <ColumnReference Column="Recr1009" />
                                      <ColumnReference Column="Recr1010" />
                                      <ColumnReference Column="Recr1011" />
                                      <ColumnReference Column="Recr1012" />
                                    </OuterReferences>
                                    <RelOp AvgRowSize="4027" EstimateCPU="2.40372E-07" EstimateIO="0" EstimateRebinds="24.0372" EstimateRewinds="0" EstimateRows="1" LogicalOp="Compute Scalar" NodeId="19" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="2.40372E-07">
                                      <OutputList>
                                        <ColumnReference Column="Expr1023" />
                                        <ColumnReference Column="Recr1008" />
                                        <ColumnReference Column="Recr1009" />
                                        <ColumnReference Column="Recr1010" />
                                        <ColumnReference Column="Recr1011" />
                                        <ColumnReference Column="Recr1012" />
                                      </OutputList>
                                      <ComputeScalar>
                                        <DefinedValues>
                                          <DefinedValue>
                                            <ColumnReference Column="Expr1023" />
                                            <ScalarOperator ScalarString="[Expr1022]+(1)">
                                              <Arithmetic Operation="ADD">
                                                <ScalarOperator>
                                                  <Identifier>
                                                    <ColumnReference Column="Expr1022" />
                                                  </Identifier>
                                                </ScalarOperator>
                                                <ScalarOperator>
                                                  <Const ConstValue="(1)" />
                                                </ScalarOperator>
                                              </Arithmetic>
                                            </ScalarOperator>
                                          </DefinedValue>
                                        </DefinedValues>
                                        <RelOp AvgRowSize="4027" EstimateCPU="2.40372E-07" EstimateIO="0" EstimateRebinds="24.0372" EstimateRewinds="0" EstimateRows="1" LogicalOp="Lazy Spool" NodeId="20" Parallel="false" PhysicalOp="Table Spool" EstimatedTotalSubtreeCost="2.40372E-07">
                                          <OutputList>
                                            <ColumnReference Column="Expr1022" />
                                            <ColumnReference Column="Recr1008" />
                                            <ColumnReference Column="Recr1009" />
                                            <ColumnReference Column="Recr1010" />
                                            <ColumnReference Column="Recr1011" />
                                            <ColumnReference Column="Recr1012" />
                                          </OutputList>
                                          <Spool Stack="true" PrimaryNodeId="2" />
                                        </RelOp>
                                      </ComputeScalar>
                                    </RelOp>
                                    <RelOp AvgRowSize="8049" EstimateCPU="3.03723E-07" EstimateIO="0" EstimateRebinds="23.0372" EstimateRewinds="0" EstimateRows="3.03723" LogicalOp="Compute Scalar" NodeId="24" Parallel="false" PhysicalOp="Compute Scalar" EstimatedTotalSubtreeCost="0.0408577">
                                      <OutputList>
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                        <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                        <ColumnReference Column="Expr1013" />
                                        <ColumnReference Column="Expr1014" />
                                      </OutputList>
                                      <ComputeScalar>
                                        <DefinedValues>
                                          <DefinedValue>
                                            <ColumnReference Column="Expr1013" />
                                            <ScalarOperator ScalarString="[Recr1010]+(1)">
                                              <Arithmetic Operation="ADD">
                                                <ScalarOperator>
                                                  <Identifier>
                                                    <ColumnReference Column="Recr1010" />
                                                  </Identifier>
                                                </ScalarOperator>
                                                <ScalarOperator>
                                                  <Const ConstValue="(1)" />
                                                </ScalarOperator>
                                              </Arithmetic>
                                            </ScalarOperator>
                                          </DefinedValue>
                                          <DefinedValue>
                                            <ColumnReference Column="Expr1014" />
                                            <ScalarOperator ScalarString="([Recr1012]+N',')+[Staging.Turbo-Kits.com].[dbo].[TblPartCategories].[catName]">
                                              <Arithmetic Operation="ADD">
                                                <ScalarOperator>
                                                  <Arithmetic Operation="ADD">
                                                    <ScalarOperator>
                                                      <Identifier>
                                                        <ColumnReference Column="Recr1012" />
                                                      </Identifier>
                                                    </ScalarOperator>
                                                    <ScalarOperator>
                                                      <Const ConstValue="N','" />
                                                    </ScalarOperator>
                                                  </Arithmetic>
                                                </ScalarOperator>
                                                <ScalarOperator>
                                                  <Identifier>
                                                    <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                                  </Identifier>
                                                </ScalarOperator>
                                              </Arithmetic>
                                            </ScalarOperator>
                                          </DefinedValue>
                                        </DefinedValues>
                                        <RelOp AvgRowSize="4031" EstimateCPU="0.0007066" EstimateIO="0.0172776" EstimateRebinds="0" EstimateRewinds="23.0372" EstimateRows="3.03723" LogicalOp="Clustered Index Scan" NodeId="25" Parallel="false" PhysicalOp="Clustered Index Scan" EstimatedTotalSubtreeCost="0.0342623" TableCardinality="571">
                                          <OutputList>
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                            <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                          </OutputList>
                                          <IndexScan Ordered="false" ForcedIndex="false" NoExpandHint="false">
                                            <DefinedValues>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catID" />
                                              </DefinedValue>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                              </DefinedValue>
                                              <DefinedValue>
                                                <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="catName" />
                                              </DefinedValue>
                                            </DefinedValues>
                                            <Object Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Index="[PK_TblPartCategories]" TableReferenceId="2" IndexKind="Clustered" />
                                            <Predicate>
                                              <ScalarOperator ScalarString="[Staging.Turbo-Kits.com].[dbo].[TblPartCategories].[parentID]=[Recr1008]">
                                                <Compare CompareOp="EQ">
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Database="[Staging.Turbo-Kits.com]" Schema="[dbo]" Table="[TblPartCategories]" Column="parentID" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                  <ScalarOperator>
                                                    <Identifier>
                                                      <ColumnReference Column="Recr1008" />
                                                    </Identifier>
                                                  </ScalarOperator>
                                                </Compare>
                                              </ScalarOperator>
                                            </Predicate>
                                          </IndexScan>
                                        </RelOp>
                                      </ComputeScalar>
                                    </RelOp>
                                  </NestedLoops>
                                </RelOp>
                                <Predicate>
                                  <ScalarOperator ScalarString="CASE WHEN [Expr1023]&gt;(100) THEN (0) ELSE NULL END">
                                    <IF>
                                      <Condition>
                                        <ScalarOperator>
                                          <Compare CompareOp="GT">
                                            <ScalarOperator>
                                              <Identifier>
                                                <ColumnReference Column="Expr1023" />
                                              </Identifier>
                                            </ScalarOperator>
                                            <ScalarOperator>
                                              <Const ConstValue="(100)" />
                                            </ScalarOperator>
                                          </Compare>
                                        </ScalarOperator>
                                      </Condition>
                                      <Then>
                                        <ScalarOperator>
                                          <Const ConstValue="(0)" />
                                        </ScalarOperator>
                                      </Then>
                                      <Else>
                                        <ScalarOperator>
                                          <Const ConstValue="NULL" />
                                        </ScalarOperator>
                                      </Else>
                                    </IF>
                                  </ScalarOperator>
                                </Predicate>
                              </Assert>
                            </RelOp>
                          </Concat>
                        </RelOp>
                      </Spool>
                    </RelOp>
                    <Predicate>
                      <ScalarOperator ScalarString="[@CatID] IS NULL OR [Recr1015]=[@CatID]">
                        <Logical Operation="OR">
                          <ScalarOperator>
                            <Identifier>
                              <ColumnReference Column="ConstExpr1020">
                                <ScalarOperator>
                                  <Compare CompareOp="IS">
                                    <ScalarOperator>
                                      <Identifier>
                                        <ColumnReference Column="@CatID" />
                                      </Identifier>
                                    </ScalarOperator>
                                    <ScalarOperator>
                                      <Const ConstValue="NULL" />
                                    </ScalarOperator>
                                  </Compare>
                                </ScalarOperator>
                              </ColumnReference>
                            </Identifier>
                          </ScalarOperator>
                          <ScalarOperator>
                            <Compare CompareOp="EQ">
                              <ScalarOperator>
                                <Identifier>
                                  <ColumnReference Column="Recr1015" />
                                </Identifier>
                              </ScalarOperator>
                              <ScalarOperator>
                                <Identifier>
                                  <ColumnReference Column="@CatID" />
                                </Identifier>
                              </ScalarOperator>
                            </Compare>
                          </ScalarOperator>
                        </Logical>
                      </ScalarOperator>
                    </Predicate>
                  </Filter>
                </RelOp>
              </Sort>
            </RelOp>
          </QueryPlan>
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>

Open in new window

0
kevp75Author Commented:
@LCSandman8  yes, but it only includes catName

table structure

CREATE VIEW [dbo].[vwSelectPartCategory]  WITH SCHEMABINDING

AS
Select catID, parentID, catName, catDescription, catAdded, catMetaKeywords, catMetaTitle, catMetaDesc, catShowInNav
From dbo.TblPartCategories;

GO

Open in new window

0
derekkrommCommented:
Try adding this index:

create nonclustered index ix_TblPartCategories_Path on dbo.TblPartCategories (
catID)
include (
parentID, catName)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevp75Author Commented:
believe me, i wouldn't do it like this unless I had to, but boss man said "this is what I want"
0
derekkrommCommented:
we've all been there :)
0
kevp75Author Commented:
LOL...  ok.  Index created...let's see how that goes
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.