Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8824241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:33:46+00:00 2026-06-14T06:33:46+00:00

This view is taking me HOURS to execute: USE [SalesDWH] GO /****** Object: View

  • 0

This view is taking me HOURS to execute:

USE [SalesDWH]
GO

/****** Object:  View [dbo].[FirstLastEstablished]    Script Date: 10/29/2012 15:12:47 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER view [dbo].[FirstLastEstablished] as


with cte_min as(

select 
      a.client_id
      ,a.specimen_source
      ,a.received_date
from
      millennium_dw_dev..F_ACCESSION_DAILY a
join
            (select distinct
                  f.client_id
                  ,f.received_date
                  ,f.accession_daily_key
            from 
                  millennium_dw_dev..F_ACCESSION_DAILY f
            join 
                  (select CLIENT_ID, MIN(received_date) MinRecDate
                  from millennium_dw_dev..F_ACCESSION_DAILY
                  group by CLIENT_ID) i
            on f.CLIENT_ID=i.CLIENT_ID
            and f.RECEIVED_DATE=i.MinRecDate) b
on    
      a.ACCESSION_DAILY_KEY=b.ACCESSION_DAILY_KEY 

)

,
cte_max as 
(

select 
      a.client_id
      ,a.specimen_source
      ,a.received_date
from
      millennium_dw_dev..F_ACCESSION_DAILY a
join
            (select distinct
                  f.client_id
                  ,f.received_date
                  ,f.accession_daily_key
            from 
                  millennium_dw_dev..F_ACCESSION_DAILY f
            join 
                  (select CLIENT_ID, max(received_date) MaxRecDate
                  from millennium_dw_dev..F_ACCESSION_DAILY
                  group by CLIENT_ID) i
            on f.CLIENT_ID=i.CLIENT_ID
            and f.RECEIVED_DATE=i.MaxRecDate) b
on    
      a.ACCESSION_DAILY_KEY=b.ACCESSION_DAILY_KEY 

)
,


cte_est as

(

select distinct client_id, MLIS_DATE_ESTABLISHED
from millennium_dw_dev..D_CLIENT
where REC_ACTIVE_FLG=1
and MLIS_DATE_ESTABLISHED is not null
)

,

mainQuery as(
select distinct
      f.client_id
      ,cmin.specimen_source first_specimen_source
      ,cmin.received_date first_received
      ,cmax.specimen_source last_specimen_source
      ,cmax.received_date last_received
      ,cest.MLIS_DATE_ESTABLISHED MLIS_DATE_ESTABLISHED
from millennium_dw_dev..F_ACCESSION_DAILY f
left join cte_max cmax
on cmax.CLIENT_ID=f.CLIENT_ID
left join cte_min cmin
on cmin.CLIENT_ID=f.CLIENT_ID
left join cte_est cest
on cest.CLIENT_ID=f.CLIENT_ID
)
,
DateDifferences
as

(
SELECT
client_id,
   (DATEDIFF(dd, MLIS_DATE_ESTABLISHED,first_received) + 1)
  -(DATEDIFF(wk, MLIS_DATE_ESTABLISHED,first_received) * 2)
  -(CASE WHEN DATENAME(dw, first_received) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, MLIS_DATE_ESTABLISHED) = 'Saturday' THEN 1 ELSE 0 END)
 -(case when cast('01/01/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('05/26/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('07/04/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('09/01/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('11/27/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('12/25/2008 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('01/01/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('05/25/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('07/03/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('09/07/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('11/26/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('12/25/2009 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('01/01/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('05/31/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('07/05/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('09/06/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('11/25/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('12/24/2010 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('01/03/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('05/30/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('07/04/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('09/05/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('11/24/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('12/26/2011 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('01/02/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('05/28/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('07/04/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('09/03/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('11/22/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)
-(case when cast('12/25/2012 ' as date) between MLIS_DATE_ESTABLISHED and first_received then 1 else 0 end)


   DifferenceExcludingWeekends
  from
  mainQuery
)
,
Territory 
as
(
select distinct c.client_id,c.MLIS_TERRITORY,s.REGION_NAME
from millennium_dw_dev..D_CLIENT c
left join millennium_dw_dev..D_SALES_HIERARCHY s
on s.TERRITORY_NAME=c.MLIS_TERRITORY
where c.REC_ACTIVE_FLG=1
and s.REC_ACTIVE_FLG=1

)

select mainQuery.*,d.DifferenceExcludingWeekends,Territory.MLIS_TERRITORY,Territory.REGION_NAME
from mainQuery
left join DateDifferences d
on mainQuery.CLIENT_ID=d.CLIENT_ID
left join Territory
on mainQuery.CLIENT_ID=Territory.CLIENT_ID

GO

I did the sql server database tuning on this, and created all the recommended indexes. After 15min of executing the view again, I stopped it.

Are there obvious things that you could kindly point out to me about my query to optimize it more and improve execution time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:33:48+00:00Added an answer on June 14, 2026 at 6:33 am

    I don’t think you are utilizing the “Common” aspect of a CTE. Try putting table: millennium_dw_dev..F_ACCESSION_DAILY in a CTE first (Only the necessary fields and records if possible). Your code selects from this table 8 times (and is this table local?).

    Several of your other CTE’s are referenced once, but I can see how one builds on the other. Yes they can help to break-down a complex query into more managable parts, but it may not help with performance.

    Performance may not be an issue here, but getting those hard-coded dates into some sort of Holiday/Day-Off table would help as well. Having a date table may be a better route than all these calculations.

    Depending on the size of data, putting this into a stored procedure would allow the use of indexed temp tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This view query is taking 2min to load 500 000 lines. I'm using EF
I have this list view that is taking up the whole page. I want
I'm dealing with a problem here. There's this view that I use to see
How can I achieve this view with CSS: ------------------TITLE or TITLE------------------ I have <div
I have this view @model IEnumerable<ViewModelRound2> ... <snip> ... @{ using (Html.BeginForm(new { round1Ring3Bids
I have this view (DetailsContainer, first class in code section of this question) with
I want to implement this view But I have one question: The text which
I created a new View (LogView) in Infrastructure.Module project. This view will be used
I have a Django view called change_priority. I'm posting a request to this view
I'm displaying a UIView with a UILabel on it and this view&label become blurry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.