Re: intermittant performance problem - Mailing list pgsql-general

From marcin mank
Subject Re: intermittant performance problem
Date
Msg-id b1b9fac60903290124r31dc8342s603125bd00d535cf@mail.gmail.com
Whole thread Raw
In response to Re: intermittant performance problem  (Mike Charnoky <noky@nextbus.com>)
Responses Re: intermittant performance problem
List pgsql-general
I think (a part of) Your problem is that order by random() is O(N
logN) complexity, while You are after O(N) .

The solution (in pseudocode)

random_sample(resultset,K):
  result := first K rows from resultset
  resultset.scrollto(K+1)
  p = K+1
  while(resultset.hasMoreRows())
        row = resultset.current()
        resultset.advance()
        if(random() < K/p)
              replace a random element in result with row
        p = p+1
  return result


the invariant being that at each step result contains a random sample
of K elements.

It should be fairly easy to implement in plpgsql.

Greetings
Marcin

pgsql-general by date:

Previous
From: Eric Smith
Date:
Subject: running two clusters on one machine
Next
From: marcin mank
Date:
Subject: Re: intermittant performance problem