Index: trunk/lib/Bookmarks.pm
===================================================================
--- trunk/lib/Bookmarks.pm	(revision 76)
+++ trunk/lib/Bookmarks.pm	(revision 78)
@@ -74,22 +74,21 @@
 }
 
-sub get_bookmarks {
-    my $self = shift;
-    my $params = shift || {};
-    my $search = Bookmarks::Search->new($params);
-
-    # build the query
+sub _sql_where_clause {
+    my $self = shift;
+    my $search = shift;
+
+    # build the where clause
     my @sql;
-
     if (@{ $search->tags }) {
+        push @sql, 'where resources.uri in';
+        # subquery to find tagged URIs
+        push @sql, '(';
         my $intersect = 0;
         for my $tag (@{ $search->tags }) {
             push @sql, 'intersect' if $intersect;
-            push @sql, 'select resources.*, bookmarks.* from resources join bookmarks on resources.uri = bookmarks.uri';
-            push @sql, 'join tags on resources.uri = tags.uri where tags.tag =', \$tag;
+            push @sql, 'select uri from tags where tag =', \$tag;
             $intersect++;
         }
-    } else {
-        push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri';
+        push @sql, ')';
     }
     if ($search->query) {
@@ -97,4 +96,17 @@
         push @sql, (@{ $search->tags } ? 'and' : 'where'), 'title like', \$fuzzy_match;
     }
+    return @sql;
+}
+
+sub get_bookmarks {
+    my $self = shift;
+    my $params = shift || {};
+    my $search = Bookmarks::Search->new($params);
+
+    # build the query
+    my @sql;
+
+    push @sql, 'select * from resources join bookmarks on resources.uri = bookmarks.uri';
+    push @sql, $self->_sql_where_clause($search);
     push @sql, 'order by ctime desc';
     push @sql, ('limit', \$search->limit) if $search->limit;
