Skip to main content
syntax error
Source Link
Aaron Bertrand
  • 282.4k
  • 37
  • 471
  • 469

The performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instead, like this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

Here are a few helpful links that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

The performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instead, like this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

Here are a few helpful links that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

The performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instead, like this

select *
from ALERTTRAN_01
where not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

Here are a few helpful links that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

deleted 2 characters in body
Source Link
Dale K
  • 28.3k
  • 15
  • 62
  • 87

The Performanceperformance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instaedinstead, Likelike this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

hereHere are a few helpful linkSlinks that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

The Performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instaed, Like this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

here are a few helpful linkS that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

The performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instead, like this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

Here are a few helpful links that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery

Source Link

The Performance with IN and NOT IN can be a little tricky, because then initially loads all the data within the sub query to the memory before it matches the records. So if there are more rows, it'll drain the performance. So try with EXISTS and NOT EXISTS instaed, Like this

select *
from ALERTTRAN_01
where PARENT_ALERT_ID not EXISTS (
    select PARENT_ALERT_ID
    from nonbulkclosealert WHERE PARENT_ALERT_ID = ALERTTRAN_01.PARENT_ALERT_ID 
)

here are a few helpful linkS that'll explain the advantages of EXISTS over IN

https://www.sqlservercentral.com/blogs/not-exists-vs-not-in

https://www.red-gate.com/hub/product-learning/sql-prompt/consider-using-not-exists-instead-not-subquery