下面的询问,不论环境如何,都需要30多秒的补偿。
SELECT COUNT( r.response_answer )
FROM response r
INNER JOIN (
SELECT G.question_id
FROM question G
INNER JOIN answer_group AG ON G.answer_group_id = AG.answer_group_id
WHERE AG.answer_group_stat = statistic
) AS q ON r.question_id = q.question_id
INNER JOIN org_survey os ON os.org_survey_code = r.org_survey_code
WHERE os.survey_id =42
AND r.response_answer = 5
AND DATEDIFF( NOW( ) , r.added_dt ) <1000000
AND r.uuid IS NOT NULL
当我解释提问时,
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1087
1 PRIMARY r ref question_id,org_survey_code,code_question,uuid,uor question_id 4 q.question_id 1545 Using where
1 PRIMARY os eq_ref org_survey_code,survey_id,org_survey_code_2 org_survey_code 12 survey_2.r.org_survey_code 1 Using where
2 DERIVED G ALL agid NULL NULL NULL 1680
2 DERIVED AG eq_ref PRIMARY PRIMARY 1 survey_2.G.answer_group_id 1 Using where
我对指数化有着非常基本的知识,但我尝试了几乎每一个组合,我认为,而且似乎不能提高这一询问的速度。 答复表是大约200万行的,问题大约为1 500行,回答小组大约为50名,而org_survey大约为8 000名。
下面是每个机构的基本结构:
CREATE TABLE `response` (
`response_id` int(10) unsigned NOT NULL auto_increment,
`response_answer` text NOT NULL,
`question_id` int(10) unsigned NOT NULL default 0 ,
`org_survey_code` varchar(7) NOT NULL,
`uuid` varchar(40) default NULL,
`added_dt` datetime default NULL,
PRIMARY KEY (`response_id`),
KEY `question_id` (`question_id`),
KEY `org_survey_code` (`org_survey_code`),
KEY `code_question` (`org_survey_code`,`question_id`),
KEY `IDX_ADDED_DT` (`added_dt`),
KEY `uuid` (`uuid`),
KEY `response_answer` (`response_answer`(1)),
KEY `response_question` (`response_answer`(1),`question_id`),
) ENGINE=MyISAM AUTO_INCREMENT=2298109 DEFAULT CHARSET=latin1
CREATE TABLE `question` (
`question_id` int(10) unsigned NOT NULL auto_increment,
`question_text` varchar(250) NOT NULL default ,
`question_group` varchar(250) default NULL,
`question_position` tinyint(3) unsigned NOT NULL default 0 ,
`survey_id` tinyint(3) unsigned NOT NULL default 0 ,
`answer_group_id` mediumint(8) unsigned NOT NULL default 0 ,
`seq_id` int(11) NOT NULL default 0 ,
PRIMARY KEY (`question_id`),
KEY `question_group` (`question_group`(10)),
KEY `survey_id` (`survey_id`),
KEY `agid` (`answer_group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1860 DEFAULT CHARSET=latin1
CREATE TABLE `org_survey` (
`org_survey_id` int(11) NOT NULL auto_increment,
`org_survey_code` varchar(10) NOT NULL default ,
`org_id` int(11) NOT NULL default 0 ,
`org_manager_id` int(11) NOT NULL default 0 ,
`org_url_id` int(11) default 0 ,
`division_id` int(11) default 0 ,
`sector_id` int(11) default NULL,
`survey_id` int(11) NOT NULL default 0 ,
`process_batch` tinyint(4) default 0 ,
`added_dt` datetime default NULL,
PRIMARY KEY (`org_survey_id`),
UNIQUE KEY `org_survey_code` (`org_survey_code`),
KEY `org_id` (`org_id`),
KEY `survey_id` (`survey_id`),
KEY `org_survey_code_2` (`org_survey_code`,`total_taken`),
KEY `org_manager_id` (`org_manager_id`),
KEY `sector_id` (`sector_id`)
) ENGINE=MyISAM AUTO_INCREMENT=9268 DEFAULT CHARSET=latin1
CREATE TABLE `answer_group` (
`answer_group_id` tinyint(3) unsigned NOT NULL auto_increment,
`answer_group_name` varchar(50) NOT NULL default ,
`answer_group_type` varchar(20) NOT NULL default ,
`answer_group_stat` varchar(20) NOT NULL default demographic ,
PRIMARY KEY (`answer_group_id`)
) ENGINE=MyISAM AUTO_INCREMENT=53 DEFAULT CHARSET=latin1
我知道,在改进数据库的效率方面,我可以做些小事,例如,在没有必要的情况下,减少分类账的规模。 然而,考虑到在此仅产生结果所花费的艰难时间,这些都相当微不足道。 我怎么能够根据我所看到的什么解释来适当编制这些表格的索引? 看来,我已尝试过大量组合,但没有结果。 而且,还有谁能看到,这将优化桌子,减少问答? 我需要用少于二次计算。 提前感谢!