Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TM13d-0002pk-Pl for pgsql-performance@postgresql.org; Wed, 10 Oct 2012 18:31:01 +0000 Received: from qmta15.emeryville.ca.mail.comcast.net ([2001:558:fe2d:44:76:96:27:228]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TM13a-000836-Ox for pgsql-performance@postgresql.org; Wed, 10 Oct 2012 18:31:01 +0000 Received: from omta03.emeryville.ca.mail.comcast.net ([76.96.30.27]) by qmta15.emeryville.ca.mail.comcast.net with comcast id 9R821k0050b6N64AFWWvkW; Wed, 10 Oct 2012 18:30:55 +0000 Received: from mail.hathawaymix.org ([98.202.113.199]) by omta03.emeryville.ca.mail.comcast.net with comcast id 9WRu1k00h4JBJii8PWRvzK; Wed, 10 Oct 2012 18:25:55 +0000 Received: from flamingo.localdomain (unknown [10.23.45.114]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.hathawaymix.org (Postfix) with ESMTPSA id 650721204084; Wed, 10 Oct 2012 12:25:54 -0600 (MDT) Message-ID: <5075BDB2.2030507@hathawaymix.org> Date: Wed, 10 Oct 2012 12:25:54 -0600 From: Shane Hathaway User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: =?ISO-8859-1?Q?Fran=E7ois_Beausoleil?= CC: pgsql-performance@postgresql.org Subject: Re: Ways to speed up ts_rank References: <5074996C.5020809@hathawaymix.org> <546C964C-5BE7-464D-9EC6-5FC816AEE65E@teksol.info> In-Reply-To: <546C964C-5BE7-464D-9EC6-5FC816AEE65E@teksol.info> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201210/128 X-Sequence-Number: 48087 On 10/10/2012 06:38 AM, François Beausoleil wrote: > Notice the ts_rank() is on the outer query, which means it'll only > run on the subset of documents which match the query. This is > explicitly mentioned in the docs: > > """Ranking can be expensive since it requires consulting the tsvector > of each matching document, which can be I/O bound and therefore slow. > Unfortunately, it is almost impossible to avoid since practical > queries often result in large numbers of matches.""" > > (last paragraph of) > http://www.postgresql.org/docs/current/static/textsearch-controls.html#TEXTSEARCH-RANKING Indeed, I have studied that paragraph in depth, trying to gather as much possible meaning from it as I can. :-) However, the following two queries take exactly the same time, suggesting to me that ts_rank_cd is really only looking at matching rows, not all rows: SELECT docid, coefficient * ts_rank_cd('{0.1, 0.2, 0.5, 1.0}', text_vector, to_tsquery('english', 'stuff')) AS rank FROM pgtextindex WHERE (text_vector @@ to_tsquery('english', 'stuff')) ORDER BY rank DESC limit 3; SELECT docid, coefficient * ts_rank_cd('{0.1, 0.2, 0.5, 1.0}', text_vector, to_tsquery('english', 'stuff')) AS rank FROM (SELECT * FROM pgtextindex WHERE (text_vector @@ to_tsquery('english', 'stuff'))) AS filtered ORDER BY rank DESC limit 3; Thanks for the suggestion though. By the way, all the tsvectors are already loaded into the kernel cache when I execute the queries, so ranking large documents is in fact CPU bound rather than I/O bound. The CPU is pegged for the whole time. Shane