sql - Fastest way to select row with max value at primary key field -



sql - Fastest way to select row with max value at primary key field -

i have got table:

create table spots( datetime timestamp, market varchar(15), spot numeric(10, 5), primary key (market, datetime) );

i need select row p_market maximum value of datetime filed, less or equal p_datetime, have got 2 opts this:

select * spots market = p_market , datetime = ( select max(datetime) spots market = p_market , datetime <= p_datetime );

and

select * spots market = p_market , datetime <= p_datetime order datetime descending limit 1;

so, question - variant improve performance perspective.

postgres supports window function, should theoretically calculated incrementally rows processed, should provide performance boost on big info sets:

select * (select *, rank() on (order datetime desc) rk spots market = p_market , datetime <= p_datetime) t spots rk = 1

sql performance postgresql greatest-n-per-group

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -