Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:22:13+00:00 2026-05-26T21:22:13+00:00

My problem is represented by the following query: SELECT b.row_id, b.x, b.y, b.something, (SELECT

  • 0

My problem is represented by the following query:

SELECT 
  b.row_id, b.x, b.y, b.something,
  (SELECT a.x FROM my_table a WHERE a.row_id = (b.row_id - 1), a.something != 42 ) AS source_x,
  (SELECT a.y FROM my_table a WHERE a.row_id = (b.row_id - 1), a.something != 42 ) AS source_y
FROM 
  my_table b

I’m using the same subquery statement twice, for getting both source_x and source_y.
That’s why I’m wondering if it’s possible to do it using one subquery only?

Because once I run this query on my real data (millions of rows) it seems to never finish and take hours, if not days (my connection hang up before the end).

I am using PostgreSQL 8.4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:22:14+00:00Added an answer on May 26, 2026 at 9:22 pm

    @DavidEG posted the best syntax for the query.

    However, your problem is definitely not just with the query technique. A JOIN instead of two subqueries can speed up things by a factor of two at best. Most likely less. That doesn’t explain “hours”. Even with millions of rows, a decently set up Postgres should finish the simple query in seconds, not hours.

    • First thing that stands out is the syntax error in your query:

      ... WHERE a.row_id = (b.row_id - 1), a.something != 42
      

      AND or OR is needed here, not a comma.

    • Next thing to check are indexes. If row_id is not the primary key, you may not have an index on it. For optimum performance of this particular query create a multi-column index on (row_id, something) like this:

      CREATE INDEX my_table_row_id_something_idx ON my_table (row_id, something)
      
    • If the filter excludes the same value every time in something != 42you can also use a partial index instead for additional speed up:

      CREATE INDEX my_table_row_id_something_idx ON my_table (row_id)
      WHERE something != 42
      

      This will only make a substantial difference if 42 is a common value or something is a bigger column than just an integer. (An index with two integer columns normally occupies the the same size on disk as an index with just one, due to data alignment. See:

      • Calculating and saving space in PostgreSQL
      • Is a composite index also good for queries on the first field?
    • When performance is an issue, it is always a good idea to check your settings. Standard settings in Postgres use minimal resources in many distributions and are not up to handling “millions of rows”.

    • Depending on your actual version of Postgres, an upgrade to a current version (9.1 at the time of writing) may help a lot.

    • Ultimately, hardware is always a factor, too. Tuning and optimizing can only get you so far.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is the following: given two bipartite directed graphs represented in .dot files,
I received the following query from a customer: I am doing some research into
Problem: I have an address field from an Access database which has been converted
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem is described and demonstrated on the following links: Paul Stovell WPF: Blurry Text
I have the following Django Model which retrieves 3 records from a database. The
I have two tables: t_promo_program and t_promo_program_param. They are represented by the following JPA
The following diagram illustrates a problem I have encountered creating a Manhattan diagram: Overlapping
I was goint through k & r. I was having problem in understanding following
I have following problem. I worked two days on a solution but I cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.