后台報了一些異常日志,查閱后發現在 oracle 數據庫中使用 in 關鍵字條件不能超過 1000 個,當時寫查詢語句時沒有關注這個問題
總結一下解決方法
1.分多次查詢,對查詢要求不高的話。把入參的集合按照每個最大1000個來處理,分幾次查詢,然后把結果進行匯總,這樣就只用改動代碼,不用改動SQL。
2.把參數分開但還是一次查詢,使用 or 連接
select * from table where id in (1, 2, …, 1000) or id in (1001, …, 1999)
3.在 in 后面接一個查詢語句
select * from A where id in (select id from B)
4.與 3 類似,使用 with as 語法,把條件封裝成一個表
with temp as (select * from A) select * from B where id in (select id from temp)