MySQL Performance issue with large number of rows -
i developing site based on xenforo engine, , have problem in query fetches threads , joins post table , forum table information first post , forum thread belongs to. query looks like:
select thread . *
xf_thread thread
inner join xf_node node on (node.node_id = thread.node_id)
inner join xf_post post on (post.post_id = thread.first_post_id)
thread.node_id in ('295', '296', '297', '298', '299', '300', '301', '302', '256', '2575', '258', '259', '260', '253', '254', '255', '127', '163', '159', '144', '145', '146', '147', '148', '164', '165', '166', '167', '168', '169', '170', '162', '171', '173', '172', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '151', '152', '153', '154', '155', '157', '156', '158', '161', '160', '149', '227', '232', '237', '233', '236', '234', '235', '238', '248', '240', '241', '242', '239', '246', '247', '243', '244', '245', '228', '229', '230', '231', '249', '250', '251', '174', '190', '195', '199', '193', '191', '197', '198', '192', '200', '204', '207', '205', '203', '206', '202', '208', '201', '187', '176', '177', '178', '189', '188', '180', '186', '184', '185', '182', '183', '181', '179', '209', '211', '217', '218', '219', '210', '212', '213', '214', '215', '216', '220', '222', '223', '224', '221', '225', '261', '291', '276', '272', '270', '265', '277', '267', '286', '292', '289', '274', '264', '287', '278', '282', '279', '281', '280', '283', '284', '285', '290', '275', '268', '263', '266', '294', '262', '293', '269', '273', '288', '271')
order thread.last_post_date desc
limit 10
explain results query are:
id select_type table type possible_keys key key_len ref rows 1 simple node index primary primary 4 null 199 using where; using index; using temporary; using filesort 1 simple thread ref node_id_last_post_date,node_id_sticky_last_post_date node_id_last_post_date 4 node.node_id 221 1 simple post eq_ref primary primary 4 thread.first_post_id 1 using index
query takes 9+ seconds execute.
removing joining of xf_node table, runs query in 0.01 seconds. explain looks
id select_type table type possible_keys key key_len ref rows 1 simple thread index node_id_last_post_date,node_id_sticky_last_post_da... last_post_date 4 null 69970 using 1 simple post eq_ref primary primary 4 thread.first_post_id 1 using index
removing joining of xf_post table runs query in 0.01 seconds, explain looks
id select_type table type possible_keys key key_len ref rows 1 simple thread index node_id_last_post_date,node_id_sticky_last_post_da... last_post_date 4 null 70840 using 1 simple node eq_ref primary primary 4 thread.node_id 1 using index
so, problem exists when both tables joined, joins seem correct , work separately.
number of rows in tables - xf_thread: 71,855, xf_node: 178, xf_post: 2,977,326
my assumption when both tables joined mysql starts use incorrect indexes, , maybe forcing index solve problem?
your , suggestions find way resolve problem highly appreciated.
edit: here create table statements tables involved
xf_node
create table `xf_node` ( `node_id` int(10) unsigned not null auto_increment, `title` varchar(50) not null, `description` text not null, `node_name` varchar(50) default null comment 'unique column used string id node types', `node_type_id` varbinary(25) not null, `parent_node_id` int(10) unsigned not null default '0', `display_order` int(10) unsigned not null default '1', `display_in_list` tinyint(3) unsigned not null default '1' comment 'if 0, hidden node list. still counts lft/rgt.', `lft` int(10) unsigned not null default '0' comment 'nested set info ''left'' value', `rgt` int(10) unsigned not null default '0' comment 'nested set info ''right'' value', `depth` int(10) unsigned not null default '0' comment 'depth = 0: no parent', `style_id` int(10) unsigned not null default '0' comment 'style override specific node', `effective_style_id` int(10) unsigned not null default '0' comment 'style override; pushed down tree', primary key (`node_id`), unique key `node_name_unique` (`node_name`,`node_type_id`), key `parent_node_id` (`parent_node_id`), key `display_order` (`display_order`), key `display_in_list` (`display_in_list`,`lft`), key `lft` (`lft`) ) engine=innodb default charset=utf8 auto_increment=304 ;
xf_post
create table `xf_post` ( `post_id` int(10) unsigned not null auto_increment, `thread_id` int(10) unsigned not null, `user_id` int(10) unsigned not null, `username` varchar(50) not null, `post_date` int(10) unsigned not null, `message` mediumtext not null, `ip_id` int(10) unsigned not null default '0', `message_state` enum('visible','moderated','deleted') not null default 'visible', `attach_count` smallint(5) unsigned not null default '0', `position` int(10) unsigned not null, `likes` int(10) unsigned not null default '0', `like_users` blob not null, `warning_id` int(10) unsigned not null default '0', `warning_message` varchar(255) not null default '', primary key (`post_id`), key `thread_id_post_date` (`thread_id`,`post_date`), key `thread_id_position` (`thread_id`,`position`), key `user_id` (`user_id`) ) engine=innodb default charset=utf8 auto_increment=3123657 ;
xf_thread
create table `xf_thread` ( `thread_id` int(10) unsigned not null auto_increment, `node_id` int(10) unsigned not null, `title` varchar(150) not null, `reply_count` int(10) unsigned not null default '0', `view_count` int(10) unsigned not null default '0', `user_id` int(10) unsigned not null, `username` varchar(50) not null, `post_date` int(10) unsigned not null, `sticky` tinyint(3) unsigned not null default '0', `discussion_state` enum('visible','moderated','deleted') not null default 'visible', `discussion_open` tinyint(3) unsigned not null default '1', `discussion_type` varchar(25) not null default '', `first_post_id` int(10) unsigned not null, `first_post_likes` int(10) unsigned not null default '0', `last_post_date` int(10) unsigned not null, `last_post_id` int(10) unsigned not null, `last_post_user_id` int(10) unsigned not null, `last_post_username` varchar(50) not null, `prefix_id` int(10) unsigned not null default '0', primary key (`thread_id`), key `node_id_last_post_date` (`node_id`,`last_post_date`), key `node_id_sticky_last_post_date` (`node_id`,`sticky`,`last_post_date`), key `last_post_date` (`last_post_date`) ) engine=innodb default charset=utf8 auto_increment=76301 ;
thank you.
sometimes order clause in mysql use temp table sort results. large data can take lot of time. avoid using "order desc" , sort data outside mysql.
Comments
Post a Comment