Exclude Certain Records with Certain Values SQL Select

前端 未结 2 669
悲哀的现实
悲哀的现实 2021-01-28 22:56

I\'ve used the solution (by AdaTheDev) from the thread below as it relates to the same question: How to exclude records with certain values in sql select

But when applyi

相关标签:
2条回答
  • 2021-01-28 23:15

    You could use EXISTS:

    SELECT StoreId
    FROM sc
    WHERE NOT EXISTS (
      SELECT 1
      FROM StoreClients cl
      WHERE sc.StoreId = cl.StoreId
      AND cl.ClientId = 5
      )
    

    Make sure to create an index on StoreId column. Your query could also benefit from an index on ClientId.

    0 讨论(0)
  • 2021-01-28 23:32

    40,000 rows isn't too big for SQL server. The other thread is suggesting some good queries too. If I understand correctly, your problem right now is the performance. To improve the performance, you may need to add a new index to your table, or just update the statistics on your table.

    If you are running this query in SQL server management studio for testing and performance tuning, make sure that you are selecting "Include Actual Execution Plan". This will make the query to take longer to execute, but the execution plan result can help you on where the time is spent. It usually suggests adding some indexes to improve the performance, which is easy to follow. If adding new indexes are not possible, then you have to spend some time reading on how to understand the execution plan's diagram and finding the slow areas.

    0 讨论(0)
提交回复
热议问题