Is the query well optimized

BenTam-3003 686 Reputation points
2022-08-15T03:36:39.747+00:00

Dear All,

According to the design of the index, is the following query well optimized?

sSql = "Select Top 1 SubjectID, CourseID, EnrolDate, Absence, Remark from Course Order by StudentID = 38

231111-design.gif

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,607 questions
{count} votes

Accepted answer
  1. Christopher Geckert 91 Reputation points
    2022-08-15T05:27:52.5+00:00

    I think you're missing a where clause.
    Select Top 1 SubjectID, CourseID, EnrolDate, Absence, Remark from Course Where StudentID = 38
    Because you filter by StudentID 38 you don't need to order by it. After that, the Query should be fine since StudentID is part of the primary key, so SQL Server should use index seek to look for the enry.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. BenTam-3003 686 Reputation points
    2022-08-15T08:07:53.963+00:00

    Hi Christopher,

    Thanks for your reply.

    0 comments No comments