Ruby evenmachine http stream filling a postgres db, scaling?

Status
Not open for further replies.

EverSpiff

Ars Scholae Palatinae
1,449
We're doing some research and to get a lot of current data we're mining the twitter spritzer json stream. I've written some ruby code leveraging the eventmachine http request gem to pulling the info, parse it, and passing to the DB and all that works great, but the DB is starting to create a backup.

Before inserting new user records, we're doing a select to make sure that we haven't seen anything from the user before, but as the DB has grown this request (and the subsequent update if it does exist) has gotten to take a long time and I was looking for some advice on how to better scale it. Right now our DB is somewhere around 20m entries in the particular table, and adding somewhere around 10k entries per hour (though this will drop as we begin to get less and less unique entries).

I have little DBA experience (however I'd designed/implemented a number of schemas), are these numbers normal for having performance degrade?

As far as making things better I've read some things about splitting up the large logical table into a bunch of smaller tables, but since our key (the user id) is basically random postgres would still be having to load up each smaller table in memory for each individual query.

Another approach could be inserting into a "scratch" table and syncing up with the main table in another process every few hours/days?
 

THBMan

Ars Legatus Legionis
18,256
Subscriptor
Even with a "scratch" table you'd still need to select against the main table at some point if you need to detect if you've seen the user before. First blush, I'd say you're simply missing a key somewhere. 20m entries isn't that big.

Edit: Also, you could simply place a uniqueness constraint on the key of the username and it would just fail if it already has one. I don't know your application, but that may be an option.
 

EverSpiff

Ars Scholae Palatinae
1,449
Keys seem to be fine, but maybe not, see any problems in the schema for table is below:

Code:
        Column        |  Type   | Modifiers
----------------------+---------+-----------
 id                   | bigint  | not null
 datestamp            | integer | not null
 name                 | text    |
 screen_name          | text    |
 created_at_timestamp | bigint  |
Indexes:
    "user_pk" PRIMARY KEY, btree (id)
    "user_info_screen_name_index" btree (screen_name)


Any problems with having the screen name there? Or using a bigint for the pk?


Also, I have changed the code to remove the selects. Currently its doing an insert, catching the duplicate key error if ones thrown, and then doing an update from there. I'm also keeping a hash within the function so I have a quick lookup for IDs I've already seen in current run, and can skip the first insert and go directly to update.
 

dredphul

Ars Tribunus Angusticlavius
6,347
Subscriptor++
I noticed that you are using the text type for the name and screen_name fields.

Why are using that datatype instead of char(n) or varchar(n)?

You may be experiencing a performance hit if you are sticking 100 or 200 character length strings into a field meant for unlimited length strings.

Also, what version of postgresql are you using? I don't recall the version (8.2 or 8.3?) that introduced auto-vacuuming, but if you are using an earlier version of postgresql, you have to perform vacuuming manually. You may also need to run an ANALYZE to update the statistics so that queries are using the best query plan. See: http://wiki.postgresql.org/wiki/Introduction_to_VACUUM,_ANALYZE,_EXPLAIN,_and_COUNT

Also, you may need to tweak the postgresql config in order to optimize for a now larger dataset. See http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server

Edit: Add stuff on postgresql vacuuming and configuration.
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
dredphul":3rjm728t said:
I noticed that you are using the text type for the name and screen_name fields.

Why are using that datatype instead of char(n) or varchar(n)?
In PostgreSQL, the difference is meaningless in terms of performance. Well, character is slowest due to the retarded space handling rules required by SQL. Inline v. outline storage is chosen automatically based on the length of the actual string stored.

Note that blindly doing the insert and checking for failure is racy. If another transaction runs that deletes the user record between your failed INSERT to user and your INSERT of the actual message, the message INSERT will fail too. To ensure this isn't an issue, you need to do a SELECT FOR UPDATE / FOR SHARE on the user table, doing an INSERT if that doesn't succeed. Please read the documentation to make sure you understand the rules for locks as they relate to transactions.

Also, since you didn't give us the actual queries or EXPLAIN output, it's tough to say what's going on, if anything wrong is going on at all. If you're not using '=' to do the match, you may be running into issues related to locale and need to manually create some operator classes on the index. Again, this is well covered in the documentation.

If all else fails, partitioning might help, though building a sane partitioning schema will be difficult.
 
Status
Not open for further replies.