我在fiddle的查詢如下。
select * from notification where status = 0 and ( notif_id in (select notif_id from notif_user where user_id = 1) OR notif_id in (select notif_id from notif_group where group_id = 1))
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=cad284e77218eb37461e60b6308bf85f
查詢按預期工作。但是,查詢是否會有任何效能問題。是否可以將內部查詢轉換為連線?
您的子查詢不是依賴子查詢,而是獨立的。也就是說,它們不引用您的
notification
表中的列,而只引用它們自己表中的列。所以這裡沒有效能問題。
您可以將子查詢表示為聯合查詢,並比較執行計劃統計資料。看看fiddle中的輸出,union似乎執行稍微更好。
另一種表達方式是使用exists