1- Do the Case when inside or outside the Where condition affect the performance , dose it affect the Query Plan of a Single Query , dose affect the Picking of the index to be used for a single Query.
2- is better to to Write 2 or more Queries each handling one option of the Case when value rather than writing a one Single Query having a case when statement.
Example:
Select Acc ,
Case
when Calculation_type = 'M' then Min_value
when Calculation_type = 'E' then Ending_value
Else
Average_BLC
END
From Table
alternative
Select Acc , Min_value
From Table
where Calculation_type = 'M'
union all
Select Acc , Ending_value
From Table
where Calculation_type = 'E'
union all
Select Acc , Average_BLC
From Table
where Calculation_type = 'A'
In this case, the first option will almost certainly be more efficient since it only requires that you hit the table (and/or the indexes on the table) once rather than hitting them three times (I'm assuming that you intended the first query to have a WHERE calculation_type IN ('A', 'E', 'M')
so that the results match the second query).
I suppose that if you have three different covering indexes, one for each of the queries you're UNION ALL
-ing together and that it makes sense for the optimizer to use those indexes, there may be little difference.
Assuming you have to hit the table for some of these columns, however, you would want to be able to read the table row once and get all the column values rather than reading the block three different times for the three different subqueries.
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.