pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL question
9+ messages / 9 participants
[nested] [flat]

* SQL question
@ 2002-01-26 01:27  Bolt Thrower <tyr@teiresias.net>
  0 siblings, 2 replies; 9+ messages in thread

From: Bolt Thrower @ 2002-01-26 01:27 UTC (permalink / raw)
  To: pgsql-admin

In the query:

	select field, length(field) from table where length(field) < 10;

How many times is the length() function called for each row?  Once or
twice?

Thanks,
-- 
Steve Chadsey <tyr@teiresias.net>



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* SQL question
@ 2002-01-28 20:26  Bolt Thrower <tyr@winter.teiresias.net>
  0 siblings, 0 replies; 9+ messages in thread

From: Bolt Thrower @ 2002-01-28 20:26 UTC (permalink / raw)
  To: pgsql-admin

In the query:

        select field, length(field) from table where length(field) < 10;

How many times is the length() function called for each row?
Once or twice?

Thanks,
-- 
Steve Chadsey <tyr@teiresias.net>



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL question
@ 2002-02-01 19:38  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Bolt Thrower <tyr@teiresias.net>
  1 sibling, 0 replies; 9+ messages in thread

From: Stephan Szabo @ 2002-02-01 19:38 UTC (permalink / raw)
  To: Bolt Thrower <tyr@teiresias.net>; +Cc: pgsql-admin


On Fri, 25 Jan 2002, Bolt Thrower wrote:

> In the query:
>
> 	select field, length(field) from table where length(field) < 10;
>
> How many times is the length() function called for each row?  Once or
> twice?

Pretty sure it'll be twice.




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL question
@ 2002-02-01 23:56  Brett W. McCoy <bmccoy@chapelperilous.net>
  parent: Bolt Thrower <tyr@teiresias.net>
  1 sibling, 1 reply; 9+ messages in thread

From: Brett W. McCoy @ 2002-02-01 23:56 UTC (permalink / raw)
  To: Bolt Thrower <tyr@teiresias.net>; +Cc: pgsql-admin

On Fri, 25 Jan 2002, Bolt Thrower wrote:

> In the query:
>
> 	select field, length(field) from table where length(field) < 10;
>
> How many times is the length() function called for each row?  Once or
> twice?

I would expect it to run both times.

I did a similar query with an explain:

 explain verbose select behavior_descr, length(behavior_descr) from
behaviors where length(behavior_descr) < 20;
NOTICE:  QUERY DUMP:

{ SEQSCAN :startup_cost 0.00 :total_cost 1.04 :rows 1 :width 22
:qptargetlist ({ TARGETENTRY :resdom { RESDOM :resno 1 :restype 1043
:restypmod 34 :resname behavior_descr :reskey 0 :reskeyop 0
:ressortgroupref 0 :resjunk false } :expr { VAR :varno 1 :varattno 2
:vartype 1043 :vartypmod 34  :varlevelsup 0 :varnoold 1 :varoattno 2}} {
TARGETENTRY :resdom { RESDOM :resno 2 :restype 23 :restypmod -1 :resname
length :reskey 0 :reskeyop 0 :ressortgroupref 0 :resjunk false } :expr {
EXPR :typeOid 23  :opType func :oper { FUNC :funcid 1319 :functype 23 }
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:args ({ VAR :varno 1 :varattno 2 :vartype 1043 :vartypmod 34
:varlevelsup 0 :varnoold 1 :varoattno 2})}}) :qpqual ({ EXPR :typeOid 16
:opType op :oper { OPER :opno 97 :opid 66 :opresulttype 16 } :args ({ EXPR
:typeOid 23  :opType func :oper { FUNC :funcid 1319 :functype 23 } :args
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
({ VAR :varno 1 :varattno 2 :vartype 1043 :vartypmod 34  :varlevelsup 0
:varnoold 1 :varoattno 2})} { CONST :consttype 23 :constlen 4 :constbyval
true :constisnull false :constvalue  4 [ 20 0 0 0 ] })}) :lefttree <>
:righttree <> :extprm () :locprm () :initplan <> :nprm 0  :scanrelid 1 }
NOTICE:  QUERY PLAN:

It looks like length() is getting run twice here.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
And I alone am returned to wag the tail.





^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL question
@ 2002-02-02 00:28  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Brett W. McCoy <bmccoy@chapelperilous.net>
  0 siblings, 0 replies; 9+ messages in thread

From: Tom Lane @ 2002-02-02 00:28 UTC (permalink / raw)
  To: Brett W. McCoy <bmccoy@chapelperilous.net>; +Cc: Bolt Thrower <tyr@teiresias.net>; pgsql-admin

"Brett W. McCoy" <bmccoy@chapelperilous.net> writes:
>> select field, length(field) from table where length(field) < 10;
>> 
>> How many times is the length() function called for each row?  Once or
>> twice?

> I would expect it to run both times.

More specifically, the one in the select list will be evaluated at
every row where the WHERE clause succeeds.  So "twice per row" is
an overstatement, possibly a large overstatement.

Also, although PG is not smart about common subexpressions in the way
that Bolt is evidently hoping, it does know all about Boolean
short-circuiting.  If you have something like

select ... from table where foo = 42 and length(bar) < 10

then length(bar) will not get evaluated at rows where foo = 42 fails.

Note: this assumes that the planner does not choose to reorder the
WHERE clauses, which AFAIR it'd only do if it pulls out a WHERE clause
to use with an index.  For example, if there's an index on foo,
the clause foo = 42 will probably get pulled out and used for an
indexscan (thus, in effect, "evaluated first") no matter whether you
write it first or second.

An interesting point here is that in the presence of functional indexes,
"avoiding the function evaluation" is not necessarily what you want.
We could imagine having an index on length(bar), in which case
	 where length(bar) < 10
reduces to a scan over the part of the index that has entries less than
10.  I'm not sure this would be a win for length(), but for expensive
functions that have a wider variation in probable values than length()
does, it can be a huge win.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* sql question
@ 2003-03-18 18:59  Jodi Kanter <jkanter@virginia.edu>
  0 siblings, 1 reply; 9+ messages in thread

From: Jodi Kanter @ 2003-03-18 18:59 UTC (permalink / raw)
  To: pgsql-admin

Can someone tell me how to create a query that will list values in a field across columns instead of listing them in row form. Can this be done in one sql query?
Thanks
Jodi

_______________________________
Jodi L Kanter
BioInformatics Database Administrator
University of Virginia
(434) 924-2846
jkanter@virginia.edu



 

 

 

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: sql question
@ 2003-03-18 22:04  Joe Conway <mail@joeconway.com>
  parent: Jodi Kanter <jkanter@virginia.edu>
  0 siblings, 0 replies; 9+ messages in thread

From: Joe Conway @ 2003-03-18 22:04 UTC (permalink / raw)
  To: Jodi Kanter <jkanter@virginia.edu>; +Cc: pgsql-admin

Jodi Kanter wrote:
> Can someone tell me how to create a query that will list values in a
> field across columns instead of listing them in row form. Can this be
> done in one sql query? Thanks Jodi

Are you referring to a crosstab, i.e.:

select * from cth;
   id | rowid |        rowdt        |   attribute    |      val
  ----+-------+---------------------+----------------+---------------
    1 | test1 | 2003-03-01 00:00:00 | temperature    | 42
    2 | test1 | 2003-03-01 00:00:00 | test_result    | PASS
    3 | test1 | 2003-03-01 00:00:00 | volts          | 2.6987
    4 | test2 | 2003-03-02 00:00:00 | temperature    | 53
    5 | test2 | 2003-03-02 00:00:00 | test_result    | FAIL
    6 | test2 | 2003-03-02 00:00:00 | test_startdate | 01 March 2003
    7 | test2 | 2003-03-02 00:00:00 | volts          | 3.1234
(7 rows)

SELECT * FROM crosstab(
    'SELECT rowid, attribute, val FROM cth ORDER BY 1',
    'SELECT DISTINCT attribute FROM cth ORDER BY 1')
AS c(rowid text, temperature int4, test_result text, test_startdate
timestamp, volts float8);
   rowid | temperature | test_result |   test_startdate    | volts
  -------+-------------+-------------+---------------------+--------
   test1 |          42 | PASS        |                     | 2.6987
   test2 |          53 | FAIL        | 2003-03-01 00:00:00 | 3.1234
(2 rows)

There is an older version of the crosstab function above, in PostgreSQL 
7.3.x, in contrib/tablefunc. The one shown above is a significant 
improvement that should be in 7.4 when it's released, but it will work 
fine with 7.3.x. If you want a copy of the updated version, you can get 
it here:

   http://www.joeconway.com/

There are some pure SQL ways to do this also; search the list archives 
for the sql and general lists:

   http://archives.postgresql.org/pgsql-sql/
   http://archives.postgresql.org/pgsql-general/

HTH,

Joe




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* SQL question
@ 2003-03-20 15:18  Mario Alberto Soto Cordones <mario_soto@compuall.cl>
  0 siblings, 1 reply; 9+ messages in thread

From: Mario Alberto Soto Cordones @ 2003-03-20 15:18 UTC (permalink / raw)
  To: pgsql-admin

Hi,

i need inser a picture in a table but i can´t.
i work with visual basic 6.0 and postgres 7.3 but i can´t

Thanks

Mario






^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL question
@ 2003-03-21 09:30  er <reetz@usco.de>
  parent: Mario Alberto Soto Cordones <mario_soto@compuall.cl>
  0 siblings, 0 replies; 9+ messages in thread

From: er @ 2003-03-21 09:30 UTC (permalink / raw)
  To: Mario Alberto Soto Cordones <mario_soto@compuall.cl>; +Cc: pgsql-admin

http://gborg.postgresql.org/project/psqlodbc/genpage.php?howto-vblo

Mario Alberto Soto Cordones wrote:

>Hi,
>
>i need inser a picture in a table but i can´t.
>i work with visual basic 6.0 and postgres 7.3 but i can´t
>
>Thanks
>
>Mario
>
>
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 6: Have you searched our list archives?
>
>http://archives.postgresql.org
>
>
>  
>





^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2003-03-21 09:30 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-01-26 01:27 SQL question Bolt Thrower <tyr@teiresias.net>
2002-02-01 19:38 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>
2002-02-01 23:56 ` Brett W. McCoy <bmccoy@chapelperilous.net>
2002-02-02 00:28   ` Tom Lane <tgl@sss.pgh.pa.us>
2002-01-28 20:26 SQL question Bolt Thrower <tyr@winter.teiresias.net>
2003-03-18 18:59 sql question Jodi Kanter <jkanter@virginia.edu>
2003-03-18 22:04 ` Re: sql question Joe Conway <mail@joeconway.com>
2003-03-20 15:18 SQL question Mario Alberto Soto Cordones <mario_soto@compuall.cl>
2003-03-21 09:30 ` er <reetz@usco.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox