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. General Programming
  3. C#
  4. Second Time Slow Code

Second Time Slow Code

Scheduled Pinned Locked Moved C#
csharpdatabasesql-serversysadmin
4 Posts 3 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.
  • O Offline
    O Offline
    obelisk29
    wrote on last edited by
    #1

    Hello I am using Sql Server 2000 Developer Edition, Now I know there are limitations too how many connections you have BUT This piece of code below executes a whole lot slower the second time you run it.

    switch(cbSearch.SelectedItem.ToString())
    {
    	case "Lastname":
    		cmd = new SqlCommand("SPSearchRmiInfoLastname", conn);
    		cmd.CommandType = CommandType.StoredProcedure;
    	        cmd.Parameters.Add("@Lastname", txtSearch.Text);
    	        break;
    	case "Firstname":
    	        cmd = new SqlCommand("SPSearchRmiInfoFirstname", conn);
                    cmd.CommandType = CommandType.StoredProcedure;	
                    cmd.Parameters.Add("@Firstname", txtSearch.Text);
    	        break;
    }
    conn.Open();
    Search.Clear();
    
    SqlDataReader drd = cmd.ExecuteReader();
    while(drd.Read())
    {
    	Search.Rows.Add(new object\[\] {drd\[0\], drd\[1\], drd\[2\], drd\[3\]});
    	sbp.Text = Search.Rows.Count+" records found.";
    }
    dg.DataSource = Search;
    
    drd.Close();
    conn.Close();
    

    The results are staggering. First time: 1783 Milliseconds Second time: 150326 Milliseconds HOLY SHAZBOT!! Now that's a difference right there, Any ideas on why this is happening and how I can change it Thanks, Obe NOTE: I already posted this in the Sql/ADO/ADO.NET Section but it has todo with C# as well ------------------ I'm naked under my clothes...

    L C 2 Replies Last reply
    0
    • O obelisk29

      Hello I am using Sql Server 2000 Developer Edition, Now I know there are limitations too how many connections you have BUT This piece of code below executes a whole lot slower the second time you run it.

      switch(cbSearch.SelectedItem.ToString())
      {
      	case "Lastname":
      		cmd = new SqlCommand("SPSearchRmiInfoLastname", conn);
      		cmd.CommandType = CommandType.StoredProcedure;
      	        cmd.Parameters.Add("@Lastname", txtSearch.Text);
      	        break;
      	case "Firstname":
      	        cmd = new SqlCommand("SPSearchRmiInfoFirstname", conn);
                      cmd.CommandType = CommandType.StoredProcedure;	
                      cmd.Parameters.Add("@Firstname", txtSearch.Text);
      	        break;
      }
      conn.Open();
      Search.Clear();
      
      SqlDataReader drd = cmd.ExecuteReader();
      while(drd.Read())
      {
      	Search.Rows.Add(new object\[\] {drd\[0\], drd\[1\], drd\[2\], drd\[3\]});
      	sbp.Text = Search.Rows.Count+" records found.";
      }
      dg.DataSource = Search;
      
      drd.Close();
      conn.Close();
      

      The results are staggering. First time: 1783 Milliseconds Second time: 150326 Milliseconds HOLY SHAZBOT!! Now that's a difference right there, Any ideas on why this is happening and how I can change it Thanks, Obe NOTE: I already posted this in the Sql/ADO/ADO.NET Section but it has todo with C# as well ------------------ I'm naked under my clothes...

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      I can see that you have 2 different stored procedures. Is it always the same stored proc that is called the second time? Also, as a piece of advice (not related to your problem), don't start you stored procedures name with "sp", because the server search for it in the "master" database when they begin with "sp". -------- "I say no to drugs, but they don't listen." - Marilyn Manson

      O 1 Reply Last reply
      0
      • L Le centriste

        I can see that you have 2 different stored procedures. Is it always the same stored proc that is called the second time? Also, as a piece of advice (not related to your problem), don't start you stored procedures name with "sp", because the server search for it in the "master" database when they begin with "sp". -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        O Offline
        O Offline
        obelisk29
        wrote on last edited by
        #3

        Thanks for the advice in the naming schema... As for the SP, when I was timing the differences I used the same SP executed one right after the other ------------------ I'm naked under my clothes...

        1 Reply Last reply
        0
        • O obelisk29

          Hello I am using Sql Server 2000 Developer Edition, Now I know there are limitations too how many connections you have BUT This piece of code below executes a whole lot slower the second time you run it.

          switch(cbSearch.SelectedItem.ToString())
          {
          	case "Lastname":
          		cmd = new SqlCommand("SPSearchRmiInfoLastname", conn);
          		cmd.CommandType = CommandType.StoredProcedure;
          	        cmd.Parameters.Add("@Lastname", txtSearch.Text);
          	        break;
          	case "Firstname":
          	        cmd = new SqlCommand("SPSearchRmiInfoFirstname", conn);
                          cmd.CommandType = CommandType.StoredProcedure;	
                          cmd.Parameters.Add("@Firstname", txtSearch.Text);
          	        break;
          }
          conn.Open();
          Search.Clear();
          
          SqlDataReader drd = cmd.ExecuteReader();
          while(drd.Read())
          {
          	Search.Rows.Add(new object\[\] {drd\[0\], drd\[1\], drd\[2\], drd\[3\]});
          	sbp.Text = Search.Rows.Count+" records found.";
          }
          dg.DataSource = Search;
          
          drd.Close();
          conn.Close();
          

          The results are staggering. First time: 1783 Milliseconds Second time: 150326 Milliseconds HOLY SHAZBOT!! Now that's a difference right there, Any ideas on why this is happening and how I can change it Thanks, Obe NOTE: I already posted this in the Sql/ADO/ADO.NET Section but it has todo with C# as well ------------------ I'm naked under my clothes...

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Another quick performance tip (although it doesn't explain why it is slower the second time around) Change your while loop to:

          while(drd.Read())
          {
          Search.Rows.Add(new object[] {drd[0], drd[1], drd[2], drd[3]});
          }
          sbp.Text = string.Concat(Search.Rows.Count.ToString()," records found.")

          Now you only generate your text once, which should speed things up. Also, I changes the stringA + stringB notation in to a string.Concat() method call which is a wee bit faster. --Colin Mackay--

          EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

          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