Consider the following relations: Product (name, production-year, rating, compan
ID: 3789064 • Letter: C
Question
Consider the following relations: Product (name, production-year, rating, company-name) Company (name, state, employee-num) Assume each product is produced by just one company, whose name is mentioned in the company- name attribute of the Product relation. Attributes name are the primary key for relations Product and Company. Attribute company-name is a foreign key from relation Product to relation Company. Attribute rating show's how popular a product is and its values are between 1-5. The following statistics are available about the relations. The following query returns the products with rating of 5 that are produced after 2000 and the states of their companies. SELECT p.name, c.state FROM Product p, Company c WHERE p.company-name = c.name and p.production-year > 2000 and p.rating = 5 Suggest an optimized logical query plan for the above query. Then, estimate the size of each intermediate relation in your query plan. By an intermediate relation, we mean the relation created after each selection or join.Explanation / Answer
<?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="1" StatementId="1" StatementOptmLevel="FULL" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" StatementSubTreeCost="0.00657552" StatementText="Select p.Name,c.State from Product p,
Company c where p.CompanyName=c.Name
and p.Year>2000 and p.rating=5" StatementType="SELECT" QueryHash="0x6617DA9250B666E4" QueryPlanHash="0x26D47CB2F04C6BFA">
<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="24" CompileTime="31" CompileCPU="6" CompileMemory="208">
<RelOp AvgRowSize="63" EstimateCPU="4.18E-06" EstimateIO="0" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Inner Join" NodeId="0" Parallel="false" PhysicalOp="Nested Loops" EstimatedTotalSubtreeCost="0.00657552">
<OutputList>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="Name" />
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Company]" Alias="[c]" Column="State" />
</OutputList>
<NestedLoops Optimized="false">
<OuterReferences>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="CompanyName" />
</OuterReferences>
<RelOp AvgRowSize="56" EstimateCPU="0.0001603" EstimateIO="0.003125" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Table Scan" NodeId="2" Parallel="false" PhysicalOp="Table Scan" EstimatedTotalSubtreeCost="0.0032853" TableCardinality="3">
<OutputList>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="Name" />
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="CompanyName" />
</OutputList>
<TableScan Ordered="false" ForcedIndex="false" NoExpandHint="false">
<DefinedValues>
<DefinedValue>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="Name" />
</DefinedValue>
<DefinedValue>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="CompanyName" />
</DefinedValue>
</DefinedValues>
<Object Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" IndexKind="Heap" />
<Predicate>
<ScalarOperator ScalarString="[Test].[dbo].[Product].[rating] as [p].[rating]=(5) AND CONVERT_IMPLICIT(int,[Test].[dbo].[Product].[Year] as [p].[Year],0)>(2000)">
<Logical Operation="AND">
<ScalarOperator>
<Compare CompareOp="EQ">
<ScalarOperator>
<Identifier>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="rating" />
</Identifier>
</ScalarOperator>
<ScalarOperator>
<Const ConstValue="(5)" />
</ScalarOperator>
</Compare>
</ScalarOperator>
<ScalarOperator>
<Compare CompareOp="GT">
<ScalarOperator>
<Convert DataType="int" Implicit="true">
<ScalarOperator>
<Identifier>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="Year" />
</Identifier>
</ScalarOperator>
</Convert>
</ScalarOperator>
<ScalarOperator>
<Const ConstValue="(2000)" />
</ScalarOperator>
</Compare>
</ScalarOperator>
</Logical>
</ScalarOperator>
</Predicate>
</TableScan>
</RelOp>
<RelOp AvgRowSize="36" EstimateCPU="0.0001581" EstimateIO="0.003125" EstimateRebinds="0" EstimateRewinds="0" EstimateRows="1" LogicalOp="Clustered Index Seek" NodeId="4" Parallel="false" PhysicalOp="Clustered Index Seek" EstimatedTotalSubtreeCost="0.0032831" TableCardinality="3">
<OutputList>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Company]" Alias="[c]" Column="State" />
</OutputList>
<IndexScan Ordered="true" ScanDirection="FORWARD" ForcedIndex="false" ForceSeek="false" NoExpandHint="false">
<DefinedValues>
<DefinedValue>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Company]" Alias="[c]" Column="State" />
</DefinedValue>
</DefinedValues>
<Object Database="[Test]" Schema="[dbo]" Table="[Company]" Index="[PK_Company]" Alias="[c]" IndexKind="Clustered" />
<SeekPredicates>
<SeekPredicateNew>
<SeekKeys>
<Prefix ScanType="EQ">
<RangeColumns>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Company]" Alias="[c]" Column="Name" />
</RangeColumns>
<RangeExpressions>
<ScalarOperator ScalarString="[Test].[dbo].[Product].[CompanyName] as [p].[CompanyName]">
<Identifier>
<ColumnReference Database="[Test]" Schema="[dbo]" Table="[Product]" Alias="[p]" Column="CompanyName" />
</Identifier>
</ScalarOperator>
</RangeExpressions>
</Prefix>
</SeekKeys>
</SeekPredicateNew>
</SeekPredicates>
</IndexScan>
</RelOp>
</NestedLoops>
</RelOp>
</QueryPlan>
</StmtSimple>
</Statements>
</Batch>
</BatchSequence>
</ShowPlanXML>