From fd725d3363edd36d39fbbb378e864e817e28c93a Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 16 May 2012 05:12:19 +0100 Subject: [PATCH] merge image ID lists before scanning the images table, not after --- core/imageboard.pack.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index cd356513..22885329 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -613,6 +613,9 @@ class Image { } /** + * WARNING: this description is no longer accurate, though it does get across + * the general idea - the actual method has a few extra optimisiations + * * "foo bar -baz user=foo" becomes * * SELECT * FROM images WHERE @@ -746,32 +749,29 @@ class Image { $have_pos = count($positive_tag_id_array) > 0; $have_neg = count($negative_tag_id_array) > 0; - $sql = "SELECT images.* FROM images WHERE "; + $sql = "SELECT images.* FROM images WHERE images.id IN ("; if($have_pos) { $positive_tag_id_list = join(', ', $positive_tag_id_array); $sql .= " - images.id IN ( - SELECT image_id - FROM image_tags - WHERE tag_id IN ($positive_tag_id_list) - GROUP BY image_id - HAVING COUNT(image_id)>=$positive_tag_count - ) + SELECT image_id + FROM image_tags + WHERE tag_id IN ($positive_tag_id_list) + GROUP BY image_id + HAVING COUNT(image_id)>=$positive_tag_count "; } if($have_pos && $have_neg) { - $sql .= " AND "; + $sql .= " EXCEPT "; } if($have_neg) { $negative_tag_id_list = join(', ', $negative_tag_id_array); $sql .= " - images.id NOT IN ( - SELECT image_id - FROM image_tags - WHERE tag_id IN ($negative_tag_id_list) - ) + SELECT image_id + FROM image_tags + WHERE tag_id IN ($negative_tag_id_list) "; } + $sql .= ")"; $query = new Querylet($sql); if(strlen($img_search->sql) > 0) {