简体   繁体   中英

How can I make large IN clauses more efficient in SQL Server?

My current query runs very slow when accessing a DB with pretty large tables

SELECT * 
FROM table1
WHERE timestamp BETWEEN 635433140000000000 AND 635433150000000000
  AND ID IN ('element1', 'element2', 'element3', ... , 'element 3002');

As you can see the IN clause has several thousand values. This query is executed roughly every second.

Is there another way to write it to improve performance?

将IN的元素添加到索引临时(如果元素更改)或永久表(如果元素是静态的)中,并对其进行内部联接。

This is your query:

SELECT *
FROM table1
WHERE timestamp BETWEEN 635433140000000000 AND 635433150000000000 AND
      ID IN ('element1', 'element2', 'element3', ... , 'element 3002');

The query is fine. Add an index on table1(id, timestamp) .

The best answer depends on how those element ID listings are selected, but it all comes down to one thing: getting them into a table somewhere that you can join against. That will help performance tremendously. But again, the real question here is how best to get those items into a table, and that will depend on information not yet included in the question.

You should check your execution plan, I guess you could have a parameter sniffing problem caused by your between. Check if the actual rows are way off you expected values. And you can rewrite your IN to a EXISTS, which works inside like a INNER JOIN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM