X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7D5FD52918 for ; Mon, 13 Jun 2005 12:01:34 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44735-02 for ; Mon, 13 Jun 2005 15:01:25 +0000 (GMT) Received: from server07.icaen.uiowa.edu (server07.icaen.uiowa.edu [128.255.17.47]) by svr1.postgresql.org (Postfix) with ESMTP id 2D169528E7 for ; Mon, 13 Jun 2005 12:01:23 -0300 (ADT) Received: from server11.icaen.uiowa.edu (server11.icaen.uiowa.edu [128.255.17.51]) by server07.icaen.uiowa.edu (8.13.3/8.12.9) with ESMTP id j5DF1G7e017186; (envelope-from ) Mon, 13 Jun 2005 10:01:20 -0500 (CDT) Received: from [192.168.1.11] ([12.215.118.172]) (authenticated user=jfmeinel) by server11.icaen.uiowa.edu (8.13.2/smtp-serv-1.7) with ESMTP id j5DElEmD029914 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256); (envelope-from ) Mon, 13 Jun 2005 09:47:15 -0500 (CDT) Message-ID: <42AD9C6E.3000705@arbash-meinel.com> Date: Mon, 13 Jun 2005 09:47:10 -0500 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Alex Stapleton Cc: pgsql-performance@postgresql.org Subject: Re: PostgreSQL using the wrong Index References: <2D1A68BF-B3A4-490E-8053-3CF6626B8DAF@advfn.com> In-Reply-To: X-Enigmail-Version: 0.92.0.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig01DE98644253D03897262417" X-Virus-Scanned: ClamAV version 0.83, clamav-milter version 0.83 on clamav.icaen.uiowa.edu X-Virus-Scanned: ClamAV version 0.83, clamav-milter version 0.83 on clamav.icaen.uiowa.edu X-Virus-Status: Clean X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/293 X-Sequence-Number: 12930 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig01DE98644253D03897262417 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Alex Stapleton wrote: > Oh, we are running 7.4.2 btw. And our random_page_cost = 1 > Which is only correct if your entire db fits into memory. Also, try updating to a later 7.4 version if at all possible. > On 13 Jun 2005, at 14:02, Alex Stapleton wrote: > >> We have two index's like so >> >> l1_historical=# \d "N_intra_time_idx" >> Index "N_intra_time_idx" >> Column | Type >> --------+----------------------------- >> time | timestamp without time zone >> btree >> Just so you are aware, writing this as: "We have an index on N_intra(time) and one on N_Intra(symbol, time)" is a lot more succinct. >> >> l1_historical=# \d "N_intra_pkey" >> Index "N_intra_pkey" >> Column | Type >> --------+----------------------------- >> symbol | text >> time | timestamp without time zone >> unique btree (primary key) >> >> and on queries like this >> >> select * from "N_intra" where symbol='SOMETHING WHICH DOESNT EXIST' >> order by time desc limit 1; >> >> PostgreSQL takes a very long time to complete, as it effectively >> scans the entire table, backwards. And the table is huge, about 450 >> million rows. (btw, there are no triggers or any other exciting >> things like that on our tables in this db.) >> >> but on things where the symbol does exist in the table, it's more or >> less fine, and nice and fast. > What happens if you do: SELECT * FROM "N_intra" WHERE symbol='doesnt exist' ORDER BY symbol, time DESC LIMIT 1; Yes, symbol is constant, but it frequently helps the planner realize it can use an index scan if you include all terms in the index in the ORDER BY clause. >> >> Whilst the option the planner has taken might be faster most of the >> time, the worst case scenario is unacceptable for obvious reasons. >> I've googled for trying to force the use of a specific index, but >> can't find anything relevant. Does anyone have any suggestions on >> getting it to use an index which hopefully will have better worst >> case performance? > Try the above first. You could also create a new index on symbol CREATE INDEX "N_intra_symbol_idx" ON "N_intra"(symbol); Then the WHERE clause should use the symbol index, which means it can know quickly that an entry doesn't exist. I'm not sure how many entries you have per symbol, though, so this might cause problems in the ORDER BY time portion. I'm guessing what you really want is to just do the ORDER BY symbol, time. John =:-> --------------enig01DE98644253D03897262417 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (Cygwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD4DBQFCrZxxJdeBCYSNAAMRArsiAJ4lhNxy7mnwvZkCyQW0JxzXRu7YPACWIYKo LXNvDF3+FCwWeHOJn/cbcg== =Ya71 -----END PGP SIGNATURE----- --------------enig01DE98644253D03897262417--