Ty Busby <tybu...@gmail.com> writes: > I have a table that stores a very large starting number called > epc_start_numeric and a quantity. I've apparently built the most inefficient > query possible for doing the job I need: find out if any records overlap. > Imagine the epc_start_numeric + quantity representing a block of numbers. I > need to find out if any of these blocks overlap.
Yeah, overlap is a hard problem. Basically, Postgres doesn't have any way to do your query short of comparing each row to each other row, so the cost goes up as O(N^2). If you know more than you've let on about the properties of the intervals, you might be able to improve things. For instance if the intervals fall into nonoverlapping buckets then you could add a constraint that the buckets of the two sides are equal. Postgres is a lot better with equality join constraints than it is with range constraints, so it would be able to match up rows and only do the O(N^2) work within each bucket. In the long run we might have better answers --- Jeff Davis has been working on range types for years now, and one of the long-range goals of that is to have smarter support for this type of problem. But for now, it's going to be painful. regards, tom lane -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql