Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing an InnoDB table and a problematic query
    primarykey
    data
    text
    <p>I have a biggish InnoDB table which at this moment contains about 20 million rows with ~20000 new rows inserted every day. They contain messages for different topics. </p> <pre><code>CREATE TABLE IF NOT EXISTS `Messages` ( `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `TopicID` bigint(20) unsigned NOT NULL, `DATESTAMP` int(11) DEFAULT NULL, `TIMESTAMP` int(10) unsigned NOT NULL, `Message` mediumtext NOT NULL, `Checksum` varchar(50) DEFAULT NULL, `Nickname` varchar(80) NOT NULL, PRIMARY KEY (`ID`), UNIQUE KEY `TopicID` (`TopicID`,`Checksum`), KEY `DATESTAMP` (`DATESTAMP`), KEY `Nickname` (`Nickname`), KEY `TIMESTAMP` (`TIMESTAMP`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=25195126 ; </code></pre> <p>NOTE: The Cheksum stores an MD5 checksum which prevents same messages inserted twice in the same topics. (nickname + timestamp + topicid + last 20 chars of message)</p> <p>The site I'm building has a newsfeed in which users can select to view newest messages from different Nicknames from different forums. The query is as follows:</p> <pre><code>SELECT Messages.ID AS MessageID, Messages.Message, Messages.TIMESTAMP, Messages.Nickname, Topics.ID AS TopicID, Topics.Title AS TopicTitle, Forums.Title AS ForumTitle FROM Messages JOIN FollowedNicknames ON FollowedNicknames.UserID = 'MYUSERID' JOIN Forums ON Forums.ID = FollowedNicknames.ForumID JOIN Subforums ON Subforums.ForumID = Forums.ID JOIN Topics ON Topics.SubforumID = Subforums.ID WHERE Messages.Nickname = FollowedNicknames.Nickname AND Messages.TopicID = Topics.ID AND Messages.DATESTAMP = '2013619' ORDER BY Messages.TIMESTAMP DESC </code></pre> <p>The TIMESTAMP contains an unix timestamp and DATESTAMP is simply a date generated from the unix timestamp for faster access via '=' operator instead of range scans with unix timestamps.</p> <p>The problem is, this query takes about 13 seconds ( or more ) unbuffered. That is of course unacceptable for the intented usage. Adding the DATESTAMP seemed to speed things up, but not by much. </p> <p>At this point, I don't really know what should I do. I've read about composite primary keys, but I am still unsure whether they would do any good and how to correctly implement one in this particular case.</p> <p>I know that using BIGINTs may be a little overkill, but do they affect that much? </p> <p>EXPLAIN:</p> <pre><code>+----+-------------+-----------------------+--------+---------------------------------------+------------+---------+-----------------------------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------------------+--------+---------------------------------------+------------+---------+-----------------------------------------------+------+----------------------------------------------+ | 1 | SIMPLE | FollowedNicknames | ALL | UserID,ForumID,Nickname | NULL | NULL | NULL | 8 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | Forums | eq_ref | PRIMARY | PRIMARY | 8 | database.FollowedNicknames.ForumiID | 1 | NULL | | 1 | SIMPLE | Messages | ref | TopicID,DATETIME,Nickname | Nickname | 242 | database.FollowedNicknames.Nickname | 15 | Using where | | 1 | SIMPLE | Topics | eq_ref | PRIMARY,SubforumID | PRIMARY | 8 | database.Messages.TopicID | 1 | NULL | | 1 | SIMPLE | Subforums | eq_ref | PRIMARY,ForumID | PRIMARY | 8 | database.Topics.SubforumID | 1 | Using where | +----+-------------+-----------------------+--------+---------------------------------------+------------+---------+-----------------------------------------------+------+----------------------------------------------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload