|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 11:36 PM
Points: 39,
Visits: 481
|
|
Hi,
I'm a beginner in index.
I've table and data as following,
CREATE TABLE [dbo].[Zip]( [City_Cd] [varchar](100) NULL, [Zip_Cd] [varchar](10) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Zip] ([City_Cd], [Zip_Cd]) VALUES (N'Mobile', N'36601') INSERT [dbo].[Zip] ([City_Cd], [Zip_Cd]) VALUES (N'New Orleans', N'70121') INSERT [dbo].[Zip] ([City_Cd], [Zip_Cd]) VALUES (N'Luling', N'70070') /****** Object: Table [dbo].[State] Script Date: 12/26/2012 19:01:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[State]( [State_Cd] [char](2) NULL, [Descr] [varchar](150) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[State] ([State_Cd], [Descr]) VALUES (N'AL', N'Alabama') INSERT [dbo].[State] ([State_Cd], [Descr]) VALUES (N'LA', N'Louisiana') /****** Object: Table [dbo].[City] Script Date: 12/26/2012 19:01:30 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[City]( [State_Cd] [char](2) NULL, [City_Cd] [varchar](100) NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[City] ([State_Cd], [City_Cd]) VALUES (N'AL', N'Mobile') INSERT [dbo].[City] ([State_Cd], [City_Cd]) VALUES (N'LA', N'New Orleans') INSERT [dbo].[City] ([State_Cd], [City_Cd]) VALUES (N'LA', N'Luling')
Then, I execute SQL as following
SELECT s.[Descr],c.City_Cd FROM dbo.[State] s INNER JOIN dbo.[City] c ON s.[State_Cd] = c.[State_Cd]
The Execution Plan as following,

Then, I create an index as following
CREATE NONCLUSTERED INDEX [IX_State_State_Cd_001] ON [dbo].[State] ( [State_Cd] ASC ) INCLUDE ( [Descr]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_City_State_Cd_001] ON [dbo].[City] ( [State_Cd] ASC ) INCLUDE ( [City_Cd]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
Then, my Execution Plan as following

How do I want to make both of them (State and City) is Index Seek?
If it can, please guide me
Really looking for help
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 2:19 AM
Points: 2,018,
Visits: 4,915
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 11:36 PM
Points: 39,
Visits: 481
|
|
Adi Cohn-120898 (12/26/2012) When SQL Server has to read very small tables, it many times uses table scan. In such small size, it doesn't make a difference.
Adi
Sir,
If large table, the index scan will change into index seek?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 2:19 AM
Points: 2,018,
Visits: 4,915
|
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:07 PM
Points: 37,687,
Visits: 29,946
|
|
idyana (12/26/2012) How do I want to make both of them (State and City) is Index Seek?
You can't.
A seek requires something to search for. Something to filter on. Your query requests the entire of two tables. The only sensible way for the entire of two tables to be queried is with a table scan.
This is true no matter how large the tables are (in fact in this case, larger tables would almost certainly result in two table scans because a table scan is the optimal way to read the entire of a table). There is no possible way for your query to use two index seeks. One index seek comes from the join, but there's nothing seekable for the other. If you added a where clause to filter for specific cities or states you might see index seeks for both, because there would be something to search for.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 19, 2013 11:36 PM
Points: 39,
Visits: 481
|
|
|
|
|