SQL query: Simulating an “AND” over several rows instead of sub-querying

后端 未结 5 1161
轮回少年
轮回少年 2020-11-29 08:07

Suppose I have a \"tags\" table with two columns: tagid and contentid. Each row represents a tag assigned to a piece of content. I want a

5条回答
  •  囚心锁ツ
    2020-11-29 08:36

    Here's a solution that has worked much faster than the for me on a very large database of objects and tags. This is an example for a three-tag intersection. It just chains many joins on the object-tag table (objtags) to indicate the same object and stipulates the tag IDs in the WHERE clause:

    SELECT w0.objid
    
    FROM       objtags t0
    INNER JOIN objtags t1 ON t1.objid=t0.objid
    INNER JOIN objtags t2 ON t2.objid=t1.objid
    
    WHERE t0.tagid=512
      AND t1.tagid=256
      AND t2.tagid=128
    

    I have no idea why this runs faster. It was inspired by the search code in the MusicBrainz server. Doing this in Postgres, I usually get a ~8-10x speedup over the HAVING COUNT(...) solution.

提交回复
热议问题