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. compare performance between two query

compare performance between two query

Scheduled Pinned Locked Moved Database
databaseperformancequestion
8 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

    Which one of two below query is excellence of another? and why? thanks for your idea query 1:

    if @aa = 1
    select @b = left(@a,10)
    else
    select @b = left(@c,10)

    query 2:

    SELECT @b = CASE WHEN @aa = 1 THEN LEFT(@a,10)
    WHEN @aa = 2 THEN LEFT(@c,10)
    END

    T P L 3 Replies Last reply
    0
    • M mhd sbt

      Which one of two below query is excellence of another? and why? thanks for your idea query 1:

      if @aa = 1
      select @b = left(@a,10)
      else
      select @b = left(@c,10)

      query 2:

      SELECT @b = CASE WHEN @aa = 1 THEN LEFT(@a,10)
      WHEN @aa = 2 THEN LEFT(@c,10)
      END

      T Offline
      T Offline
      Tim Carmichael
      wrote on last edited by
      #2

      The first query only checks for @aa =1. If @aa is not equal to 1, the second select is executed. The second query checks for @aa = 1 or @aa = 2; if @aa is neither, then no select is executed.

      1 Reply Last reply
      0
      • M mhd sbt

        Which one of two below query is excellence of another? and why? thanks for your idea query 1:

        if @aa = 1
        select @b = left(@a,10)
        else
        select @b = left(@c,10)

        query 2:

        SELECT @b = CASE WHEN @aa = 1 THEN LEFT(@a,10)
        WHEN @aa = 2 THEN LEFT(@c,10)
        END

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        The first thing isn't a query. And they are unequal, as Tim mentioned. And have you considered: SELECT @b = LEFT(CASE @aa WHEN 1 THEN @a ELSE @c END ,10) I expect they are all very similar in performance, but I would stick with pure SQL.

        1 Reply Last reply
        0
        • M mhd sbt

          Which one of two below query is excellence of another? and why? thanks for your idea query 1:

          if @aa = 1
          select @b = left(@a,10)
          else
          select @b = left(@c,10)

          query 2:

          SELECT @b = CASE WHEN @aa = 1 THEN LEFT(@a,10)
          WHEN @aa = 2 THEN LEFT(@c,10)
          END

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

          Hunting for micro-optimizations usually indicates that one is in need of a profiler, to discover where the real bottleneck is.

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

          M 1 Reply Last reply
          0
          • L Lost User

            Hunting for micro-optimizations usually indicates that one is in need of a profiler, to discover where the real bottleneck is.

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

            M Offline
            M Offline
            mhd sbt
            wrote on last edited by
            #5

            ok thank you for reply. but how can i do this ,i mean use of sql profiler for compare performance between two query? thank you

            L 1 Reply Last reply
            0
            • M mhd sbt

              ok thank you for reply. but how can i do this ,i mean use of sql profiler for compare performance between two query? thank you

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

              mehdi.sabet wrote:

              but how can i do this ,i mean use of sql profiler for compare performance between two query?

              It's usually not very helpful to try and find "small" differences like those; there's also some intelligence in the server, and something called an optimizer. If your queries are slow, find out which ones are the mayor culprits. You can simply time them, by whichever means you want. Then, optimize those. Also periodically check your server for the usual stuff that a DBA would check; are your indexes still good? Does the Wizard from the management studio agree with that? (I believe there's an index-wizard in Sql2008+) Do the procedures fetch unnecessary data (think "select *" when "select cola, bolb" would do), do any of the tables need partitioning? How about caching stuff?

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

              M 1 Reply Last reply
              0
              • L Lost User

                mehdi.sabet wrote:

                but how can i do this ,i mean use of sql profiler for compare performance between two query?

                It's usually not very helpful to try and find "small" differences like those; there's also some intelligence in the server, and something called an optimizer. If your queries are slow, find out which ones are the mayor culprits. You can simply time them, by whichever means you want. Then, optimize those. Also periodically check your server for the usual stuff that a DBA would check; are your indexes still good? Does the Wizard from the management studio agree with that? (I believe there's an index-wizard in Sql2008+) Do the procedures fetch unnecessary data (think "select *" when "select cola, bolb" would do), do any of the tables need partitioning? How about caching stuff?

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

                M Offline
                M Offline
                mhd sbt
                wrote on last edited by
                #7

                ok thanks for your good advice.

                L 1 Reply Last reply
                0
                • M mhd sbt

                  ok thanks for your good advice.

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

                  You're welcome :)

                  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