Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. difference between with and temp table in my two query

difference between with and temp table in my two query

Scheduled Pinned Locked Moved Database
databaseperformancehelp
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mhd sbt
    wrote on last edited by
    #1

    what diffrence betwwen in two below table

    with cteStudent(ID,Name,Family)
    AS
    (
    select ID,Name,Family from tblStudent
    )
    select * from cteStudent

    SELECT ID,Name,Family into #tblTemp FROM tblStudent
    select * from #tblTemp

    assume that i remove temp table after run two query. which of this two query is better than another (in performance aspect) thanks for any help t

    L J F 3 Replies Last reply
    0
    • M mhd sbt

      what diffrence betwwen in two below table

      with cteStudent(ID,Name,Family)
      AS
      (
      select ID,Name,Family from tblStudent
      )
      select * from cteStudent

      SELECT ID,Name,Family into #tblTemp FROM tblStudent
      select * from #tblTemp

      assume that i remove temp table after run two query. which of this two query is better than another (in performance aspect) thanks for any help t

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      mehdi.sabet wrote:

      what diffrence betwwen in two below table

      The first will only work on Sql2005+, since that's when the Common Table Expression was introduced.

      mehdi.sabet wrote:

      assume that i remove temp table after run two query.
      which of this two query is better than another (in performance aspect)

      What makes you think there's much difference in terms of speed? :) It's not like there's "duplicate" functionality and programmers having to look for the "most efficient" version of a routine. Your hunting for speed in the wrong place. Now, to answer the question; the second version is preferred, it'd be unnoticeable faster. A good reason would be if you'd need to do multiple mutations on the same set. A good reason to use the CTE would be recursion. Good luck.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      1 Reply Last reply
      0
      • M mhd sbt

        what diffrence betwwen in two below table

        with cteStudent(ID,Name,Family)
        AS
        (
        select ID,Name,Family from tblStudent
        )
        select * from cteStudent

        SELECT ID,Name,Family into #tblTemp FROM tblStudent
        select * from #tblTemp

        assume that i remove temp table after run two query. which of this two query is better than another (in performance aspect) thanks for any help t

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        On SQLServer you can think of the CTE as an inline view, while the temporary table will be materialized and can be indexed.

        mehdi.sabet wrote:

        which of this two query is better than another (in performance aspect)

        In this specific case probably the CTE, because the select is straight forward and directly streamed to output. While the temp table needs to be materialized, if even just in memory, and selected from memory to the output. If the CTE/temptable would be used in more than one place and/or the select could benefit from indexing the temptable, the result could be quite different. On Oracle it's a bit different though. There a CTE can be materialized.

        Be excellent to each other. And... PARTY ON, DUDES! Abraham Lincoln

        1 Reply Last reply
        0
        • M mhd sbt

          what diffrence betwwen in two below table

          with cteStudent(ID,Name,Family)
          AS
          (
          select ID,Name,Family from tblStudent
          )
          select * from cteStudent

          SELECT ID,Name,Family into #tblTemp FROM tblStudent
          select * from #tblTemp

          assume that i remove temp table after run two query. which of this two query is better than another (in performance aspect) thanks for any help t

          F Offline
          F Offline
          Fabricio Dos Santos Antunes
          wrote on last edited by
          #4

          As Jörgen Andersson said, cte is faster cause there's no IO on disk, it's just memory. when you declare an #Temporary, you really create the table on your tempdb, that means that you have to write on disk. For a long range of information, you can say that is not that fast as use an cte. If i'm not wrong, declare an variable ( Declare @Table Table (column) ) is faster as use an CTE , and less complicate, cause just create the table on Memory. BUT, you got to remember, that this will only use memory, for a long range of information, this mean that will use a lot of ram.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups