Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql General > Re: large query...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 5 Topic 15441 of 17598
Post > Topic >>

Re: large query by offset and limt

by steve@[EMAIL PROTECTED] (Steve Atkins) May 3, 2008 at 11:17 AM

On May 2, 2008, at 2:01 PM, finecur wrote:

> Hi, I am ruuning a database behind a webserver and there is a table
> which is huge. I need to pull data from this table and send to user
> through http. If I use
>
> select * from huge_table where userid = 100
>
> It will return millions of records which exhuasts my server's memory.
> So I do this:
>
> select * from huge_table where userid = 100 limit 1000 offset 0
> and then send the results to user, then
>
> select * from huge_table where userid = 100 limit 1000 offset 1000
> and then send the results to user, then
>
> select * from huge_table where userid = 100 limit 1000 offset 2000
> and then send the results to user,
>
> Continue this until there is no records available
>
> It runs great but it is kind of slow. I think it is because even I
> need only 1000 records, the query search the whole table every time.

Not quite - if you do a "limit 1000 offset 5000" it'll stop after  
retrieving
the first 6000 from the table. A bigger problem with doing it this
way is that the results aren't particularly well defined unless there's
an order by statement in the query.

>
>
> Is there a better way to do this?

You want a cursor. See
http://www.postgresql.org/docs/8.3/static/sql-declare.html

Cheers,
   Steve


-- 
Sent via pgsql-general mailing list (pgsql-general@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
 




 5 Posts in Topic:
large query by offset and limt
finecur <finecur@[EMAI  2008-05-02 14:01:43 
Re: large query by offset and limt
steve@[EMAIL PROTECTED]   2008-05-03 11:17:00 
Re: large query by offset and limt
craig@[EMAIL PROTECTED]   2008-05-04 02:34:38 
Re: large query by offset and limt
Ge Cong <gecong@[EMAI  2008-05-03 13:53:36 
Re: large query by offset and limt
craig@[EMAIL PROTECTED]   2008-05-04 08:56:54 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Dec 1 8:30:47 CST 2008.