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. Other Discussions
  3. The Weird and The Wonderful
  4. Logical thinking could get very complicated

Logical thinking could get very complicated

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasecssannouncement
10 Posts 6 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.
  • T Offline
    T Offline
    Tomz_KV
    wrote on last edited by
    #1

    Here is a simple stored procedure in company database that does a search and returns results in less than a second:

    CREATE PROCEDURE [dbo].[WorkOrder_Search](@Description varchar(250))
    AS
    Begin
    SELECT Order_id,Dept_Code,Graphic_Number,[Description],Quantity,Date_Received as [Recv. Date],Date_Due as [Due Date],
    Date_Delivered as [Delivered Date],proof_sent_date as [Proof Date],print_date as [To Print],
    dbo.func_grOrder_GetAssigned(Order_id) as [Assign To],Status,Note, Master_Cal_ID
    From grGraphic_Order
    where [Description] like '%' + @Description + '%'
    end

    But it used to be a really "BIG" stored procedure as shown below that did the same search and took about 5 minutes to return results. It did return the same results as the above does.

    CREATE PROCEDURE [dbo].[WorkOrder_search](@Description varchar(250))
    AS
    begin
    SET QUOTED_IDENTIFIER OFF
    SET ANSI_NULLS OFF
    SET NoCount ON

    Declare @sql varchar(8000)
    Declare @Orig\_1 varchar(1000)
    Declare @Declare varchar(200),
    @From varchar(100),@Into varchar(50),@Groupby varchar(300),
    @Orderby varchar(400),@Select varchar(400),@Select1 varchar(400),@Select2 varchar(300),
    @cursor1 varchar (500),@cursor2 varchar(4000),@create varchar(4000)
    
    set @Orig\_1 = ''
    
    Create table #Assignto\_temp(Order\_id int, Assignto\_names varchar(500))
    
    Insert into #Assignto\_temp(Order\_id )
    
    Select gro.order\_id
    From grassignto gro
    Inner join wfpmauser wf
    On gro.user\_id = wf.user\_id
    group by gro.order\_id
    
    declare @order\_number int, @Assignto varchar(255), @List\_names varchar(255),@order\_number\_prev int
    declare assignto Cursor
    Local
    Forward\_only
    for
    
    Select gro.order\_id,Wf.First\_name+' '+Wf.Last\_name as Assignto
    From grassignto gro
    Inner join wfpmauser wf
    On gro.user\_id = wf.user\_id
    order by order\_id 
    
    open assignto
    Fetch Next from assignto into @order\_number, @Assignto
    While @@Fetch\_Status=0
    begin
    		select @List\_names=Assignto\_names from #Assignto\_temp where order\_id=@order\_number
     	
    	if @List\_names is null 
    	begin
    		update #Assignto\_temp
    		set Assignto\_names=@Assignto
    		where order\_id=@order\_number
    	end
    	else
    	begin
    		update #Assignto\_temp
    		set Assignto\_names=@list\_names + ',' + @Assignto
    		where order\_id=@order\_number
    	end
    	  Fetch Next from assignto into @order\_number, @Assignto
    end
    close assignto
    deallocate assignto
    
    Set @Declare ="Declare @Orig\_1 varchar(1000),@Originator varchar(100),@order\_id int,@Orig varchar(300) set @Ori
    
    B E B Y 4 Replies Last reply
    0
    • T Tomz_KV

      Here is a simple stored procedure in company database that does a search and returns results in less than a second:

      CREATE PROCEDURE [dbo].[WorkOrder_Search](@Description varchar(250))
      AS
      Begin
      SELECT Order_id,Dept_Code,Graphic_Number,[Description],Quantity,Date_Received as [Recv. Date],Date_Due as [Due Date],
      Date_Delivered as [Delivered Date],proof_sent_date as [Proof Date],print_date as [To Print],
      dbo.func_grOrder_GetAssigned(Order_id) as [Assign To],Status,Note, Master_Cal_ID
      From grGraphic_Order
      where [Description] like '%' + @Description + '%'
      end

      But it used to be a really "BIG" stored procedure as shown below that did the same search and took about 5 minutes to return results. It did return the same results as the above does.

      CREATE PROCEDURE [dbo].[WorkOrder_search](@Description varchar(250))
      AS
      begin
      SET QUOTED_IDENTIFIER OFF
      SET ANSI_NULLS OFF
      SET NoCount ON

      Declare @sql varchar(8000)
      Declare @Orig\_1 varchar(1000)
      Declare @Declare varchar(200),
      @From varchar(100),@Into varchar(50),@Groupby varchar(300),
      @Orderby varchar(400),@Select varchar(400),@Select1 varchar(400),@Select2 varchar(300),
      @cursor1 varchar (500),@cursor2 varchar(4000),@create varchar(4000)
      
      set @Orig\_1 = ''
      
      Create table #Assignto\_temp(Order\_id int, Assignto\_names varchar(500))
      
      Insert into #Assignto\_temp(Order\_id )
      
      Select gro.order\_id
      From grassignto gro
      Inner join wfpmauser wf
      On gro.user\_id = wf.user\_id
      group by gro.order\_id
      
      declare @order\_number int, @Assignto varchar(255), @List\_names varchar(255),@order\_number\_prev int
      declare assignto Cursor
      Local
      Forward\_only
      for
      
      Select gro.order\_id,Wf.First\_name+' '+Wf.Last\_name as Assignto
      From grassignto gro
      Inner join wfpmauser wf
      On gro.user\_id = wf.user\_id
      order by order\_id 
      
      open assignto
      Fetch Next from assignto into @order\_number, @Assignto
      While @@Fetch\_Status=0
      begin
      		select @List\_names=Assignto\_names from #Assignto\_temp where order\_id=@order\_number
       	
      	if @List\_names is null 
      	begin
      		update #Assignto\_temp
      		set Assignto\_names=@Assignto
      		where order\_id=@order\_number
      	end
      	else
      	begin
      		update #Assignto\_temp
      		set Assignto\_names=@list\_names + ',' + @Assignto
      		where order\_id=@order\_number
      	end
      	  Fetch Next from assignto into @order\_number, @Assignto
      end
      close assignto
      deallocate assignto
      
      Set @Declare ="Declare @Orig\_1 varchar(1000),@Originator varchar(100),@order\_id int,@Orig varchar(300) set @Ori
      
      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      pssssst. Use pre blocks to make your code pretty.

      Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

      OriginalGriffO 1 Reply Last reply
      0
      • B Brisingr Aerowing

        pssssst. Use pre blocks to make your code pretty.

        Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Pretty? Pretty? You'd need a very large magnet and a flare gun to make that code pretty! :laugh:

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • T Tomz_KV

          Here is a simple stored procedure in company database that does a search and returns results in less than a second:

          CREATE PROCEDURE [dbo].[WorkOrder_Search](@Description varchar(250))
          AS
          Begin
          SELECT Order_id,Dept_Code,Graphic_Number,[Description],Quantity,Date_Received as [Recv. Date],Date_Due as [Due Date],
          Date_Delivered as [Delivered Date],proof_sent_date as [Proof Date],print_date as [To Print],
          dbo.func_grOrder_GetAssigned(Order_id) as [Assign To],Status,Note, Master_Cal_ID
          From grGraphic_Order
          where [Description] like '%' + @Description + '%'
          end

          But it used to be a really "BIG" stored procedure as shown below that did the same search and took about 5 minutes to return results. It did return the same results as the above does.

          CREATE PROCEDURE [dbo].[WorkOrder_search](@Description varchar(250))
          AS
          begin
          SET QUOTED_IDENTIFIER OFF
          SET ANSI_NULLS OFF
          SET NoCount ON

          Declare @sql varchar(8000)
          Declare @Orig\_1 varchar(1000)
          Declare @Declare varchar(200),
          @From varchar(100),@Into varchar(50),@Groupby varchar(300),
          @Orderby varchar(400),@Select varchar(400),@Select1 varchar(400),@Select2 varchar(300),
          @cursor1 varchar (500),@cursor2 varchar(4000),@create varchar(4000)
          
          set @Orig\_1 = ''
          
          Create table #Assignto\_temp(Order\_id int, Assignto\_names varchar(500))
          
          Insert into #Assignto\_temp(Order\_id )
          
          Select gro.order\_id
          From grassignto gro
          Inner join wfpmauser wf
          On gro.user\_id = wf.user\_id
          group by gro.order\_id
          
          declare @order\_number int, @Assignto varchar(255), @List\_names varchar(255),@order\_number\_prev int
          declare assignto Cursor
          Local
          Forward\_only
          for
          
          Select gro.order\_id,Wf.First\_name+' '+Wf.Last\_name as Assignto
          From grassignto gro
          Inner join wfpmauser wf
          On gro.user\_id = wf.user\_id
          order by order\_id 
          
          open assignto
          Fetch Next from assignto into @order\_number, @Assignto
          While @@Fetch\_Status=0
          begin
          		select @List\_names=Assignto\_names from #Assignto\_temp where order\_id=@order\_number
           	
          	if @List\_names is null 
          	begin
          		update #Assignto\_temp
          		set Assignto\_names=@Assignto
          		where order\_id=@order\_number
          	end
          	else
          	begin
          		update #Assignto\_temp
          		set Assignto\_names=@list\_names + ',' + @Assignto
          		where order\_id=@order\_number
          	end
          	  Fetch Next from assignto into @order\_number, @Assignto
          end
          close assignto
          deallocate assignto
          
          Set @Declare ="Declare @Orig\_1 varchar(1000),@Originator varchar(100),@order\_id int,@Orig varchar(300) set @Ori
          
          E Offline
          E Offline
          ENOTTY
          wrote on last edited by
          #4

          Obviously the old code is a hideous display of incompetence what with the gratitious use of cursors (not even FAST_FORWARD at that) and temp tables in dynamic SQL no less, but I think the new code is missing an ORDER BY Graphic_number ASC, Dept_code DESC (with matching nonclustered indexes on the table) and probably could be improved by an inline table valued function with a CROSS APPLY in place of dbo.func_grOrder_GetAssigned(Order_id) and a FULLTEXT index for the Description column to avoid the non-SARGable wildcard prefix.

          T 1 Reply Last reply
          0
          • T Tomz_KV

            Here is a simple stored procedure in company database that does a search and returns results in less than a second:

            CREATE PROCEDURE [dbo].[WorkOrder_Search](@Description varchar(250))
            AS
            Begin
            SELECT Order_id,Dept_Code,Graphic_Number,[Description],Quantity,Date_Received as [Recv. Date],Date_Due as [Due Date],
            Date_Delivered as [Delivered Date],proof_sent_date as [Proof Date],print_date as [To Print],
            dbo.func_grOrder_GetAssigned(Order_id) as [Assign To],Status,Note, Master_Cal_ID
            From grGraphic_Order
            where [Description] like '%' + @Description + '%'
            end

            But it used to be a really "BIG" stored procedure as shown below that did the same search and took about 5 minutes to return results. It did return the same results as the above does.

            CREATE PROCEDURE [dbo].[WorkOrder_search](@Description varchar(250))
            AS
            begin
            SET QUOTED_IDENTIFIER OFF
            SET ANSI_NULLS OFF
            SET NoCount ON

            Declare @sql varchar(8000)
            Declare @Orig\_1 varchar(1000)
            Declare @Declare varchar(200),
            @From varchar(100),@Into varchar(50),@Groupby varchar(300),
            @Orderby varchar(400),@Select varchar(400),@Select1 varchar(400),@Select2 varchar(300),
            @cursor1 varchar (500),@cursor2 varchar(4000),@create varchar(4000)
            
            set @Orig\_1 = ''
            
            Create table #Assignto\_temp(Order\_id int, Assignto\_names varchar(500))
            
            Insert into #Assignto\_temp(Order\_id )
            
            Select gro.order\_id
            From grassignto gro
            Inner join wfpmauser wf
            On gro.user\_id = wf.user\_id
            group by gro.order\_id
            
            declare @order\_number int, @Assignto varchar(255), @List\_names varchar(255),@order\_number\_prev int
            declare assignto Cursor
            Local
            Forward\_only
            for
            
            Select gro.order\_id,Wf.First\_name+' '+Wf.Last\_name as Assignto
            From grassignto gro
            Inner join wfpmauser wf
            On gro.user\_id = wf.user\_id
            order by order\_id 
            
            open assignto
            Fetch Next from assignto into @order\_number, @Assignto
            While @@Fetch\_Status=0
            begin
            		select @List\_names=Assignto\_names from #Assignto\_temp where order\_id=@order\_number
             	
            	if @List\_names is null 
            	begin
            		update #Assignto\_temp
            		set Assignto\_names=@Assignto
            		where order\_id=@order\_number
            	end
            	else
            	begin
            		update #Assignto\_temp
            		set Assignto\_names=@list\_names + ',' + @Assignto
            		where order\_id=@order\_number
            	end
            	  Fetch Next from assignto into @order\_number, @Assignto
            end
            close assignto
            deallocate assignto
            
            Set @Declare ="Declare @Orig\_1 varchar(1000),@Originator varchar(100),@order\_id int,@Orig varchar(300) set @Ori
            
            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            And now? When your boss tells you that the application is slow, where will you find some lines for easy improvements?

            T 1 Reply Last reply
            0
            • E ENOTTY

              Obviously the old code is a hideous display of incompetence what with the gratitious use of cursors (not even FAST_FORWARD at that) and temp tables in dynamic SQL no less, but I think the new code is missing an ORDER BY Graphic_number ASC, Dept_code DESC (with matching nonclustered indexes on the table) and probably could be improved by an inline table valued function with a CROSS APPLY in place of dbo.func_grOrder_GetAssigned(Order_id) and a FULLTEXT index for the Description column to avoid the non-SARGable wildcard prefix.

              T Offline
              T Offline
              Tomz_KV
              wrote on last edited by
              #6

              Great advice.

              TOMZ_KV

              1 Reply Last reply
              0
              • B Bernhard Hiller

                And now? When your boss tells you that the application is slow, where will you find some lines for easy improvements?

                T Offline
                T Offline
                Tomz_KV
                wrote on last edited by
                #7

                What would you suggest to do?

                TOMZ_KV

                E 1 Reply Last reply
                0
                • T Tomz_KV

                  What would you suggest to do?

                  TOMZ_KV

                  E Offline
                  E Offline
                  ENOTTY
                  wrote on last edited by
                  #8

                  Sprinkle WAITFOR DELAY '00:00:01' (or a more appropriate value, do a performance analysis first; it should run faster than the old unoptimized version) judiciously across your newly optimized SPs, so you still have room for improvement.

                  1 Reply Last reply
                  0
                  • T Tomz_KV

                    Here is a simple stored procedure in company database that does a search and returns results in less than a second:

                    CREATE PROCEDURE [dbo].[WorkOrder_Search](@Description varchar(250))
                    AS
                    Begin
                    SELECT Order_id,Dept_Code,Graphic_Number,[Description],Quantity,Date_Received as [Recv. Date],Date_Due as [Due Date],
                    Date_Delivered as [Delivered Date],proof_sent_date as [Proof Date],print_date as [To Print],
                    dbo.func_grOrder_GetAssigned(Order_id) as [Assign To],Status,Note, Master_Cal_ID
                    From grGraphic_Order
                    where [Description] like '%' + @Description + '%'
                    end

                    But it used to be a really "BIG" stored procedure as shown below that did the same search and took about 5 minutes to return results. It did return the same results as the above does.

                    CREATE PROCEDURE [dbo].[WorkOrder_search](@Description varchar(250))
                    AS
                    begin
                    SET QUOTED_IDENTIFIER OFF
                    SET ANSI_NULLS OFF
                    SET NoCount ON

                    Declare @sql varchar(8000)
                    Declare @Orig\_1 varchar(1000)
                    Declare @Declare varchar(200),
                    @From varchar(100),@Into varchar(50),@Groupby varchar(300),
                    @Orderby varchar(400),@Select varchar(400),@Select1 varchar(400),@Select2 varchar(300),
                    @cursor1 varchar (500),@cursor2 varchar(4000),@create varchar(4000)
                    
                    set @Orig\_1 = ''
                    
                    Create table #Assignto\_temp(Order\_id int, Assignto\_names varchar(500))
                    
                    Insert into #Assignto\_temp(Order\_id )
                    
                    Select gro.order\_id
                    From grassignto gro
                    Inner join wfpmauser wf
                    On gro.user\_id = wf.user\_id
                    group by gro.order\_id
                    
                    declare @order\_number int, @Assignto varchar(255), @List\_names varchar(255),@order\_number\_prev int
                    declare assignto Cursor
                    Local
                    Forward\_only
                    for
                    
                    Select gro.order\_id,Wf.First\_name+' '+Wf.Last\_name as Assignto
                    From grassignto gro
                    Inner join wfpmauser wf
                    On gro.user\_id = wf.user\_id
                    order by order\_id 
                    
                    open assignto
                    Fetch Next from assignto into @order\_number, @Assignto
                    While @@Fetch\_Status=0
                    begin
                    		select @List\_names=Assignto\_names from #Assignto\_temp where order\_id=@order\_number
                     	
                    	if @List\_names is null 
                    	begin
                    		update #Assignto\_temp
                    		set Assignto\_names=@Assignto
                    		where order\_id=@order\_number
                    	end
                    	else
                    	begin
                    		update #Assignto\_temp
                    		set Assignto\_names=@list\_names + ',' + @Assignto
                    		where order\_id=@order\_number
                    	end
                    	  Fetch Next from assignto into @order\_number, @Assignto
                    end
                    close assignto
                    deallocate assignto
                    
                    Set @Declare ="Declare @Orig\_1 varchar(1000),@Originator varchar(100),@order\_id int,@Orig varchar(300) set @Ori
                    
                    Y Offline
                    Y Offline
                    YDaoust
                    wrote on last edited by
                    #9

                    How can you be sure that both procedures are doing the same? I mean in all cases, not just on a few test cases.

                    T 1 Reply Last reply
                    0
                    • Y YDaoust

                      How can you be sure that both procedures are doing the same? I mean in all cases, not just on a few test cases.

                      T Offline
                      T Offline
                      Tomz_KV
                      wrote on last edited by
                      #10

                      The old one had been in production for a long while. Users knew that it returned correct results but just took too much time to run. They got timeout messages from time to time. Instead of fixing it, it was easier and quicker to write a new one.

                      TOMZ_KV

                      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