1

I need some guidance with this query being too slow.

SELECT DISTINCT ON (id)
 id,
 views - lead(views)
 OVER (PARTITION BY id
 ORDER BY update_date DESC) vdiff
FROM videoupdate;

With 10 million+ rows it takes ~30 seconds. I have created a multicolumn index that reduced the original time from 1 minute. I want to see difference between views for each row partitioned by id. Some thoughts I had:

  • After table update create TABLE AS with the query and select from it.
  • Move old data to backup and shrink table.
  • Look up data warehouse?
  • Change database schema?
Evan Carroll
65.7k50 gold badges259 silver badges510 bronze badges
asked Sep 9, 2017 at 13:47
6
  • 1
    I'm not sure what that returns because it's undefined. You're using DISTINCT ON without and ORDER BY? I'm assuming it's ordered by update _date DESC Commented Sep 9, 2017 at 14:17
  • I figured I didn't need to add ORDER BY when I have it in the window function. It should be ordered by update_date, yes. I believe I tested this. Nonetheless, the query still takes too long. @EvanCarroll Commented Sep 9, 2017 at 14:33
  • 1
    You could try to remove the distinct on and use a row_number() over the same window as the lead() function and use that row number to get the distinct ID. Commented Sep 9, 2017 at 16:22
  • @a_horse_with_no_name I don't think that will work, because he needs the lead() and WHERE runs before the window function. So the distinct on is still the best bet. Commented Sep 9, 2017 at 17:03
  • No I mean something like this: privatebin.net/… Commented Sep 9, 2017 at 22:02

1 Answer 1

0

Following @a_horse_with_no_name's suggestion again, because he's really smart though super, super-resilient to using the Post Your Answer functionality.

SELECT DISTINCT ON(id),
 id,
 views - lead(views) OVER (PARTITION BY id ORDER BY update_date DESC) AS vdiff
FROM (
 SELECT id,
 views,
 update_desc,
 row_number() OVER (PARTITION BY id ORDER BY update_date DESC) AS rn
 FROM videoupdate
) AS t
WHERE rn <=2
ORDER BY id, update_desc DESC;
answered Sep 9, 2017 at 16:46
3
  • Thank you very much for your answer! I will try it out as soon as possible. @evancarrol Commented Sep 9, 2017 at 17:02
  • @Misa not yet, it's not right. Commented Sep 9, 2017 at 17:02
  • @Misa try that. =) Commented Sep 9, 2017 at 17:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.