From benjamin@moonfactory.co.jp Tue Mar 22 09:23:05 2005 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 C94DF56999 for ; Tue, 22 Mar 2005 09:23:05 +0000 (GMT) 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 24328-09 for ; Tue, 22 Mar 2005 09:22:49 +0000 (GMT) Received: from moonfactory.co.jp (0.moonfactory.co.jp [211.5.120.193]) by svr1.postgresql.org (Postfix) with ESMTP id 6F997569A7 for ; Tue, 22 Mar 2005 09:22:28 +0000 (GMT) Received: from benjamindon (192.168.133.146) by moonfactory.co.jp with ESMTP (Eudora Internet Mail Server 3.0.3) for ; Tue, 22 Mar 2005 18:22:27 +0900 Message-ID: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> From: "Layet Benjamin" To: Subject: best practices with index on varchar column Date: Tue, 22 Mar 2005 18:22:24 +0900 Organization: Moonfactory MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_006F_01C52F0C.18A24D50" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.119 tagged_above=0 required=5 tests=AWL, HTML_50_60, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200503/429 X-Sequence-Number: 11134 This is a multi-part message in MIME format. ------=_NextPart_000_006F_01C52F0C.18A24D50 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit Hi everyone, I hope it is the correct newsletter for this question. Can I use an index on a varchar column to optimize the SELECT queries that use " column LIKE 'header%' "? If yes what is the best tree algotithm to use ? I don't care about optimising INSERT, DELETE and UPDATE queries, as they are only done at night when the load is very low. Thank you very much for any help, Benjamin Layet ------=_NextPart_000_006F_01C52F0C.18A24D50 Content-Type: text/html; charset="iso-2022-jp" Content-Transfer-Encoding: quoted-printable
Hi everyone,
I hope it is the correct newsletter for this question.
 
Can I use an index on a varchar column to optimize the SELECT = queries that=20 use " column LIKE 'header%'  "?
If yes what is the best tree algotithm to use ?
 
I don't care about optimising INSERT, DELETE and UPDATE queries, as = they=20 are only done at night when the load is very low.
Thank you very much for any help,
Benjamin Layet
 
 
------=_NextPart_000_006F_01C52F0C.18A24D50-- From qnex42@gmail.com Tue Mar 22 09:49:29 2005 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 98C9B564DA for ; Tue, 22 Mar 2005 09:49:29 +0000 (GMT) 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 41606-01 for ; Tue, 22 Mar 2005 09:49:25 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.193]) by svr1.postgresql.org (Postfix) with ESMTP id A557A56439 for ; Tue, 22 Mar 2005 09:49:23 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id y7so1229212rne for ; Tue, 22 Mar 2005 01:49:26 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=Kxiz56uBhdsQYgGHnmwEiRulDeYh0HO9X7D7g4e4Eh9/nkqLqle+i5ZnfiK9vx2WzY262tfxAiEqZ96OBlIj3wSaNUTSlORiOaPhpIrZzhPYgIVF5MalOHUC0b/qkL0wCbRkWuywpXcROWBboiJ9cjpLOvK/h+8TKca7f8SjMEU= Received: by 10.38.65.20 with SMTP id n20mr6158813rna; Tue, 22 Mar 2005 01:49:25 -0800 (PST) Received: by 10.38.101.75 with HTTP; Tue, 22 Mar 2005 01:49:25 -0800 (PST) Message-ID: <758d5e7f050322014953c311fe@mail.gmail.com> Date: Tue, 22 Mar 2005 10:49:25 +0100 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column In-Reply-To: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit References: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.738 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200503/430 X-Sequence-Number: 11135 On Tue, 22 Mar 2005 18:22:24 +0900, Layet Benjamin wrote: > Can I use an index on a varchar column to optimize the SELECT queries that > use " column LIKE 'header%' "? > If yes what is the best tree algotithm to use ? Yes, that is the correct place. The best tree algorithm is B-Tree, which is the default. So no need for giving 'USING ...' to CREATE INDEX. The other types of indexes are either not trees (HASH), different and more complex (GiST, RTREE) kinds of trees which are there for different kinds of data (spatial, full text, etc). Remember to VACUUM ANALYZE this table from time to time, so the planner can judge efficiently whether to use this new index or not. Use EXPLAIN ANALYZE SELECT .... to see whether the index is really used. > I don't care about optimising INSERT, DELETE and UPDATE queries, as they are > only done at night when the load is very low. > Thank you very much for any help, Oh, they can benefit from the index anyhow. :) Regards, Dawid From lists@boutiquenumerique.com Tue Mar 22 10:49:45 2005 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 6FC8E564BD for ; Tue, 22 Mar 2005 10:49:45 +0000 (GMT) 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 67402-07 for ; Tue, 22 Mar 2005 10:49:36 +0000 (GMT) Received: from boutiquenumerique.com (boutiquenumerique.com [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id 36B14563F1 for ; Tue, 22 Mar 2005 10:49:35 +0000 (GMT) Received: (qmail 19256 invoked from network); 22 Mar 2005 11:49:46 +0100 Received: from unknown (HELO localhost) (boutiquenumerique-lists@192.168.0.4) by boutiquenumerique.com with SMTP; 22 Mar 2005 11:49:46 +0100 To: "Layet Benjamin" , pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column References: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> Message-ID: Date: Tue, 22 Mar 2005 11:49:36 +0100 From: PFC Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> User-Agent: Opera M2(BETA2)/8.0 (Linux, build 987) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.003 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/432 X-Sequence-Number: 11137 > Can I use an index on a varchar column to optimize the SELECT queries > that > use " column LIKE 'header%' "? Yes > If yes what is the best tree algotithm to use ? Btree Note that if you want case insensitive matching you need to make an index on lower(column) and SELECT WHERE lower(column) LIKE 'header%' Locales may bite you. From tgl@sss.pgh.pa.us Tue Mar 22 18:08:27 2005 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 CDD6856A45 for ; Tue, 22 Mar 2005 18:08:27 +0000 (GMT) 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 18144-07 for ; Tue, 22 Mar 2005 18:08:19 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 011C156A0D for ; Tue, 22 Mar 2005 18:08:18 +0000 (GMT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j2MI8EDd010503; Tue, 22 Mar 2005 13:08:14 -0500 (EST) To: PFC Cc: "Layet Benjamin" , pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column In-reply-to: References: <007301c52ec0$a8f64ec0$9285a8c0@moonfactory.co.jp> Comments: In-reply-to PFC message dated "Tue, 22 Mar 2005 11:49:36 +0100" Date: Tue, 22 Mar 2005 13:08:14 -0500 Message-ID: <10502.1111514894@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.007 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/458 X-Sequence-Number: 11163 PFC writes: >> Can I use an index on a varchar column to optimize the SELECT queries >> that use " column LIKE 'header%' "? > Yes > Note that if you want case insensitive matching you need to make an index > on lower(column) and SELECT WHERE lower(column) LIKE 'header%' > Locales may bite you. Yes. If your database locale is not "C" then the default btree index behavior does not match up with what LIKE needs. In that case you need a special index using the appropriate "pattern_ops" opclass, eg CREATE INDEX test_index ON test_table (col varchar_pattern_ops); or if you want case insensitive matching CREATE INDEX test_index ON test_table (lower(col) varchar_pattern_ops); and then write the queries with lower() as PFC illustrates. *Don't* use ILIKE --- it basically can't use indexes at all. For more info see http://www.postgresql.org/docs/8.0/static/indexes-opclass.html regards, tom lane From mpuncia@census.gov.ph Wed Mar 23 09:16:31 2005 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 AEB4A5355B for ; Wed, 23 Mar 2005 09:16:31 +0000 (GMT) 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 96943-10 for ; Wed, 23 Mar 2005 09:16:18 +0000 (GMT) Received: from mail.census.gov.ph (mail.census.gov.ph [203.172.28.120]) by svr1.postgresql.org (Postfix) with ESMTP id B559A53551 for ; Wed, 23 Mar 2005 09:16:01 +0000 (GMT) Received: from notnot ([192.168.1.70]) by mail.census.gov.ph (8.13.1/8.13.1) with ESMTP id j2N42ax1022249 for ; Wed, 23 Mar 2005 12:02:36 +0800 Message-Id: <200503230402.j2N42ax1022249@mail.census.gov.ph> From: "Michael Ryan S. Puncia" To: Subject: Re: best practices with index on varchar column Date: Wed, 23 Mar 2005 12:11:56 +0800 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 In-Reply-To: Thread-Index: AcUuzPGCUiY9hn5hSmiI4tDc+4rTygAkQb9Q X-NSO-MailScanner: Found to be clean by NSO MailScanner. X-MailScanner-From: mpuncia@census.gov.ph X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.137 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/486 X-Sequence-Number: 11191 I have an experience using LIKE in a VARCHAR column and select statement suffers a lot so I decided to go back in CHAR Note: my database has about 50 millions records a b tree index > Can I use an index on a varchar column to optimize the SELECT queries > that > use " column LIKE 'header%' "? Yes > If yes what is the best tree algotithm to use ? Btree Note that if you want case insensitive matching you need to make an index on lower(column) and SELECT WHERE lower(column) LIKE 'header%' Locales may bite you. ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org __________ NOD32 1.1023 (20050310) Information __________ This message was checked by NOD32 Antivirus System. http://www.nod32.com From qnex42@gmail.com Wed Mar 23 09:35:56 2005 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 C267D5335E for ; Wed, 23 Mar 2005 09:35:56 +0000 (GMT) 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 02822-02 for ; Wed, 23 Mar 2005 09:35:47 +0000 (GMT) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.192]) by svr1.postgresql.org (Postfix) with ESMTP id 16ED153237 for ; Wed, 23 Mar 2005 09:35:46 +0000 (GMT) Received: by rproxy.gmail.com with SMTP id j1so116323rnf for ; Wed, 23 Mar 2005 01:35:48 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=FFt+mxq/yEp2bs9AjUyVvyZWAe57EglH+14d6E+2C3dfNbTawmiy4wvsCvlpOfmHzpPo9tafD4pWqCG0mokGdi3VmPN4JlS/FN79kbYQOkOUgWGc0BE2Dw0p3gQfA5UdiWnbBxnlZyXV2zDJzoQY2p4weg1QXPfrtAss1gwjl3o= Received: by 10.38.78.1 with SMTP id a1mr412868rnb; Wed, 23 Mar 2005 01:35:48 -0800 (PST) Received: by 10.38.101.75 with HTTP; Wed, 23 Mar 2005 01:35:48 -0800 (PST) Message-ID: <758d5e7f05032301351b685c44@mail.gmail.com> Date: Wed, 23 Mar 2005 10:35:48 +0100 From: Dawid Kuroczko Reply-To: Dawid Kuroczko To: pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column In-Reply-To: <200503230402.j2N42ax1022249@mail.census.gov.ph> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit References: <200503230402.j2N42ax1022249@mail.census.gov.ph> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.743 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, FROM_ENDS_IN_NUMS, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200503/487 X-Sequence-Number: 11192 On Wed, 23 Mar 2005 12:11:56 +0800, Michael Ryan S. Puncia wrote: > > I have an experience using LIKE in a VARCHAR column and select statement > suffers a lot so I decided to go back in CHAR > > Note: my database has about 50 millions records a b tree index Strange... According to the PostgreSQL's documentation: Tip: There are no performance differences between these three types, apart from the increased storage size when using the blank-padded type. While character(n) has performance advantages in some other database systems, it has no such advantages in PostgreSQL. In most situations text or character varying should be used instead. To my best knowledge char and varchar are stored in a same way (4-byte length plus textual value), so using char should make tables bigger in your case. Then again, having each row exactly the same size makes it easier to delete and then later insert a new row in a same spot. Am I thinking correct? Is it a case where using char(n) makes that table avoid hmm fragmentation of some sort? Regards, Dawid From dev@archonet.com Wed Mar 23 11:31:37 2005 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 236E6533BE for ; Wed, 23 Mar 2005 11:31:37 +0000 (GMT) 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 38824-05 for ; Wed, 23 Mar 2005 11:31:28 +0000 (GMT) Received: from anchor-post-31.mail.demon.net (anchor-post-31.mail.demon.net [194.217.242.89]) by svr1.postgresql.org (Postfix) with ESMTP id 5C218533B8 for ; Wed, 23 Mar 2005 11:31:27 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-31.mail.demon.net with esmtp (Exim 4.42) id 1DE42z-000DON-4R; Wed, 23 Mar 2005 11:29:13 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 9BFAF177C5; Wed, 23 Mar 2005 11:31:28 +0000 (GMT) Message-ID: <42415390.8030208@archonet.com> Date: Wed, 23 Mar 2005 11:31:28 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Dawid Kuroczko Cc: pgsql-performance@postgresql.org Subject: Re: best practices with index on varchar column References: <200503230402.j2N42ax1022249@mail.census.gov.ph> <758d5e7f05032301351b685c44@mail.gmail.com> In-Reply-To: <758d5e7f05032301351b685c44@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.061 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200503/490 X-Sequence-Number: 11195 Dawid Kuroczko wrote: > On Wed, 23 Mar 2005 12:11:56 +0800, Michael Ryan S. Puncia > wrote: > >>I have an experience using LIKE in a VARCHAR column and select statement >>suffers a lot so I decided to go back in CHAR > According to the PostgreSQL's documentation: > > Tip: There are no performance differences between these three types, > apart from the increased storage size when using the blank-padded type. > While character(n) has performance advantages in some other database > systems, it has no such advantages in PostgreSQL. In most situations text > or character varying should be used instead. > > > To my best knowledge char and varchar are stored in a same way > (4-byte length plus textual value), so using char should make tables > bigger in your case. Then again, having each row exactly the same > size makes it easier to delete and then later insert a new row in > a same spot. Am I thinking correct? Is it a case where using char(n) > makes that table avoid hmm fragmentation of some sort? There aren't any noticeable differences between char and varchar. MVCC doesn't overwrite rows anyway, so static size is irrelevant. In any case, PG's toast setup splits out large text fields and compresses them - so it's not that simple. -- Richard Huxton Archonet Ltd