Re: How to get many data at once?

From: "Dmitry Koterov" <dmitry(at)koterov(dot)ru>
To: 窦德厚(ddh) <doudehou(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org, "Chandra ASGI Tech" <chandraasgi(at)gmail(dot)com>
Subject: Re: How to get many data at once?
Date: 2008-08-13 10:34:17
Message-ID: d7df81620808130334o400e50f0k82ce8b4f221d1c22@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Try to use

SELECT ARRAY(SELECT t_data FROM THETABLE WHERE t_ref_id = '1') AS v;

In PHP you may fetch all matched values as a single string and then - use
explode() to split it into values (possibly with later stripslashes).
It is much faster than fetching a thousands of rows.

On Thu, Aug 7, 2008 at 3:03 PM, 窦德厚(ddh) <doudehou(at)gmail(dot)com> wrote:

> Hi, if I have such a table:
>
> t_ref_id t_data
> --------------------
> 1 'abc'
> 2 '321'
> 1 'ddd'
> 2 'xyz'
> 9 '777'
> ...
>
>
> I want to get data with a special t_ref_id:
>
> SELECT t_data FROM THETABLE WHERE t_ref_id = '1';
>
> I must use a while loop to extract the data (I'm using PHP):
>
> $rows = array();
> while (($row = pgsql_fetch_assoc($result) !== false) {
> $rows[] = $row;
> }
>
> And if there are many matched rows, such as many hundreds or thousands of
> rows, I think such a loop maybe inefficient.
>
> How to do this in a more efficient way?
>
> Thank you!
>
>
>
> --
> ddh
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Raymond O'Donnell 2008-08-13 11:16:20 Re: psql tutorial
Previous Message Raymond O'Donnell 2008-08-13 09:41:31 Re: Is the primary key constraint also an index?