0

I’ve implemented a solution which takes into consideration this post where new records get aged.

When I query the records, I want to know if a previous record has been sent out for the same key.

Below is an example table & data:

CREATE TABLE IF NOT EXISTS wages 
(
    PRIMARY KEY (key, age),
    key UUID NOT NULL,
    age INT NOT NULL,
    sent TIMESTAMP,
    balanceinpence INT
);

INSERT INTO wages (key, age, sent, balanceinpence) 
VALUES (‘49a362a1-6bc1-4cbe-b0b8-f23b292c623d’, 0, null, 6472);
INSERT INTO wages (key, age, sent, balanceinpence) 
VALUES (‘49a362a1-6bc1-4cbe-b0b8-f23b292c623d’, 1, ‘2022-02-27 15:02:10.000000’, 7405);
INSERT INTO wages (key, age, sent, balanceinpence) 
VALUES (‘49a362a1-6bc1-4cbe-b0b8-f23b292c623d’, 2, ‘2022-02-26 15:02:10.000000’, 3490);
INSERT INTO wages (key, age, sent, balanceinpence) 
VALUES (‘137d1a38-3555-4daa-8b68-ca416b3069b5’, 0, null, 1473);
INSERT INTO wages (key, age, sent, balanceinpence) 
VALUES (‘019c04a7-b2e5-4dd9-9b49-e52539937ebb’, 0, null, 2671);

Taking the first key into account (49a362a1-6bc1-4cbe-b0b8-f23b292c623d), I want to get the balanceinpence of the record with an age of 0 and sent as the date of any of the previously sent records, i.e. I want to see:

49a362a1-6bc1-4cbe-b0b8-f23b292c623d    2022-02-26 15:02:10.000000  0   6472

I have tried using a CTE but the performance is poor as it is querying the whole database (10+ million records) before getting my subset. An example query I have run.

WITH results AS 
(
    SELECT 
        key,
        FIRST_VALUE(sent) OVER (ORDER BY CASE WHEN sent IS NOT NULL THEN sent END) AS sent,
        age,
        balanceinpence
    FROM 
        wages
)
SELECT *
FROM results
WHERE key = ‘49a362a1-6bc1-4cbe-b0b8-f23b292c623d’
  AND age = 0;

What would the most performant way of getting this information be?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    I removed the conflicting DBMS tags. Please add only one tag for the database product you are really using. –  Feb 28 '22 at 15:59
  • There is no more efficient way to write that, except that the window can be reduced to `(ORDER BY sent)`. – Laurenz Albe Feb 28 '22 at 17:01

0 Answers0