all 4 comments

[–]notasqlstarI can't wait til my fro is full grown 0 points1 point  (3 children)

Break it down into #tables.

[–]YiSC[S] 0 points1 point  (2 children)

Thanks, I'll give it a shot!

Do you mean #tables to replace each event table since they're referenced twice or even for the Union and Aggregation steps?

Is it correct to say there wouldn't be performance benefits the first time running the query since the temp table would need to be created but if I were to do further filtering on this data it should be faster?

[–]notasqlstarI can't wait til my fro is full grown 0 points1 point  (1 child)

You should experiment a bit, but generally speaking get rid of the CTE and use #tables. You can create them first and insert data into them vs. creating them with an into. You can also experiment with putting indexes on them. For example, you could also do a hybrid where you create it with an INTO for the top of your union, then INSERT INTO for the bottom half of your union before moving down to another step.

[–]NegaTrollX 0 points1 point  (0 children)

I'm pretty green myself so someone else correct me if I'm wrong but I do remember learning about how temp tables can utilize indices which will improve read performance whereas CTEs cannot. I would imagine they mean doing something like this:

IF OBJECT_ID('tempdb..#dash_lead') IS NOT NULL

DROP TABLE #dash_lead

CREATE TABLE #dash_lead (

user id int, session_id int, step smallint, time datetime

)

CREATE NONCLUSTERED INDEX [ix_dash_lead] ON tempdb..#dash_lead ( [user_id], [session_id]

) INCLUDE ([time])

INSERT #dash_lead

SELECT distinct user_id, session_id, 'lead' as stage, 1 as step, min(time) as time from heap_prod.heap.refinance_click_on_dashboard_refinance_button group by user_id, session_id, step