I am designing in a simple forum database using innodb and got few questions . I your free time , could you please help me ?
Below are Posts and Comments DB design
CREATE TABLE `my_posts` ( `forum_id` mediumint(8) unsigned NOT NULL, `post_id` int(10) unsigned NOT NULL, // not an auto increment subject varchar(200) NOT NULL, `details` text NOT NULL, `access` tinyint(3) unsigned NOT NULL, //private,friends,public `created_by` int(10) unsigned NOT NULL, `created_on` int(10) unsigned NOT NULL, `updated_on` int(10) unsigned NOT NULL, `comment_count` smallint(5) unsigned NOT NULL DEFAULT '0', `ip_address` int(10) unsigned NOT NULL, sphinx_unique_id int(10) unsigned NOT NULL, // not an auto increment PRIMARY KEY (`forum_id`,`post_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `my_posts_comments` ( `forum_id` mediumint(8) unsigned NOT NULL, `post_id` int(10) unsigned NOT NULL, // not an auto increment `comment_id` int(10) unsigned NOT NULL, `details` text NOT NULL, `created_by` int(10) unsigned NOT NULL, `created_on` int(10) unsigned NOT NULL, `ip_address` int(10) unsigned NOT NULL, PRIMARY KEY (`forum_id`,`post_id`,comment_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I have queries based on forum_id, post_id, created_by and also sorting on updated_on , comment_count
SELECT * FROM my_posts WHERE access=public ORDER BY updated_on DESC //OR ASC SELECT * FROM my_posts WHERE access=public ORDER BY comment_count DESC //OR ASC SELECT * FROM my_posts WHERE forum_id=? and access=public ORDER BY updated_on DESC //OR ASC SELECT * FROM my_posts WHERE forum_id=? and access=public ORDER BY comment_count DESC //OR ASC SELECT * FROM my_posts WHERE created_by=? ORDER BY updated_on DESC //OR ASC SELECT * FROM my_posts WHERE created_by=(Friends_ids) AND access=(public,friends) ORDER BY updated_on DESC //OR ASC SELECT * FROM my_posts WHERE forum_id=? AND post_id=? SELECT * FROM my_posts_comments WHERE forum_id=? AND post_id=?
1) Is this the best design for using Cluster Index?
2) Since the Cluster primary key on forum_id, post_id, I see problem accessing it just conditioning on created_by . What is the best design to query on created_by? Can I just add index for it . I will loose Cluster performance here ..
3) Also All the queries I have sorting on updated_on and on comment_count . These two are not of PK , so the first two simple queries looks like a major problem because the data is physically ordered in the database on forum_id and on post_id level
Thank You for checking it
Regards
Yii Fan