postgresql - Select physically last record without ORDER BY -


an application inherited me oriented on "natural record flow" in postgresql table , there delphi code:

query.open('select * thetable'); query.last(); 

the task fields of last table record. decided rewrite query in more effective way, this:

select * thetable order reportdate desc limit 1 

but broke workflow. of reportdate records turned out null. application oriented on "natural" records order in table.

how physically last record selection without order by?

to physically last record selection, should use ctid - tuple id, last 1 - select max(ctid). smth like:

pond93=# select ctid,* t order ctid desc limit 1;   ctid  |               t                --------+-------------------------------  (5,50) | 2017-06-13 11:41:04.894666+00 (1 row) 

and without order by:

pond93=# select t t ctid = (select max(ctid) t);                t                -------------------------------  2017-06-13 11:41:04.894666+00 (1 row) 

its worth knowing can find ctid after sequential scan. checking latest physically row costy on large data sets


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -