The query below to get the distinct zip codes from the Address
table takes roughly 4 mins and 42 seconds. There are 1,006,699 records in the Address
table. The composite key for the table is Address1, Address2, City, ZipCode
.
There have been times when the query takes 5 seconds to run or even 1 mill seconds.
How do I improve the performance of the query?
Here is the SQL query:
SELECT DISTINCT ZipCode FROM Address
Here is the schema for the table:
CREATE TABLE [dbo].[Address]
(
[AddressID] [INT] IDENTITY(1,1) NOT NULL,
[Address1] [NVARCHAR](1000) NOT NULL,
[Address2] [NVARCHAR](1000) NOT NULL,
[City] [NVARCHAR](1000) NOT NULL,
[StateCd] [NVARCHAR](2) NULL,
[ZipCode] [NVARCHAR](10) NOT NULL,
PRIMARY KEY CLUSTERED
([Address1] ASC, [Address2] ASC, [City] ASC, [ZipCode] ASC)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Address] ADD DEFAULT ('') FOR [Address2]
GO
I can't seem to add an image of the execution plan.