You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

搜索引擎查询中避免双重NOT IN子查询的优化需求

Optimizing the Search Query to Exclude Mutual Blocks

Hey there! Let's refine your query to avoid those nested NOT IN subqueries while keeping the core logic—excluding any users who have a mutual block relationship with user ID 1. Here are two efficient, cleaner approaches:

Approach 1: Use LEFT JOIN + IS NULL

This method leverages left joins to flag blocked users and filters out anyone with a matching block record. It’s often more performant than NOT IN, especially if you have indexes on the user and block columns of the block table.

SELECT 
    c.nome, 
    c.user, 
    c.id, 
    p.foto 
FROM users c 
LEFT JOIN profile_picture p ON c.id = p.user
-- Join to find users blocked by ID 1
LEFT JOIN block b1 ON c.id = b1.block AND b1.user = '1'
-- Join to find users who blocked ID 1
LEFT JOIN block b2 ON c.id = b2.user AND b2.block = '1'
-- Keep only users with no matching block records in either join
WHERE b1.id IS NULL 
  AND b2.id IS NULL
  -- Add your other existing conditions here (the "c..." part from your original query)

How it works:

  • We left join the block table twice: once to check if user 1 has blocked the current user (b1), and once to check if the current user has blocked user 1 (b2).
  • The WHERE clause filters out any users where either join found a matching block record (since b1.id IS NULL means no block from user 1 to this user, and b2.id IS NULL means no block from this user to user 1).

Approach 2: Use NOT EXISTS

If you prefer a more readable approach that’s also efficient, NOT EXISTS is a great choice. It checks for the absence of block records in a direct, intuitive way:

SELECT 
    c.nome, 
    c.user, 
    c.id, 
    p.foto 
FROM users c 
LEFT JOIN profile_picture p ON c.id = p.user
WHERE 
  -- No record where user 1 blocked this user
  NOT EXISTS (
      SELECT 1 
      FROM block b 
      WHERE b.user = '1' AND b.block = c.id
  )
  -- No record where this user blocked user 1
  AND NOT EXISTS (
      SELECT 1 
      FROM block b 
      WHERE b.block = '1' AND b.user = c.id
  )
  -- Add your other existing conditions here

Why this beats NOT IN:

  • NOT EXISTS handles NULL values far more predictably than NOT IN (if your block columns ever have NULLs, NOT IN can return unexpected empty results).
  • Most databases optimize NOT EXISTS well, especially with proper indexing on block.user and block.block.

Both approaches match the behavior of your original query but are cleaner and often more efficient. Just plug back any additional conditions you had in the (c...) part of your original query into the WHERE clause of either version.

内容的提问来源于stack exchange,提问作者RGS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:09:53