Probabilistic Many-to-Many Relationships (with Bloom Filters)
blog.zacharyvoase.com
blog.zacharyvoase.com
> and the upper bound on the time taken to join all three tables will be the square of that
These kinds of from-principle assertions about what Postgres's (or other DBs') performance will be like sound helpful but usually aren't. The kinds of queries you issue can change everything. Indexing can change everything. Postgres's configuration can change everything. Actual size of the table can change everything. For example, if the table is small, Postgres will keep it in memory and your plans will have scary looking but actually innocent sequential scans, which I think actually happened in his join table example.
Anyway, it's good to have a lot of tools in your toolbox, and this is an interesting tool with interesting uses. I just think it would be a grave error to take the performance ratios here as fixed.
The lack of an index on the junction table definitely did have a major effect. By just doing the following:
CREATE INDEX ON movie_person (movie_id);
CREATE INDEX ON movie_person (person_id);
The junction query speeds up to around 2ms—it's comparable to or faster than the bloom filter query. But the trade-off is revealed when you see the total size of the movie_person table (including indexes): SELECT pg_size_pretty(pg_total_relation_size('movie_person'));
=> 45MB
Whereas, by my calculations, the total size added by the bloom filters and hashes on movie and person is just 2094kB in total.I plan on adding an erratum to my article explaining my error, the time/memory trade-off, and ideas for further improvement or exploration, potentially including bloom-based GiST indexes and the opportunities for parallelization.
We've recently replaced a GIST index with a GIN one and got a lot more than the 'rule of thumb' speedup. More like an order of magnitude. Or two. So much, that Tom Lane suggested we might have stumbled onto a bug in GIST, so we'll see.
Any recommended book or set of articles for starting with Postgres?
Is there any chance that the bloom could be used as a short-circuit filter but still follow-up with the m2m join to filter out the false positives? If the query optimizer can take advantage of that, then you could likely balance the size and cost of the bloom field.
The Postgres query planner can also recheck constraints automatically to recover from bloom filter false positive matches at query time.
FYI -- bloom filters are already used internally within the PostgreSQL intarray contrib module and the full-text search functionality.
See: http://postgresql.1045698.n5.nabble.com/bloom-filter-indexes... http://code.google.com/p/postgres-learning/wiki/BloomFilter
EDIT: for clarity, typo correction
For the bloom filter: (actual time=0.033..2.546 rows=430 loops=1)
And for the join: (actual time=7.440..64.843 rows=9 loops=1)
So the join returned 9 movies for person_id=160, while the bloom filtered returned 430.
I understand it's a probabilistic model, but that's a pretty whopping difference in data. Have I missed something?
But I need to brush up on murmur hash, and bloom filters in general, so I may be entirely wrong. Maybe it's valid, and maybe that does represent a 0.5% error.
For the join table, that's 2 integers * 575,281 ratings * 4 bytes = 4,602,248 bytes used in the join table.
With the filter, in each movie row, you need to store 1632 bits for the person_filter and 1048 bits for the hash, so 3,883 movies * (1632 bits + 1048 bits) = 1,300,805 bytes.
In each user row you need to store the same number of bits for the filter and hash, so 6,040 users * (1632 bits + 1048 bits) = 2,023,400 bytes.
Is my math here wrong? With this approach you save about 1.22MB, or about 27% over the join table approach (ignoring how much overhead there is for each row of the table and each page to store the table in).
Depending on the dataset it doesn't seem like the space savings would be worth the sacrifice in accuracy.
I think the reason that OP's join-based queries are slow is that there are no indexes over his junction table's foreign keys:
CREATE TABLE movies_people (
movie_id INTEGER REFERENCES movie,
person_id INTEGER REFERENCES person
);
Thus when he wants the movies associated with person 160, the database must examine the entire junction: EXPLAIN ANALYZE SELECT * FROM movies_for_people_junction WHERE person_id = 160;
Hash Join (cost=282.37..10401.08 rows=97 width=33) (actual time=7.440..64.843 rows=9 loops=1)
Hash Cond: (movie_person.movie_id = movie.id)
-> Seq Scan on movie_person (cost=0.00..10117.01 rows=97 width=8) (actual time=2.540..59.933 rows=9 loops=1)
Filter: (person_id = 160)
-> Hash (cost=233.83..233.83 rows=3883 width=29) (actual time=4.884..4.884 rows=3883 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 233kB
-> Seq Scan on movie (cost=0.00..233.83 rows=3883 width=29) (actual time=0.010..2.610 rows=3883 loops=1)
Total runtime: 64.887 ms
Note the sequential scan on movie_person that accounts for 2.5 to 60 seconds. If there were an index on movie_person(person_id), this could be an index scan.(EDITED TO ADD: I totally misread the timings in milliseconds for timings in seconds, so most of what I originally wrote is off by a factor of 1000. I'm leaving it here for entertainment value and because you might want to play with the data set in SQLite. But my point is still valid: a vanilla join is comparable in performance to the OP's bloom-filter method.)
I'm having a hard time believing that the straightforward join on a data set as small as the OP's sample is really going to take 65 seconds on PostgreSQL. Maybe that's what EXPLAIN predicts (with spotty stats, I'd wager), but EXPLAIN is not a reliable way to measure performance. For this data, I'd expect real queries to perform much better.
EDITED TO ADD: The OP's article shows the results for EXPLAIN ANALYZE, which ought to have performed the queries. So I'm not sure why the results are so slow.
Heck, even SQLite, when processing a superset of the data set on my 4-year-old computer, can do the OP's final query (and return additional ratings data) almost instantly:
$ time sqlite3 ratings.db '
select *
from
users
natural join ratings
natural join movies
where user_id = 160
' > /dev/null
real 0m0.006s
user 0m0.002s
sys 0m0.004s
If you want to try some queries yourself, here you go: $ wget http://www.grouplens.org/system/files/ml-1m.zip
$ unzip ml-1m.zip
$ cd m1-1m
$ sqlite3 ratings.db <<EOF
CREATE TABLE movies (
movie_id INTEGER PRIMARY KEY NOT NULL
, title TEXT NOT NULL
, genres TEXT NOT NULL
);
CREATE TABLE users (
user_id INTEGER PRIMARY KEY NOT NULL
, gender TEXT NOT NULL
, age TEXT NOT NULL
, occupation TEXT NOT NULL
, zipcode TEXT NOT NULL
);
CREATE TABLE ratings (
user_id INTEGER REFERENCES users(user_id)
, movie_id INTEGER REFERENCES movies(movie_id)
, rating INTEGER NOT NULL
, timestamp INTEGER NOT NULL
, PRIMARY KEY (user_id, movie_id)
);
.separator ::
.import movies.dat movies
.import users.dat users
.import ratings.dat ratings
EOF
Fully enumerating the joined data takes under a second: $ time sqlite3 ratings.db '
select count(*)
from
users
natural join ratings
natural join movies
'
1000209
real 0m0.953s
user 0m0.925s
sys 0m0.021s
And even going so far as to print the joined data takes under six seconds: $ time sqlite3 ratings.db '
select *
from
users
natural join ratings
natural join movies
' > /dev/null
real 0m5.586s
user 0m5.497s
sys 0m0.059s
Sometimes, the old stuff works better than we give it credit for. -> Seq Scan on movie_person (cost=0.00..10117.01 rows=97 width=8)
(actual time=2.540..59.933 rows=9 loops=1)
Filter: (person_id = 160)
If he had an index on movie_person, that would be a ~ 1ms index lookup, not a table scan. You can't do that sort of database layout without putting indexes on the M2M mapping table.(edit, oh, yeah, it's right there in the parent post....)
further, you might decide to cluster on the index or partition.
I'd also like to see how good the bloom filter's indexing is at scale, or if it winds up being some sort of table scan.
Because you can't index that bloom column, it seem's you'd always be doing full table scans.
In fact it doesn't appear any indexes were used throughout this whole exercise, is that right?
A major problem is a lack of indexes in array elements, which then forces you to build an otherwise unneeded table.
That being said, it's definitely doable, to some extent.
http://www.postgresql.org/docs/9.1/static/intarray.html#AEN1...
And it happens to use bloom filters under-the-hood to do it.
From the docs:
"Two GiST index operator classes are provided: gist__int_ops (used by default) is suitable for small- to medium-size data sets, while gist__intbig_ops uses a larger signature and is more suitable for indexing large data sets (i.e., columns containing a large number of distinct array values). The implementation uses an RD-tree data structure with built-in lossy compression."
That bit about the lossy compression in the implementation if referencing the use of a bloom filter. Also mentioned here, http://archives.postgresql.org/pgsql-hackers/2005-05/msg0147...
That being said, if you stored your bitvector as an array of powers of two, then it would work. But that would be horribly inefficient in terms of space usage.
In your case, though, a strict immutable function mapping the bitvector to an int array as part of a functional index should be sufficient to use the existing contrib module: you don't need to store the things you index in the table.
I see the table `movies_people` uses (SIGNED) INTEGERS as datatypes, but they reference to a UNSIGNED BIGINT (SERIAL).
This fact makes it unusable for many use-cases, but it’s an interesting and good article nevertheless.