Link to home
Start Free TrialLog in
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
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial