2012-06-05 8 views
0

아래 웹 사이트의 캘린더 코드를 보면 문제는이 코드가로드되는 데 약 8-10 초가 걸리는 것입니다. 당신 중 누구라도로드 시간을 최소화하는 방법을 볼 수 있다면 나는 감사 할 것입니다.캘린더의 로딩 시간 단축

public static IEnumerable<DateTime> AllDatesInMonth(int year, int month) 
    { 
     foreach (var day in Enumerable.Range(1, DateTime.DaysInMonth(year, month))) 
     { 
      yield return new DateTime(year, month, day); 
     } 
    } 

    public void ForeachDayInMonth(int year, int month, SqlConnection connection) 
    { 
     int day; 
     int count; 
     double divHeight; 

     lbl_Month.Text += "<table class=\"Month\">"; 
     foreach (DateTime date in AllDatesInMonth(year, month)) 
     { 
      day = int.Parse(date.ToString().Substring(0, 2)); 
      count = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection).Count; // Creates a sql select statement 

      lbl_Month.Text += "<tr>" + 
       "<td style=\"height: 30px; width: 70px;"; 

      if (date.Date == DateTime.Today) 
      { 
       lbl_Month.Text += "-webkit-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" + 
        "-moz-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" + 
        "box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);"; 
      } 

      lbl_Month.Text += "\">"; 

      if (count != 0) 
      { 
       divHeight = 100/count; 

       foreach (Begivenheder b in Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection)) // creates a sql select statement 
       { 
        lbl_Month.Text += "<div style=\"background-color: " + b.begivenhed.type.TypeFarve + "; height: " + divHeight + "%;\">" + 
         "<a href=\"KalenderEvent.aspx?Event=" + b.begivenhed.ID + "\">"; 
        if (b.begivenhed.Navn.Length > 9) 
        { 
         lbl_Month.Text += b.begivenhed.Navn.Remove(9) + "..."; 
        } 
        else 
        { 
         lbl_Month.Text += b.begivenhed.Navn; 
        } 
        lbl_Month.Text += "</a>" + 
         "</div>"; 
       } 
      } 

      lbl_Month.Text += "</td>" + 
       "</tr>"; 
     } 
     lbl_Month.Text += "</table>"; 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string year; 

     if (Request.QueryString["Year"] == null) 
     { 
      year = DateTime.Today.Year.ToString(); 
     } 
     else 
     { 
      year = Request.QueryString["Year"]; 
     } 

     Page.Title += " - Kalender " + year; 

     SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["Database"]); 
     connection.Open(); 
     try 
     { 
      Year(int.Parse(year), connection); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 

    private void Year(int year, SqlConnection connection) 
    { 
     string thisYear = year.ToString(); 
     int lastYear = year - 1; 
     int nextYear = year + 1; 

     lbl_Year.Text = "<div>" + 
      "<a href=\"Kalender.aspx?Year=" + lastYear.ToString() + "\" id=\"LastYear\"></a>" + 
      "</div>" + 
      "<div>" + 
      thisYear + 
      "</div>" + 
      "<div>" + 
      "<a href=\"Kalender.aspx?Year=" + nextYear.ToString() + "\" id=\"NextYear\"></a>" + 
      "</div>"; 

     lbl_Month.Text = "<table>" + 
      "<tr>" + 
      "<td>"; 
     // Dage 
     lbl_Month.Text += "<p>" + 
      "</p>" + 
      "<br />" + 
      "<table>"; 
     for (int i = 1; i <= 31; i++) 
     { 
      lbl_Month.Text += "<tr>" + 
       "<td style=\"height: 30px;\">" + 
       "<p>" + 
       i.ToString() + 
       "</p>" + 
       "</td>" + 
       "</tr>"; 
     } 
     lbl_Month.Text += "</table>" + 
      "</td>"; 

     //Januar (repeats 12 times, ones for each month) 
     lbl_Month.Text += "<td>" + 
      "<p>" + 
      "Januar" + 
      "</p>"; 
     ForeachDayInMonth(int.Parse(thisYear), 1, connection); 
     lbl_Month.Text += "</td>"; 

     lbl_Month.Text += "</tr>" + 
      "</table>"; 
    } 

"Begivenheder.Get_Begivenhed_By_Date"코드가 있습니다.

public static List<Begivenheder> Get_Begivenhed_By_Date(int år, int måned, int dag, SqlConnection connection) 
    { 
     List<Begivenheder> result = new List<Begivenheder>(); 

     using (var command = new SqlCommand("Select ID, FK_Begivenhed_ID from Begivenhed_Datoer where Dag=" + dag + " and Måned=" + måned + " and År=" + år, connection)) 
     { 
      SqlDataReader reader = command.ExecuteReader(); 
      try 
      { 
       while (reader.Read()) 
       { 
        Begivenheder b = new Begivenheder(); 
        b.Dato_ID = reader.GetInt32(0); 
        b.ID = reader.GetInt32(1); 
        b.Dato_Day = dag; 
        b.Dato_Month = måned; 
        b.Dato_Year = år; 
        result.Add(b); 
       } 
      } 
      finally 
      { 
       reader.Close(); 
      } 

      foreach (Begivenheder b in result) 
      { 
       b.begivenhed = Begivenheder.Get_Begivenhed_By_ID(b.ID, connection); 
      } 
     } 

     return result; 
    } 

나는 코드가 많다는 것을 알고 있지만 어떻게 줄일 수 있는지 알지 못합니다.

+0

질문의 반대라고 말했기 때문에 나는 방금 제목을 편집했습니다. – BlackBear

+0

감사합니다 ^^ –

답변

2

으로 string + string + string 코드를 대체 - 오버 헤드를 많이 시도하고 만 얘기해야 있도록 개편은 그게 전부 데이터베이스를 한 번. 그 외에도 .NET 프로파일 러를 사용하고 대부분의 시간을 어디에 사용하는지 확인하십시오.

1

기본 문자열 최적화가 큰 차이를 만듭니다. 대신 string += 문자열 작성기를 사용하십시오. 그런 다음 모든 계산이 완료되면 StringBuilder를 .ToString() 메서드를 사용하여 문자열로 변환합니다.

참조 MSDN StringBuilder Class

또한 모든를 Page_Load가 CCA (30) 데이터베이스 쿼리에 이르게 AppendFormat 방법

+2

데이터베이스 적중과 10 초당 페이지로드를 줄이려는 목표와 관련하여 이것은 아마도 무시할 만하다. –

2

각 날짜마다 하나의 쿼리를 수행하는 대신 전체 간격에 대한 모든 이벤트를 반입하여 성능을 향상시킬 수 있습니다. 쉬운 변화 인 현재 데이터 모델을 고려할 때 한 달 안에 모든 이벤트를 원한다고 생각됩니다.

당신이 하루에 한 쿼리를 수행 그것에 SqlParameters을 추가, 하나가 아닌 28 ~ 31 배의 새로운 브랜드를 다시 작성하여 동일한 SqlCommand를 다시 선택 할 경우

.

1

1) 사용 :

day = date.Day; 

대신은 :

day = int.Parse(date.ToString().Substring(0, 2)); 

2) 스타일 시트/CSS로이 긴 스타일을 넣고 짧은 클래스 대신 = '이름'사용

webkit-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" + 
        "-moz-box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8);" + 
        "box-shadow:inset 0px 0px 20px rgba(000,000,000,0.8); 

3) 매일 Begivenhed_By_Date를 두 번 호출합니다.

1)  count = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection).Count; 
2) foreach (Begivenheder b in Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection)) 
could be: 
+) List<Begivenheder> hedByDate = Begivenheder.Get_Begivenhed_By_Date(year, month, day, connection); 
1.x count = hedByDate.Count; 
2.x foreach (Begivenheder b in hedByDate, connection)) 

4) 최소한 StringBuilder를 사용하여 lbl_Month.Text + = 문자열을 먼저 연결 한 다음 마지막으로 할당하십시오.

5) Repeater 컨트롤을 사용하므로 코드를 HTML과 같이 낭비하지 않아도됩니다.

6) SQL 쿼리를 여러 번이 아닌 한 번만 수행하도록 변경하십시오.