Search code examples
c#mysqlquery-optimizationibatis

Select Query Optimization


I have select query that fetch record based on a condition

Select * from Employee where EmpStatus=#EmpStatus#

The EmpStatus in the DB for each employee would either 0 or 1.

EmpID EmpName EmpStatus
***********************
1     Name1   0
2     Name2   0
3     Name4   1
4     Name5   1

When I pass EmpStatus as 1, I should get list containing ONLY 3 and 4. But if i pass EmpStatus as 0, ALL the 4 records should be fetched. How can this be done with a single optimal select query?


Solution

  • You could change = to >=:

    SELECT col1, col2 -- etc...
    FROM Employee
    WHERE EmpStatus >= #EmpStatus#
    

    The most important thing for performance is to add an appropriate index.