Re: Queue in SQL
От | Christopher Kings-Lynne |
---|---|
Тема | Re: Queue in SQL |
Дата | |
Msg-id | GNELIHDDFBOCMGBFGEFOIEJICAAA.chriskl@familyhealth.com.au обсуждение исходный текст |
Ответ на | Queue in SQL ("Gyorgy Molnar" <gyorgy.molnar@home.com>) |
Список | pgsql-sql |
Hi Gyorgy, Try this: BEGIN; SELECT * FROM table ORDER BY oid LIMIT 1 FOR UPDATE; DELETE FROM table WHERE oid=(SELECT MIN(oid) FROM table); COMMIT; Few notes: 1. You might want to add an index over the oid column: CREATE INDEX "my_idx" ON table(oid); 2. If you are executing this series from a programming language, you can probably just change the first SELECT to "SELECT oid, * FROM ..." and just grab out the oid and pass it is a parameter to the DELETE, rather than having to do the aggregate subselect. Chris > -----Original Message----- > From: pgsql-sql-owner@postgresql.org > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Gyorgy Molnar > Sent: Tuesday, 27 November 2001 10:11 PM > To: pgsql-sql@postgresql.org > Subject: [SQL] Queue in SQL > > > Hi! > > Question: > I need to store some incoming data and retrieve them one by one (LIFO). > Different processes will manage the storage and the retrieval. > How can I retrieve only the first row from a table? > ================== > > Explanation > I intend to use libpq and C. as far as I know to retrieve the > result from a > query I need to do the following: > > BEGIN A TRANSACTION > DECLARE CURSOR cursor FOR SELECT > FETCH one row from cursor > Process > CLOSE cursor > DELETE row > COMMIT > > Let say I have 100,000 records in my table, I have to pick up > only the first > row. I do not have any special criteria to execute a query, and I > only need > the oldest added row (let say the first row if the table is indexed). > How can I narrow the search criteria, not to receive all of the 100,000 > record in the result? > Unfortunately, I do not know the internal working of the SELECT. > I think it > should create a temporary object to store the result. This object size may > depend on the number of the rows in the result and the size of the stored > data per row. I can fetch the rows one by one using this temporary object. > > Kind Regards, > Gyorgy Molnar > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/users-lounge/docs/faq.html >
В списке pgsql-sql по дате отправления: