sql - Checking condition in IF vs WHERE clause -


I have a problem and I have two solutions to the problem that I will write a process that the required categories Rows will return The first solution is to pass the required categories as the boolean (or bit) values: -

  DECLARE @ ISRowType1Req bit; DECLARE @ ISRowType2Req bit; DECLARE @ tbl1 table (ID int, bread type varchar (50)); DECLARE @tmpTbl table (ID int, bread type varchar (50)); If IRowType1Req = 1 INSERT @tmpTbl SELECT * FROM @ TBL1 WHERE RowType = 'RowType1'; IF @IsRowType2Req = 1 @tmpTbl SELECT * FROM @ tbl1 IO RowType = 'RowType2'; SELECT * FROM @tmpTbl;   

Now, this solution only uses the IF section to select the required types of rows. The second one is: -

  DECLARE @ ISRowType1Req bit; DECLARE @ ISRowType2Req bit; DECLARE @ tbl1 table (ID int, bread type varchar (50)); SELECT * @ tbl1 where RowType = 'RowType1' and @ IsRowType1Req = 1 UNION SELECT * Where @ tbl1 the FROM RowType = 'RowType2' and @ IsRowType2Req = 1   

WHERE clause is satisfied Uses unwanted types of rows, can anyone tell me which will be faster and why?

The answer will probably depend on the size of the table as well as the size of each subset returned.

If the solution is to insert the rows into another table, which does not come cheap. On the other hand, the WHERE solution uses the union, which means sorting to remove duplicate entries. If you have given the Union with UNION ALL (which you can do safely because you do not duplicate at any of your two subsets), the WHERE solution will be better in two hands.

However, the code> @ TB 1 can not be more than one way, you can solve differently to issue a dynamically created @ IsRowType1Req and @ IsRowType2Req the values ​​0, 1 or against a table consisting of 2 rows can eat @ tbl1 mail. This way you will build the table:

  select Where 'RowType1' @ IsRowType1Req = 1 UNION select all gone 'RowType2' @ IsRowType2Req = 1   < p> and  
  SELECT * t join insider (Choose Where 'RowType1' @ IsRowType1Req = 1 UNION select all gone 'RowType2': the  @ tbl1 To join it, go to the ISRO Type2Req = 1) T (SR) on SR (Roti Type). RO Type = F. RO type; In a way, this could mean that "where" is "win" but you can rewrite the virtual table without using WHERE:  
  SELECT CASE @ ISRowType1Req when 1 then select 'RowType1' end UNION all the case @IsRowType2Req when 1 then 'RowType2' end   

or this way, values ​​creator SQL Server 2008 Using the started value:

  values ​​(case @ IsRowType1Req when 1 then 'RowType1' end), (case @ IsRowType2Req when 1 then 'RowType2' end)   

Table like this always Will join 2 rows, each row that contains a requested type or zero. The results of being included in that table will still be similar and match the results you want.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -