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
--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
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_co unty] on dbo.Geographics ( Region, County ) with ( fillfactor = 95, sort_in_tempdb = on );
Then try the original code again.
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_co
Then try the original code again.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?