English 中文(简体)
MySQL 极为缓慢的查询索引
原标题:MySQL Indexes for extremely slow queries

下面的询问,不论环境如何,都需要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

我知道,在改进数据库的效率方面,我可以做些小事,例如,在没有必要的情况下,减少分类账的规模。 然而,考虑到在此仅产生结果所花费的艰难时间,这些都相当微不足道。 我怎么能够根据我所看到的什么解释来适当编制这些表格的索引? 看来,我已尝试过大量组合,但没有结果。 而且,还有谁能看到,这将优化桌子,减少问答? 我需要用少于二次计算。 提前感谢!

问题回答

1. 导言 如果您希望使用<代码>r.plus_dt索引,而不是:

DATEDIFF(NOW(), r.added_dt) < 1000000

使用:

CURDATE() - INTERVAL 1000000 DAY < r.added_dt 

不管怎样,上述条件是检查<代码>附加_at 是1百万天。 您是否真正储存了这么老的日期? 否则,你就可以简单地消除这一条件。

如果你想要这一条件,关于<代码>的附加索引——at 。 这将有助于很多工作。 您问,目前的情况是,将这一条件的所有行文都核对起来,并称之为<条码>执行局(>)<>条码/条码>,其功能与<条码>的行文相同。


2.Since r.response_answer 不能使用<代码>NUL,而不是:

SELECT COUNT( r.response_answer ) 

使用:

SELECT COUNT( * ) 

www.un.org/spanish/ga/president


3. 。 您用来填写表格的三个领域有两个不同的数据类型:

ON       question . answer_group_id 
   = answer_group . answer_group_id

CREATE TABLE question (
  ...
  answer_group_id mediumint(8) ...,               <--- mediumint

CREATE TABLE answer_group (
  answer_group_id` tinyint(3)  ...,               <--- tinyint

-------------------------------

ON org_survey . org_survey_code 
   = response . org_survey_code

CREATE TABLE response (
  ...
  org_survey_code varchar(7) NOT NULL,               <--- 7

CREATE TABLE org_survey (
  ...
  org_survey_code varchar(10) NOT NULL default   ,   <--- 10

数据类型<代码>mediumint与tinyint不相同,varchar(7)varchar(10)相同。 当他们被用于加入时,我的SQL不得不失去从一种类型转换到另一种类型的时间。 更改其中一种数据类型,使之具有相同的数据类型。 这不是问题的主要问题,但这一变化还将帮助使用这些内容的所有其他询问。

在作出这一改动之后,对表格进行了分析。 它将帮助我制定更好的执行计划。


页: 1 这不是一个错误,而是最好使用<代码>response_answer = 5 (如果你不这样做,MySQL将作任何改动,将<代码>5改为<编码> 5。

真正的问题是,你没有在<代码>中使用的3个领域设置一个复合指数。 WHERE conditions. 添加:

ALTER TABLE response 
  ADD INDEX ind_u1_ra1_aa
      (uuid(1), response_answer(1), added_at) ;

(这在您的议席不小时可能发生)

Can you try the following query? I ve removed the sub-query from your original one. This may let the optimiser produce a better execution plan.

SELECT COUNT(r.response_answer) 
FROM response r
    INNER JOIN question q      ON r.question_id = q.question_id
    INNER JOIN answer_group ag ON q.answer_group_id = ag.answer_group_id
    INNER JOIN org_survey os   ON os.org_survey_code = r.org_survey_code
WHERE 
      ag.answer_group_stat =   statistic 
  AND os.survey_id = 42
  AND r.response_answer = 5
  AND DATEDIFF(NOW(), r.added_dt) < 1000000
  AND r.uuid IS NOT NULL




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签