Showing posts with label clause. Show all posts
Showing posts with label clause. Show all posts

Wednesday, March 21, 2012

Non-equi Joins using <>

Can we replace the where clause operator of '<>' with a join syntax condition.

Could someone explain me the dynamics behind the following query:

select a.b, c.d from a inner join c on a.b <> c.d

This could effectively replace the NOT IN and EXCEPT operators

No.. <> wont work as you expected. You query is similar to the following query,

select * from a cross join cWhere a.b <> c.d

Since it is set opertation your query will guide you to make a cross join and it only filter those values which are present on both the tables.

You have to use NOT IN or NOT EXISTS on your query. Join will be compared on both tables row by row. The IN & EXISTS will be verified with the one table value with other tables rows of value.

|||

Since, I am avoiding the use of NOT IN & NOT EXISTS due to its obvious perofrmance degradations, I also wouldn't use a Cross Join (which I think for tables with more rows would be terribly slow). Hence, would I get a Join query to have a performance upgrade over them.

|||

Hi there,

NOT IN and NOT EXISTS are no the same when it comes to performance. NOT EXISTS is much better in terms of performance especially if you're checking for a specific value as the query would return a TRUE value if a match is found and therefore avoid further iterations.

|||

I tend to use NOT EXISTS in these situations to take advantage of the semi-joins. Also, beware that the EXCEPT operator is typically slower.

Here are some previous threads that discuss NOT IN versus NOT EXISTS:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=299702&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=726903&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1087482&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=637335&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=654087&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=532892&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=607796&SiteID=1

Here is a thread that discusses NOT IN versus EXCEPT:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1021998&SiteID=1

Monday, March 19, 2012

Nonclustered index helps ORDER BY?

Hi,
Does nonclustered index help ORDER BY clause in the query? If yes, how?
Thanks in advance,
AminSQL Server can use a non-clustered index to provide result ordering by
scanning the index in ORDER BY sequence. However, the optimizer might
choose other methods to provide the ordering (e.g. SORT) due to other
factors, such as using another index to satisfy WHERE or JOIN predicates.
--
Hope this helps.
Dan Guzman
SQL Server MVP
"Amin Sobati" <amins@.morva.net> wrote in message
news:%23sWEpkhgEHA.592@.TK2MSFTNGP11.phx.gbl...
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes, how?
> Thanks in advance,
> Amin
>|||Amin Sobati wrote:
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes,
> how? Thanks in advance,
> Amin
It will also help if your non-clustered index is a covering index.
Remember, all clustered index keys are a part of non-clustered indexes,
which makes designign covering indexes a little easier... in some cases.
--
David G.

Nonclustered index helps ORDER BY?

Hi,
Does nonclustered index help ORDER BY clause in the query? If yes, how?
Thanks in advance,
AminSQL Server can use a non-clustered index to provide result ordering by
scanning the index in ORDER BY sequence. However, the optimizer might
choose other methods to provide the ordering (e.g. SORT) due to other
factors, such as using another index to satisfy WHERE or JOIN predicates.
Hope this helps.
Dan Guzman
SQL Server MVP
"Amin Sobati" <amins@.morva.net> wrote in message
news:%23sWEpkhgEHA.592@.TK2MSFTNGP11.phx.gbl...
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes, how?
> Thanks in advance,
> Amin
>|||Amin Sobati wrote:
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes,
> how? Thanks in advance,
> Amin
It will also help if your non-clustered index is a covering index.
Remember, all clustered index keys are a part of non-clustered indexes,
which makes designign covering indexes a little easier... in some cases.
David G.

Nonclustered index helps ORDER BY?

Hi,
Does nonclustered index help ORDER BY clause in the query? If yes, how?
Thanks in advance,
Amin
SQL Server can use a non-clustered index to provide result ordering by
scanning the index in ORDER BY sequence. However, the optimizer might
choose other methods to provide the ordering (e.g. SORT) due to other
factors, such as using another index to satisfy WHERE or JOIN predicates.
Hope this helps.
Dan Guzman
SQL Server MVP
"Amin Sobati" <amins@.morva.net> wrote in message
news:%23sWEpkhgEHA.592@.TK2MSFTNGP11.phx.gbl...
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes, how?
> Thanks in advance,
> Amin
>
|||Amin Sobati wrote:
> Hi,
> Does nonclustered index help ORDER BY clause in the query? If yes,
> how? Thanks in advance,
> Amin
It will also help if your non-clustered index is a covering index.
Remember, all clustered index keys are a part of non-clustered indexes,
which makes designign covering indexes a little easier... in some cases.
David G.

Monday, March 12, 2012

non-bitwise exclusive or?

Is there a way to do a logical exclusive OR (XOR) in sql server?

I'm trying to do this in where clause, something like:

WHERE
(not exists (select 1 from table a where a.date > '01/30/03') XOR
exists (select 1 from table a where a.date < '01/30/03'))

Thanks!no idea what you mean by "exclusive or" -- do you mean one is true or the other is true but not both false and not both true?|||Originally posted by r937
no idea what you mean by "exclusive or" -- do you mean one is true or the other is true but not both false and not both true?

Regular "OR" logic with boolean values a & b:

a = true, b = false returns TRUE
a = false, b = true returns TRUE
a = true, b = true returns TRUE
a = false, b = false returns FALSE

Exclusive "OR" logic (XOR) with same:

a = true, b = false returns TRUE
a = false, b = true returns TRUE
a = true, b = true returns FALSE
a = false, b = false returns FALSE

In other words XOR returns true when ONLY one of the two arguments is true, but not both.|||the general structure is:

declare @.a int
declare @.b int

set @.a=1
set @.b=1

if not ( (@.a=1) AND (@.b=1) ) AND not((@.a=0) AND (@.b=0))|||okay, translating this to your example, your two conditions are

a: not exists (select 1 from table a where a.date > '01/30/03'
b: exists (select 1 from table a where a.date < '01/30/03')

and you want exactly one of these to be true

that's easy!

WHERE (
(not exists (select 1 from table a
where a.date > '01/30/03'))
AND
NOT (exists (select 1 from table a
where a.date < '01/30/03'))
)
OR (
NOT (not exists (select 1 from table a
where a.date > '01/30/03'))
AND (exists (select 1 from table a
where a.date < '01/30/03'))
)|||[QUOTE][SIZE=1]Originally posted by r937
okay, translating this to your example, your two conditions are

yeah but I was hoping to be able to do it using a single operator like you can in ORACLE, which uses XOR. ;) O well, guess my monstrous SQL will get more so, and more proof that microsoft products suck hehe|||rather than look at the shortcomings of the database language, i would examine my conditions a little more closely

the WHERE clause comes down to either

1) table1 contains no dates at all, or else at most one date, 01/30/03
or
2) table1 contains at least one date on each side of 01/30/03, and possibly even 01/30/03 itself

i cannot imagine where this might be a real world situation that you want to filter for, but i can tell you one thing, i would certainly code it so that the purpose of the filtering is crystal clear, rather than obfuscate the meaning using some fancy logical operator that the next guiy to maintain the code (which could be you yourself six months from now) first has to figure out before even touching the code

just because a particular language feature exists doesn't mean it is appropriate to use it|||I think I was somewhat unclear. I actually just made up the two date clauses. My real clauses are very very large and so I didnt want to replicate them in here because that would have just been confusing.

I didn't realize that XOR was such a fancy operator ;)|||okay, i understand

good luck

you're gonna need it, eh :cool:|||We are sincerely sorry your code is bloated. Please accept our deepest condolences.|||I suppose you could write an XOR function. Deosn't seem like it would be that hard.|||You would have to pass boolean values as parameters, or convert them to bits (in which case you could just use the bitwise EXCLUSIVE OR operator ("^").|||Hmm. Thinking about it, it actually would be very hard to write.|||where
(
case
when (complex_clause_1_true )
then 1
else 0
end
^
case
when (complex_clause_2_true )
then 1
else 0
end
) = 1|||HanafiH, that is insidious, devious, marginally incomprehensible...

... and gorgeous!

nice one

i would put a great big humungous comment next to that ^ explaining that it's not a typo, it's an actual legitimate operator

first time i've seen it, and i had to go look it up in BOL to confirm

kudos to you|||The logic is simple. It's writing it as a function that is the problem.

Unfortunately, a returned boolean result is not a valid SQL server datatype and so can't be passed as a parameter without converting it first. And once you convert it you can use any of the bitwise operators, so passing them to a function doesn't gain you much.

My preferred conversion would be:
select cast(count(*) as bit) from table
...but that's just because I have a mild aversion to CASE statements.|||count(*) ? You would really rather do that? Man, you ain't seen the data then!|||Count(*) is very fast, because under most circumstances the optimizer need only pull the rowcount from the system tables.|||Count(*) is very fast, because under most circumstances the optimizer need only pull the rowcount from the system tables.
Is this true in MS-SQL... When I test this the query plan estimates reading the clustered index, and with a trace it appears that the entire index is being read. I tried this both before and after updating statistics.|||If you have additional filtering criteria it would need to resort to searching the index, so the strategy to use will ultimately depend on exactly what "complex_clause_1" and "complex_clause_2" are.

I'd be willing to bet that in blm14_cu's case the statement could be rewritten with much greater clarity using JOINs than his EXISTS clauses, after which he could use some of the Null-related functions such as ISNULL and NULLIF to implement his logic.

blm14_cu is bashing SQL Server, but poorly written code is going to be difficult to translate to any platform.|||Originally posted by blindman
If you have additional filtering criteria it would need to resort to searching the index, so the strategy to use will ultimately depend on exactly what "complex_clause_1" and "complex_clause_2" are.

I'd be willing to bet that in blm14_cu's case the statement could be rewritten with much greater clarity using JOINs than his EXISTS clauses, after which he could use some of the Null-related functions such as ISNULL and NULLIF to implement his logic.

blm14_cu is bashing SQL Server, but poorly written code is going to be difficult to translate to any platform.

Absolutely. The XOR condition in relational terms is an inverse intersection and should be convertible to NOT IN (intersection) in almost all cases.

NON_EMPTY_BEHAVIOR if NON EMPTY isn't used

A cube I'm working on has lots of calculations. But none of the MDX queries in the reports use the NON EMPTY clause or the NonEmpty function. What I'm wondering is whether the optimizer or any other part of the Formula Engine uses NON_EMPTY_BEHAVIOR for anything else besides removing empty cells from the resultset.

If the Sum function is summing two calculated measures, will a NEB on those two calculated measures help the Sum function, for example? That's the kind of thing I'm looking for... places where NEB is used other than removing entire cells from the resultset.

Yes, NON_EMPTY_BEHAVIOR is used even if there are no NON EMPTY queries. Some more information about it here: http://www.sqljunkies.com/WebLog/mosha/archive/2006/11/05/non_empty_behavior.aspx|||

Mosha, thanks for the reply.

The takeaway from your blog regarding NEBs is that the multiplication operator (as in the * operator used for math, not for crossjoin) uses the NEB. Are there any others?

What I was hoping for was sort of a list of places where NEB's are used unless that's an enormous list.

|||

Multiplication was one of the examples. Many other (but not all) MDX functions also take it into account. The exact list is not easy to build, because there are some other additional factors affecting whether or not NEB will be used.

The most important thing though, is to ensure that NEB is used correctly, before thinking about perf. Even in this forum, we routinely see people using NEB incorrectly, which can and does cause wrong results.