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

  • Home
  • SEARCH
  • 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 6233905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:23:12+00:00 2026-05-24T10:23:12+00:00

This problem is causing me a headache for a while. I’ve a PostgreSQL 8.4

  • 0

This problem is causing me a headache for a while. I’ve a PostgreSQL 8.4 database that consists of only one table containing more than 4.000.000 records. This table is structured as follows:

CREATE TABLE metadata (
  id serial NOT NULL,
  "value" text NOT NULL DEFAULT ''::text,
  segment_box box NOT NULL DEFAULT box(point(((-9223372036854775808)::bigint)::double precision, (1)::double precision), point((9223372036854775807::bigint)::double precision, ((-1))::double precision)),
  CONSTRAINT metadata_pk PRIMARY KEY (id)
)

CREATE INDEX metadata_segment_box_ix
  ON metadata
  USING gist
  (segment_box);

CREATE INDEX metadata_tag_value_ix
  ON metadata
  USING btree
  (value);

The table contains segments (in time), represented as rectangular boxes. These segments are annotated using the “value” column.

The kind of queries I would like to perform on the database try to find all segments with a specified value that is contained within a certain window. A query that successfully achieves this is:

SELECT * FROM (SELECT * FROM metadata WHERE value='X') a, 
(SELECT * FROM metadata WHERE AND value='Y') b 
WHERE a.segment_box <-> b.segment_box <= 3000

But, as you probably noticed, this query cannot be performed efficiently by the database. The cartesian product of sub-queries a and b is becoming really large. Is there a way to perform these queries more efficiently? I can imagine some sort of sliding window approach would do the trick. Maybe something like the following:

SELECT *, rank() OVER (
PARTITION BY "value" ORDER BY (segment_box[1])[0], (segment_box[0])[0]
) FROM metadata WHERE value='X' OR value='Y'

Update:
One of the things I tried after posting this question is creating a custom function in Postgres. I tried:

CREATE OR REPLACE FUNCTION within_window(size bigint DEFAULT 0)
  RETURNS setof metadata AS
$BODY$DECLARE
  segment RECORD;
  neighbour RECORD;
  newwindow box;
BEGIN
  FOR segment IN (
    SELECT * FROM metadata WHERE value='X' OR value='Y' 
      ORDER BY (segment_box[1])[0], (segment_box[0])[0]
  ) LOOP
    newwindow := box(segment.segment_box[0], 
      point((((segment.segment_box[1])[0]) + size), (segment.segment_box[1])[1]));
    FOR neighbour IN (
      SELECT DISTINCT ON (metadata_id) * FROM metadata WHERE value='X' OR value='Y') 
        AND segment_box &< newwindow
        AND segment_box &> newwindow 
    ) LOOP
      RETURN NEXT neighbour;
    END LOOP;
  END LOOP;
END;$BODY$
  LANGUAGE plpgsql;

However, this function is as slow as the basic solution I described above because of the subquery that must be performed many times. Any other thoughts on this??

  • 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-24T10:23:13+00:00Added an answer on May 24, 2026 at 10:23 am

    I solved the problem myself with a kind of sweep line algorithm. Only one query is performed. I use a cursor to sweep back and forth over the query’s resultset. The resulting algorithm works as follows:

    CREATE OR REPLACE FUNCTION within_window(size bigint DEFAULT 0)
      RETURNS setof metadata AS
    $BODY$DECLARE 
    crsr SCROLL CURSOR FOR (SELECT * FROM metadata WHERE value='X' OR value='Y' ORDER BY (segment_box[1])[0], (segment_box[0])[0]);
    rc RECORD;
    rcc RECORD;
    crsr_position int;
    last_crsr int;
    BEGIN
        OPEN crsr;
        crsr_position := 0;
        LOOP FETCH NEXT FROM crsr INTO rc;
            IF NOT FOUND THEN
                EXIT;
            END IF;
            last_crsr := crsr_position;
            LOOP FETCH NEXT FROM crsr INTO rcc;
                IF NOT FOUND THEN
                    EXIT;
                ELSEIF 
                    rcc.segment_box &< box(rc.segment_box[0], point((((rc.segment_box[1])[0]) + size), (rc.segment_box[1])[1])) AND
                    rcc.segment_box &> box(rc.segment_box[0], point((((rc.segment_box[1])[0]) + size), (rc.segment_box[1])[1]))
                THEN
                    RETURN NEXT rcc;
                ELSE 
                    EXIT;
                END IF;
            END LOOP;
            crsr_position := last_crsr + 1;
            MOVE ABSOLUTE crsr_position FROM crsr;
        END LOOP;
        CLOSE crsr;
    END;$BODY$
      LANGUAGE plpgsql;
    

    Using this function the query only needs 476 ms instead of 6+ minutes (on the 4+ million row database)!

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

Sidebar

Related Questions

One thing that constantly causing me headache in programming is when I don't have
Ok, I'm stumped by this one I have no idea what's causing this problem.
http://support.microsoft.com/kb/953102 We spent about 50 hours during Christmas holiday to identify this problem, causing
I have a problem with IE causing two errors: 1. Object doesn't support this
What could be causing this problem? public ActionResult Index(int page = 0) { const
Hey, I've been having this problem for a while now and I was hoping
Texturing headache... I can't understand where i go wrong here. My problem is that
I have this small function that is causing me headaches on RHEL6, and I
What is causing this problem shown below with firefox? The first button is a
I have a WCF service that is causing a bit of a headache. I

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.