简体   繁体   English

哪一个是更好的解决方案?

[英]which one is a better solution?

Solution 1: 解决方案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: 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. 更新:我将数据存储到DATATABLE ....然后将其与ListView一起使用。

Is there some reason not to use "IN" to grab all these semester numbers in one query, as in something like this? 是否有一些理由不使用“IN”在一个查询中抓取所有这些学期数字,就像这样的东西?

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. 因为如果你可以使用“IN”,那就行。 "Don't Repeat Yourself," yeah sure, ya betcha. “不要重复自己,”是的,你敢打赌。

One principle of writing software is: Dont Repeat Yourself (DRY). 编写软件的一个原则是:不要重复自己(DRY)。 So solution 1 is definitely better. 所以解决方案1肯定更好。 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. 所以,正如你所说,在你的评论中, (Semester=1 OR Semester=2 OR Semester=3")会更好。

From your code, I assume that semester is a varchar field. 从您的代码中,我假设semester是一个varchar字段。 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. 但是,您可以获得的最佳性能是,在数据表中是否具有正确的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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