Thread: Selecting the last 2 rows of a table

Selecting the last 2 rows of a table

From
Machiel Richards
Date:
Good day all

     I am trying to find out how I can select the last 2 or (n amount ) of rows from a specific table.

      We have a table which has been growing rediculously the last view days (I must say after a code go-live).

       I am trying to find out what data is being written to the table that is causing this, however due to the size of the table at the moment, it can cause problems so I am just interested in the last number of records.


      I would appreciate any assistance in this.


Regards
Machiel

Re: Selecting the last 2 rows of a table

From
Thom Brown
Date:
On 9 November 2010 09:34, Machiel Richards <machielr@rdc.co.za> wrote:
Good day all

     I am trying to find out how I can select the last 2 or (n amount ) of rows from a specific table.

      We have a table which has been growing rediculously the last view days (I must say after a code go-live).

       I am trying to find out what data is being written to the table that is causing this, however due to the size of the table at the moment, it can cause problems so I am just interested in the last number of records.


      I would appreciate any assistance in this.

Well, you'll have to decide what determines the order of your tuples.  If you have a column bound to a sequence, and that is considered to put them in order, then it would be

SELECT my_columns
FROM my_table
ORDER BY sequence_column DESC
LIMIT 2

Or you could replace that with a date field if appropriate.  But whatever column you use, you'd need an index on it.

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

Re: Selecting the last 2 rows of a table

From
Majid Azimi
Date:
On Tue, 2010-11-09 at 11:34 +0200, Machiel Richards wrote:
> Good day all
>
>      I am trying to find out how I can select the last 2 or (n
> amount ) of rows from a specific table.
>
>       We have a table which has been growing rediculously the last
> view days (I must say after a code go-live).
>
>        I am trying to find out what data is being written to the table
> that is causing this, however due to the size of the table at the
> moment, it can cause problems so I am just interested in the last
> number of records.
>
>
>       I would appreciate any assistance in this.
>
>
> Regards
> Machiel

Or if you don't want to use "order by" you can use this but it is
inefficient:

SELECT * FROM <Table name> OFFSET (SELECT count(*) - 2 FROM <table
name>);




Re: Selecting the last 2 rows of a table

From
Frank Bax
Date:
Machiel Richards wrote:
> Good day all
>
>      I am trying to find out how I can select the last 2 or (n amount )
> of rows from a specific table.
>
>       We have a table which has been growing rediculously the last view
> days (I must say after a code go-live).
>
>        I am trying to find out what data is being written to the table
> that is causing this, however due to the size of the table at the
> moment, it can cause problems so I am just interested in the last number
> of records.



Compare two backup files.