简体   繁体   中英

which one is a better solution?

Solution 1:

Dim i As Integer = CInt(_table.Rows(0).Item(3))
Do While i - 2 > 0
                _tableBackLogs.Merge(Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'"))
                i = i - 2
            Loop

Solutin 2:

 If i = 1 Then
                Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i & "'")
            ElseIf i = 2 Then
                Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'")
            ElseIf i = 3 Then
                Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade FROM SubjectPI WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & "' AND Status='Fail' AND Semester='" & i - 2 & "'")
                'On and on....upto i=8
            End If

Which one is a better solution in terms of performance and speed of execution??

UPDATE: I am storing the data into a DATATABLE....and then using it with a ListView.

Is there some reason not to use "IN" to grab all these semester numbers in one query, as in something like this?

Global.DataAccess.GetDataTable("SELECT SubjectID,SubjectName,Grade 
    FROM SubjectPI 
    WHERE RegNo='" & CInt(HttpContext.Current.Session("userName")) & 
           "' AND Status='Fail' 
              AND Semester IN ('8', '6', '4', '2'))");

Because if you can use "IN", do. "Don't Repeat Yourself," yeah sure, ya betcha.

One principle of writing software is: Dont Repeat Yourself (DRY). So solution 1 is definitely better. You will run into code maintenance problems otherwise. I doubt you will get any measurable speed difference between the two solutions.

Which one is a better solution in terms of performance and speed of execution??

Well, putting aside the concern about string concatenation, the best approach will be to get all the data needed in the minimum number of calls to the database.

You should change your code to identify which semesters are needed and then put everything together in an unique query.
So, as you said, in your comment, (Semester=1 OR Semester=2 OR Semester=3") would be better.

From your code, I assume that semester is a varchar field. That's bad because, if it was a number, you could write a better query using minimum and maximum values. (Also if you can assure that semesters are in consecutive order.). (Semester >= 1 AND Semester <= 3)

However, the best performance you can get out of this, is if in your datatable has the correct indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM