|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 8:34 AM
Points: 2,008,
Visits: 2,472
|
|
I have recently read about this in the below link.
http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/
I just heard about "NP-Complete Problem" first time :)
From Joe Celko...
This UNION vs OR problem has been in SQL for along time, not just T-SQL. The one-table VIEW with the OR is updatable; the UNION version counts as two tables, so the VIEW is not updateable, even tho it is logically equivalent.
When we wrote the Standards, we knew that VIEW updatign is an NP-Complete problem, so we went with the easiest definition for the Standards.
The conclusion is to use 'OR' instead UNION. Right?
karthik
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 2:08 AM
Points: 2,596,
Visits: 4,506
|
|
karthik M (2/5/2013)
I have recently read about this in the below link. http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/ I just heard about "NP-Complete Problem" first time :) From Joe Celko...
This UNION vs OR problem has been in SQL for along time, not just T-SQL. The one-table VIEW with the OR is updatable; the UNION version counts as two tables, so the VIEW is not updateable, even tho it is logically equivalent.
When we wrote the Standards, we knew that VIEW updatign is an NP-Complete problem, so we went with the easiest definition for the Standards.
The conclusion is to use 'OR' instead UNION. Right?
I wouldn't say so. As not-updateable VIEW can be made updateable with INSTEAD OF triggers help... I also think that use 'OR' over 'UNION' should be decided case-by-case based on best performance.
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 4:58 AM
Points: 38,074,
Visits: 30,370
|
|
Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 8:34 AM
Points: 2,008,
Visits: 2,472
|
|
few more thoughts ?
karthik
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:52 AM
Points: 1,333,
Visits: 4,016
|
|
karthik M (2/6/2013) few more thoughts ?
It depends.
MM
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 10:11 AM
Points: 1,333,
Visits: 1,803
|
|
No, I wouldn't use OR just because that makes a view updateable -- UPDATEs aren't typically done using views like that anyway.
SQL DBA,SQL Server MVP('07, '08, '09) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 2:08 AM
Points: 2,596,
Visits: 4,506
|
|
ScottPletcher (2/6/2013) No, I wouldn't use OR just because that makes a view updateable -- UPDATEs aren't typically done using views like that anyway.
What is the problem with updateable VIEW? I agree, usually views are not subjects of modification queries, but I don't think that "updateability" of a VIEW is evil enough to stop creating such views. What about VIEW which doesn't even need OR or UNION, it can be updateable, will you not have such views at all?
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:26 AM
Points: 5,705,
Visits: 11,136
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 10:11 AM
Points: 1,333,
Visits: 1,803
|
|
Eugene Elutin (2/7/2013)
ScottPletcher (2/6/2013) No, I wouldn't use OR just because that makes a view updateable -- UPDATEs aren't typically done using views like that anyway.What is the problem with updateable VIEW? I agree, usually views are not subjects of modification queries, but I don't think that "updateability" of a VIEW is evil enough to stop creating such views. What about VIEW which doesn't even need OR or UNION, it can be updateable, will you not have such views at all?
I think you misread what I said. I don't object to updateable views per se. I meant I wouldn't change the way I coded a view just to make the view updateable (unless I already intended to UPDATE via that view).
SQL DBA,SQL Server MVP('07, '08, '09) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 2:43 PM
Points: 10,990,
Visits: 10,576
|
|
karthik M (2/5/2013) The conclusion is to use 'OR' instead UNION. Right? No.
Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.
SQL is a declarative language: you specify the results you want, not how to get them; that's the optimizer's job.
Regarding the original article, it's interesting that the author chose not to make the clustered index on the table UNIQUE, even though the clustering key is an IDENTITY column. Had he done so, the example query would have produced this execution plan:

The example was also carefully crafted to use a very small number of rows and unhelpful nonclustered indexes. Joe Celko's comment about updatable views is mildly interesting if you want to understand why the SQL standard is the mess it is, but it has little practical consequence.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|