Avatar of elucero
elucero

asked on 

sql get all existing values??

what can I do instead of the following?  It takes too long.

--get all values
select  distinct
a.[Year],
b.Region,
b.County
from
(select distinct [year] from [dbo].[YTDDate] as a with(nolock)) a,
(select distinct Region, County from [dbo].[Geographics] as b with(nolock) ) b
SQL

Avatar of undefined
Last Comment
PortletPaul
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Which part of the query takes too long?
select distinct [year] from [dbo].[YTDDate] as a with(nolock)
or
select distinct Region, County from [dbo].[Geographics] as b with(nolock)
?
How many rows is it returning? How many rows are in the original tables?
You can remove the distinct from the outer query, like so:

select
a.[Year],
b.Region,
b.County
from
(select distinct [year] from [dbo].[YTDDate] as a with(nolock)) a,
(select distinct Region, County from [dbo].[Geographics] as b with(nolock) ) b

If that doesn't improve the speed enough, add this index on the Geographics table:

create nonclustered index [Geographics__ix_region_county] on dbo.Geographics ( Region, County ) with ( fillfactor = 95, sort_in_tempdb = on );

Then try the original code again.
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo