Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 66D931337BB4 for ; Wed, 23 Mar 2011 09:50:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 99933-04 for ; Wed, 23 Mar 2011 12:50:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.cisx.com (failover.mail.cisx.com [78.41.6.83]) by mail.postgresql.org (Postfix) with ESMTP id 32C061337B69 for ; Wed, 23 Mar 2011 09:50:26 -0300 (ADT) Received: from DEVELOP1 ([192.168.100.50]) by mail.cisx.com (8.13.1/8.13.1) with SMTP id p2NCoJVv011473 for ; Wed, 23 Mar 2011 12:50:25 GMT Message-ID: <7C3EA8B4867547AC899AD3EC43DEA6AA@DEVELOP1> From: "Donald Fraser" To: "[BUGS]" Subject: Index Ignored Due To Use Of View Date: Wed, 23 Mar 2011 12:50:19 -0000 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0023_01CBE958.DD7A8860" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5843 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201103/292 X-Sequence-Number: 29802 This is a multi-part message in MIME format. ------=_NextPart_000_0023_01CBE958.DD7A8860 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable PostgreSQL 8.3.14 OS: Linux Redhat 5.4 Note: I have used the same subject for this email taken from an email: = Posted 2011-02-24 13:29:22-08 by "David Johnston", because this seems to = be a very similar observation. Bug/Problem Summary: We are using a simple query based on a simple view and the query = optimizer is not choosing an index.=20 The same query without the view is using an index. The same query on an almost identical view, but having either removed a = single column which was generated via a function call or replace the = function call with equivalent SQL, then the query optimizer is choosing = an index. Other notes: We observe the same behaviour on two separate databases running on = separate servers (both the same version). The genetic query optimizer settings are all on defaults for these = versions. Details; With the following view: CREATE OR REPLACE VIEW vu_tbl_news_web AS=20 SELECT=20 n.id, n.id_cmpy, n.id_news, n.id_newshdline,=20 n.s_origcmpyname, n.s_hdline, n.s_news,=20 n.b_amend, n.b_replace,=20 n.dt_publish,=20 n.tsv_hdline, n.tsv_news,=20 n.b_hasorigdoc,=20 (SELECT h.s_hdline FROM tbl_newshdline h WHERE h.id =3D n.id_newshdline = LIMIT 1) AS s_hdlinetype,=20 get_cmpyname(n.id_contrib) AS s_provider FROM tbl_news n WHERE n.dt_publish IS NOT NULL; Query: SELECT n.id, n.id_cmpy, n.s_origcmpyname,=20 n.s_hdline, n.dt_publish=20 FROM vu_tbl_news_web n=20 ORDER BY n.dt_publish DESC=20 LIMIT 25 Analyse produces: Limit (cost=3D180017.37..180017.43 rows=3D25 width=3D80) -> Sort (cost=3D180017.37..180110.54 rows=3D37267 width=3D80) Sort Key: n.dt_publish -> Subquery Scan n (cost=3D0.00..178965.72 rows=3D37267 = width=3D80) -> Seq Scan on tbl_news n (cost=3D0.00..178593.05 = rows=3D37267 width=3D1152) Filter: (dt_publish IS NOT NULL) SubPlan -> Limit (cost=3D0.00..4.29 rows=3D1 width=3D22) -> Seq Scan on tbl_newshdline h = (cost=3D0.00..4.29 rows=3D1 width=3D22) Filter: (id =3D $0) Although the number of rows (37k) is small, there is a lot of data in = some of the columns so, with a seq. scan its taking nearly 2 seconds. = Compared to 16ms when using an index. Now if I perform the same query without using the view, such as: SELECT n.id, n.id_cmpy, n.s_origcmpyname,=20 n.s_hdline, n.dt_publish=20 FROM tbl_news n=20 WHERE n.dt_publish IS NOT NULL=20 ORDER BY n.dt_publish DESC=20 LIMIT 25 Analyse produces: Limit (cost=3D0.00..21.34 rows=3D25 width=3D73) -> Index Scan Backward using tbl_news_publish1_key on tbl_news n = (cost=3D0.00..31807.05 rows=3D37267 width=3D73) Finally I discovered that if I remove a column from the view, that is = generated via a function, then all works as expected? CREATE OR REPLACE VIEW vu_tbl_news_web3 AS=20 SELECT=20 n.id, n.id_cmpy, n.id_news, n.id_newshdline,=20 n.s_origcmpyname, n.s_hdline, n.s_news,=20 n.b_amend, n.b_replace,=20 n.dt_publish, n.tsv_hdline, n.tsv_news,=20 n.b_hasorigdoc, (SELECT h.s_hdline FROM tbl_newshdline h WHERE h.id =3D n.id_newshdline = LIMIT 1) AS s_hdlinetype FROM tbl_news n WHERE n.dt_publish IS NOT NULL; SELECT n.id, n.id_cmpy, n.s_origcmpyname,=20 n.s_hdline, n.dt_publish=20 FROM vu_tbl_news_web3 n=20 ORDER BY n.dt_publish DESC=20 LIMIT 25 Analyse produces: Limit (cost=3D0.00..21.34 rows=3D25 width=3D73) -> Index Scan Backward using tbl_news_publish1_key on tbl_news n = (cost=3D0.00..31807.05 rows=3D37267 width=3D73) The definition of the function used is: CREATE OR REPLACE FUNCTION get_cmpyname(integer) RETURNS citext AS $BODY$ DECLARE idcmpy ALIAS FOR $1; sumb citext; sres citext; BEGIN SELECT INTO sumb, sres s_umbname, s_name FROM tbl_cmpy WHERE (id =3D = idcmpy) LIMIT 1; IF length(sumb) > 0 THEN sres :=3D sumb || ' - ' || sres; END IF; RETURN sres; END $BODY$ LANGUAGE 'plpgsql' VOLATILE STRICT SECURITY DEFINER COST 100; I also noted that if I replace the function call in the view with = equivalent SQL: (SELECT (CASE WHEN length(c.s_umbname) > 0 THEN c.s_umbname || ' - = '::citext || c.s_name ELSE c.s_name END) AS s_cmpyname FROM tbl_cmpy c = WHERE c.id =3D n.id_contrib LIMIT 1) AS s_provider the problem also goes away. Regards Donald Fraser ------=_NextPart_000_0023_01CBE958.DD7A8860 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
PostgreSQL 8.3.14
OS: Linux Redhat 5.4
 
Note: I have used the same subject for this email = taken from=20 an email: Posted 2011-02-24 13:29:22-08 by "David Johnston", because = this=20 seems to be a very similar observation.
 
Bug/Problem Summary:
We are using a simple query based on a simple view = and the=20 query optimizer is not choosing an index.
The same query without the view is using an=20 index.
The same query on an almost identical view, but = having either=20 removed a single column which was generated via a function call or = replace the=20 function call with equivalent SQL, then the query=20 optimizer is choosing an index.
 
Other notes:
We observe the same behaviour on two separate = databases=20 running on separate servers (both the same version).
The genetic query optimizer settings are all on = defaults for=20 these versions.
 
Details;
With the following view:
 
CREATE OR REPLACE VIEW vu_tbl_news_web AS
SELECT =
 n.id, n.id_cmpy, n.id_news, n.id_newshdline,=20
 n.s_origcmpyname, n.s_hdline, n.s_news,
 n.b_amend,=20 n.b_replace,
 n.dt_publish,
 n.tsv_hdline, n.tsv_news, =
 n.b_hasorigdoc,
 (SELECT h.s_hdline FROM = tbl_newshdline h=20 WHERE h.id =3D n.id_newshdline LIMIT 1) AS s_hdlinetype,=20
 get_cmpyname(n.id_contrib) AS s_provider
FROM =  tbl_news=20 n
WHERE  n.dt_publish IS NOT NULL;
Query:
SELECT  n.id, n.id_cmpy, n.s_origcmpyname,=20
 n.s_hdline, n.dt_publish
FROM  vu_tbl_news_web n =
ORDER BY=20 n.dt_publish DESC
LIMIT 25
Analyse produces:
Limit  (cost=3D180017.37..180017.43 rows=3D25=20 width=3D80)
  ->  Sort  = (cost=3D180017.37..180110.54 rows=3D37267=20 width=3D80)
        Sort Key:=20 n.dt_publish
        ->  = Subquery=20 Scan n  (cost=3D0.00..178965.72 rows=3D37267=20 width=3D80)
         &nbs= p;   =20 ->  Seq Scan on tbl_news n  (cost=3D0.00..178593.05 = rows=3D37267=20 width=3D1152)
         &n= bsp;         =20 Filter: (dt_publish IS NOT=20 NULL)
          &nbs= p;        =20 SubPlan
          &n= bsp;          =20 ->  Limit  (cost=3D0.00..4.29 rows=3D1=20 width=3D22)
         &nbs= p;            = ;     =20 ->  Seq Scan on tbl_newshdline h  (cost=3D0.00..4.29 = rows=3D1=20 width=3D22)
         &nbs= p;            = ;           =20 Filter: (id =3D $0)
Although the number of rows (37k) is small, there is = a lot of=20 data in some of the columns so, with a seq. scan its taking = nearly 2=20 seconds. Compared to 16ms when using an index.

Now if I perform the same query without using the view, such = as:
 
SELECT  n.id, n.id_cmpy, n.s_origcmpyname,=20
 n.s_hdline, n.dt_publish
FROM  tbl_news n
WHERE=20  n.dt_publish IS NOT NULL
ORDER BY n.dt_publish DESC
LIMIT=20 25
 
Analyse produces:
Limit  (cost=3D0.00..21.34 rows=3D25 = width=3D73)
 =20 ->  Index Scan Backward using tbl_news_publish1_key on tbl_news = n =20 (cost=3D0.00..31807.05 rows=3D37267 width=3D73)
Finally I discovered that if I remove a column = from the=20 view, that is generated via a function, then all works as = expected?
 
CREATE OR REPLACE VIEW vu_tbl_news_web3 AS =
SELECT=20
 n.id, n.id_cmpy, n.id_news, n.id_newshdline,=20
 n.s_origcmpyname, n.s_hdline, n.s_news,
 n.b_amend,=20 n.b_replace,
 n.dt_publish, n.tsv_hdline, n.tsv_news,=20
 n.b_hasorigdoc,
 (SELECT h.s_hdline FROM = tbl_newshdline h=20 WHERE h.id =3D n.id_newshdline LIMIT 1) AS s_hdlinetype
FROM =  tbl_news=20 n
WHERE  n.dt_publish IS NOT NULL;
SELECT  n.id, n.id_cmpy, n.s_origcmpyname,=20
 n.s_hdline, n.dt_publish
FROM  vu_tbl_news_web3 n =
ORDER=20 BY n.dt_publish DESC
LIMIT 25
Analyse produces:
Limit  = (cost=3D0.00..21.34=20 rows=3D25 width=3D73)
  ->  Index Scan Backward using=20 tbl_news_publish1_key on tbl_news n  (cost=3D0.00..31807.05 = rows=3D37267=20 width=3D73)
The definition of the function used is:
CREATE OR REPLACE FUNCTION get_cmpyname(integer)  RETURNS = citext=20 AS
$BODY$
DECLARE
 idcmpy ALIAS FOR $1;
 sumb=20 citext;
 sres citext;
BEGIN
 SELECT INTO sumb, sres=20 s_umbname, s_name FROM tbl_cmpy WHERE (id =3D idcmpy) LIMIT = 1;
 IF=20 length(sumb) > 0 THEN
  sres :=3D  sumb || ' - ' || = sres;
 END IF;
 RETURN sres;
END $BODY$
LANGUAGE = 'plpgsql'=20 VOLATILE STRICT SECURITY DEFINER COST 100;
I also noted that if I replace the function call in the view with=20 equivalent SQL:
(SELECT (CASE WHEN length(c.s_umbname) > 0 THEN c.s_umbname || ' = -=20 '::citext || c.s_name ELSE c.s_name END) AS s_cmpyname FROM tbl_cmpy c = WHERE=20 c.id =3D n.id_contrib LIMIT 1) AS s_provider
the problem also goes away.
 
Regards
Donald Fraser
------=_NextPart_000_0023_01CBE958.DD7A8860--