Pruebas de desigualdad en T-SQL
Acabo de encontrar esto en una cláusula WHERE:
AND NOT (t.id = @id)
¿Cómo se compara esto con:
AND t.id != @id
O con:
AND t.id <> @id
Siempre escribiría esto último yo mismo, pero claramente alguien piensa diferente. ¿Uno va a funcionar mejor que el otro? Sé que usar <>
o !=
va a romper cualquier esperanza de usar un índice que podría haber tenido, pero seguramente el primer enfoque anterior sufrirá el mismo problema?
4 answers
Estos 3 obtendrán exactamente el mismo plan de ejecución
declare @id varchar(40)
select @id = '172-32-1176'
select * from authors
where au_id <> @id
select * from authors
where au_id != @id
select * from authors
where not (au_id = @id)
También dependerá de la selectividad del propio índice, por supuesto. Yo siempre uso au_id @id
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-10 05:41:54
Tenga en cuenta que el != el operador no es SQL estándar. Si desea que su código sea portátil (es decir, si le importa), use en su lugar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-11 16:01:42
Solo un pequeño ajuste para aquellos que vengan después:
El operador de igualdad genera un valor desconocido cuando hay un null y el valor desconocido es tratado como falso. No (desconocido) es desconocido
En el siguiente ejemplo trataré de decir si un par (a1, b1) es igual a (a2, b2). Tenga en cuenta que cada columna tiene 3 valores 0, 1 y NULL.
DECLARE @t table (a1 bit, a2 bit, b1 bit, b2 bit)
Insert into @t (a1 , a2, b1, b2)
values( 0 , 0 , 0 , NULL )
select
a1,a2,b1,b2,
case when (
(a1=a2 or (a1 is null and a2 is null))
and (b1=b2 or (b1 is null and b2 is null))
)
then
'Equal'
end,
case when not (
(a1=a2 or (a1 is null and a2 is null))
and (b1=b2 or (b1 is null and b2 is null))
)
then
'not Equal'
end,
case when (
(a1<>a2 or (a1 is null and a2 is not null) or (a1 is not null and a2 is null))
or (b1<>b2 or (b1 is null and b2 is not null) or (b1 is not null and b2 is null))
)
then
'Different'
end
from @t
Tenga en cuenta que aquí esperamos resultados:
- Igual a null
- no igual a no ser igual
- diferente ser diferente
Pero obtenemos otro resultado
- Igual es nulo OK
- No igual es nulo???
- Diferente es diferente
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-05-20 15:25:54
No habrá ningún golpe de rendimiento, ambas declaraciones son perfectamente iguales.
HTH
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-11 15:58:48