I am comming to this example
https://www.mssqltips.com/sqlservertip/2353/performance-advantages-of-sql-server-filtered-statistics/
CREATE TABLE MyRegionTable(idINT, Location
NVARCHAR(100),
USState CHAR(2))
GO
CREATE TABLE MySalesTable(idINT, detail
INT, quantity INT)
GO
CREATE CLUSTERED INDEX IDX_d1
ON MyRegionTable(id)
GO
CREATE INDEX IDX_MyRegionTable_name
ON MyRegionTable(Location)
GO
CREATE STATISTICS IDX_MyRegionTable_id_name
ON MyRegionTable(id,Location)
GO
CREATE CLUSTERED INDEX IDX_MySalesTable_id_detailON MySalesTable(id,detail)
GO
INSERT MyRegionTable VALUES(0,'Atlanta', 'GA')
INSERT MyRegionTable
VALUES(1, 'San Francisco', 'CA')
GO
SET NOCOUNT ON
-- MySalesTable will contain 1 row for Atlanta and 1000 rows for San Francisco
INSERT MySalesTable
VALUES(0,
0, 50)
DECLARE @i INT
SET @i =
1
WHILE @i <=1000 BEGIN
INSERT MySalesTable VALUES (1,@i, @i*3)
SET @i = @i +1
END
GO
UPDATE STATISTICS MyRegionTable
WITH fullscan
UPDATE STATISTICS MySalesTable
WITH fullscan
GO
--- So when come into the following statement
SELECT detail
FROM MyRegionTable JOIN MySalesTable
ON MyRegionTable.id = MySalesTable.id
WHERE Location='Atlanta'OPTION (recompile)
Image may be NSFW.
Clik here to view.
so we know the estimation is wrong. But I am question why the join query doesn't consider the stats of
IDX_MySalesTable_id_detail??????
Image may be NSFW.
Clik here to view.