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. How to optimize the SQL command ...

How to optimize the SQL command ...

Scheduled Pinned Locked Moved Database
databasetutorialannouncementcode-reviewcareer
11 Posts 5 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.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    IF EXIST(SELECT 1 FROM t where name='abc' ADN age=20 AND job='worker')
    UPDATE t set num=num+1 WHERE name='abc' ADN age=20 AND job='worker'
    ELSE INSERT INTO t(id,name,age,job,num) VALUES(NEWID(),'abc',20,'worker',1)

    P A 2 Replies Last reply
    0
    • F followait

      IF EXIST(SELECT 1 FROM t where name='abc' ADN age=20 AND job='worker')
      UPDATE t set num=num+1 WHERE name='abc' ADN age=20 AND job='worker'
      ELSE INSERT INTO t(id,name,age,job,num) VALUES(NEWID(),'abc',20,'worker',1)

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      What is your question?

      "I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon

      F 1 Reply Last reply
      0
      • P Paul Conrad

        What is your question?

        "I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon

        F Offline
        F Offline
        followait
        wrote on last edited by
        #3

        See the title.

        P 1 Reply Last reply
        0
        • F followait

          See the title.

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          Not sure where you can optimize it. Looks pretty tight. Why do you think it needs optimizing? Is it taking a long time to run?

          "I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon

          F 1 Reply Last reply
          0
          • P Paul Conrad

            Not sure where you can optimize it. Looks pretty tight. Why do you think it needs optimizing? Is it taking a long time to run?

            "I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon

            F Offline
            F Offline
            followait
            wrote on last edited by
            #5

            Yes. Two conditions duplicates.

            P 1 Reply Last reply
            0
            • F followait

              Yes. Two conditions duplicates.

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              I don't think you can improve this. You can't escape duplicating the WHERE clause in the EXISTS and UPDATE queries. It's not really duplication anyway - the UPDATE query is only fired under certain conditions.

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              1 Reply Last reply
              0
              • F followait

                IF EXIST(SELECT 1 FROM t where name='abc' ADN age=20 AND job='worker')
                UPDATE t set num=num+1 WHERE name='abc' ADN age=20 AND job='worker'
                ELSE INSERT INTO t(id,name,age,job,num) VALUES(NEWID(),'abc',20,'worker',1)

                A Offline
                A Offline
                andyharman
                wrote on last edited by
                #7

                The following will remove the need for the "if exist" statement:

                UPDATE t SET num=num+1
                  WHERE name='abc'
                  AND age=20
                  AND job='worker'
                IF (@@ROWCOUNT = 0) BEGIN
                  INSERT INTO t(id,name,age,job,num)
                     VALUES(NEWID(),'abc',20,'worker',1)
                END
                

                Additionally you should look at your indexes. I would expect something like:

                CREATE UNIQUE INDEX t_idx1 ON t (name, age, job)
                

                I would recommend looking at the other SQL that use this table, then change the order of the columns in the above index to best-suit their requirements. Regards Andy

                F E 2 Replies Last reply
                0
                • A andyharman

                  The following will remove the need for the "if exist" statement:

                  UPDATE t SET num=num+1
                    WHERE name='abc'
                    AND age=20
                    AND job='worker'
                  IF (@@ROWCOUNT = 0) BEGIN
                    INSERT INTO t(id,name,age,job,num)
                       VALUES(NEWID(),'abc',20,'worker',1)
                  END
                  

                  Additionally you should look at your indexes. I would expect something like:

                  CREATE UNIQUE INDEX t_idx1 ON t (name, age, job)
                  

                  I would recommend looking at the other SQL that use this table, then change the order of the columns in the above index to best-suit their requirements. Regards Andy

                  F Offline
                  F Offline
                  followait
                  wrote on last edited by
                  #8

                  I'm using ADO. Any way? Thanks.

                  A 1 Reply Last reply
                  0
                  • F followait

                    I'm using ADO. Any way? Thanks.

                    A Offline
                    A Offline
                    andyharman
                    wrote on last edited by
                    #9

                    It would slot-in in the same way as your original code sample. However a better way would be to wrap it in a stored procedure:

                    CREATE PROCEDURE dbo.Insert_t
                    @Name VARCHAR(40),
                    @Age SMALLINT,
                    @Job VARCHAR(40)
                    AS BEGIN
                      SET NOCOUNT ON
                      UPDATE t SET num = num+1
                        WHERE name = @Name
                        AND age = @Age
                        AND job = @Job
                      IF (@@ROWCOUNT = 0) BEGIN
                        INSERT INTO t (id, name, age, job, num)
                          VALUES (NEWID(), @Name, @Age, @Job, 1)
                      END
                      RETURN 0
                    END
                    

                    You can then create an ADO command object, with parameters, to execute it from your code. Note that you will need to add some error-trapping code. I'll let you figure that all out. Regards Andy

                    F 1 Reply Last reply
                    0
                    • A andyharman

                      It would slot-in in the same way as your original code sample. However a better way would be to wrap it in a stored procedure:

                      CREATE PROCEDURE dbo.Insert_t
                      @Name VARCHAR(40),
                      @Age SMALLINT,
                      @Job VARCHAR(40)
                      AS BEGIN
                        SET NOCOUNT ON
                        UPDATE t SET num = num+1
                          WHERE name = @Name
                          AND age = @Age
                          AND job = @Job
                        IF (@@ROWCOUNT = 0) BEGIN
                          INSERT INTO t (id, name, age, job, num)
                            VALUES (NEWID(), @Name, @Age, @Job, 1)
                        END
                        RETURN 0
                      END
                      

                      You can then create an ADO command object, with parameters, to execute it from your code. Note that you will need to add some error-trapping code. I'll let you figure that all out. Regards Andy

                      F Offline
                      F Offline
                      followait
                      wrote on last edited by
                      #10

                      Thanks a lot. :)

                      1 Reply Last reply
                      0
                      • A andyharman

                        The following will remove the need for the "if exist" statement:

                        UPDATE t SET num=num+1
                          WHERE name='abc'
                          AND age=20
                          AND job='worker'
                        IF (@@ROWCOUNT = 0) BEGIN
                          INSERT INTO t(id,name,age,job,num)
                             VALUES(NEWID(),'abc',20,'worker',1)
                        END
                        

                        Additionally you should look at your indexes. I would expect something like:

                        CREATE UNIQUE INDEX t_idx1 ON t (name, age, job)
                        

                        I would recommend looking at the other SQL that use this table, then change the order of the columns in the above index to best-suit their requirements. Regards Andy

                        E Offline
                        E Offline
                        Ennis Ray Lynch Jr
                        wrote on last edited by
                        #11

                        Interesting approach but I wonder if the failed update statement executes faster than the EXISTS statement? Either way, I don't like the approach beyond the novelty. In cases where I really need "extra" performance I separate the stored procedures and let the application decide which one to call, taking advantage of distributed logic.

                        Need a C# Consultant? I'm available.
                        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                        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