MySQL Forums
Forum List  »  Newbie

Re: New to design; how to optimize 3 column single table with high I/O?
Posted by: Rick James
Date: July 11, 2011 10:13PM

> per user per event
Eh? I don't see any column for "event"?

Can you really guarantee that you won't insert two rows with the same millisecond? Of so, then
CREATE TABLE foo (
    userid MEDIUMINT UNSIGNED NOT NULL, 
    timestmp BIGINT UNSIGNED NOT NULL, -- or DOUBLE
    state  ENUM('yes', 'no') NOT NULL,
    PRIMARY KEY(userid, timestmp)   -- Here's your index.
) ENGINE=InnoDB;

CREATE TABLE Users (
    userid MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
    ...,  -- info about the user
    PRIMARY KEY (userid)
) ENGINE=InnoDB;

Any query that includes WHERE userid=... will be quite efficient. In particular, for
> For User X at Time X, Yes or No?
SELECT state
    FROM foo
    WHERE userid = ...
      AND timestmp <= ...
    ORDER BY timestmp DESC
    LIMIT 1;
Very efficient.

If you are starting with a username, then a JOIN would be indicated.

Options: ReplyQuote


Subject
Written By
Posted
Re: New to design; how to optimize 3 column single table with high I/O?
July 11, 2011 10:13PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.