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.
For this query:
You want an index on
ZipCodeor at least whereZipCodeis the first column:The resulting execution plan should be a scan of the index, which is much faster than processing the original table (and aggregating to get the distinct zip codes).
You could also change your existing index to
(zipcode, city, address1, address2). This makes the index more useful (in my opinion), becausezipcodeis more likely to be used for filtering thanaddress1. However, that index will be larger than one only onzipcode.