How to get rid of #temp tables from the query

前端 未结 1 497
自闭症患者
自闭症患者 2020-12-21 20:29

I have three queries that put data in their respective #temp tables. Later, In one query those #temp tables are used again to get the data.

i.e

sele         


        
相关标签:
1条回答
  • 2020-12-21 20:47

    if you are using MSSql, use CTE

    ;with cte1 as (
        select {some columns} from {some tables} where {conditions1}
    ), 
    cte2 as (
        select {some other columns} from {some tables} where {conditions2}
    ),
    cte3 as (
        select {some other columns} from {some tables} where {conditions3}
    )
    select {some columns from all ctes} from cte1, cte2, cte3 where {conditions}
    

    this should run faster as there is no need to insert data into temp table.

    another advantage of avoiding temp table is, using temp table sometimes has really bad impact on performances, as there is only one tempdb for entire sql server and heavily use tempdb, may block other queries. just google temp table and performance impacts, you will find a lots of article on this topic

    0 讨论(0)
提交回复
热议问题