MySQL命令非常慢

时间:2014-10-01 14:00:26

标签: mysql indexing sql-order-by

我知道有很多按顺序排列的问题有各种解决方案,但我要求的是特定于我在下面设置的数据结构。

我有以下2个表设置:

表格设置

CREATE TABLE `record` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `file_group_id` INT(11) NOT NULL,
  `status_id` INT(11) NOT NULL,
  `title` VARCHAR(3000) NOT NULL,
  `date_created` DATETIME NOT NULL,
  `user_created` INT(11) NOT NULL,
  `publish_date` DATETIME NOT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_RECORD_FGID` (`file_group_id`)
);

CREATE TABLE `record_meta_text` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `record_id` INT(11) NOT NULL,
  `column_id` INT(11) NOT NULL,
  `value` VARCHAR(3000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_RMT_VALUE` (`value`(800)),
  KEY `IDX_RMT_COL_ID` (`column_id`),
  KEY `IDX_RMT_RECORD_ID_COL_ID` (`record_id`,`column_id`,`value`(800))
)

不按订单查询

SELECT
    r.title AS col_title,
    rmt_2780.value AS col_2780,
    rmt_2781.value AS col_2781,
    rmt_18474.value AS col_18474
FROM 
    record r
INNER JOIN record_meta_text AS rmt_2780 ON rmt_2780.record_id = r.id AND rmt_2780.column_id = 2780
INNER JOIN record_meta_text AS rmt_2781 ON rmt_2781.record_id = r.id AND rmt_2781.column_id = 2781
INNER JOIN record_meta_text AS rmt_18474 ON rmt_18474.record_id = r.id AND rmt_18474.column_id = 18474
WHERE 
    r.file_group_id = 2350 
    AND r.status_id = 1
LIMIT 0, 50

输出和说明

执行时间:.004秒

这是EXPLAIN输出:

    id  select_type  table      type    possible_keys                            key                       key_len  ref                             rows  Extra        
------  -----------  ---------  ------  ---------------------------------------  ------------------------  -------  ----------------------------  ------  -------------
     1  SIMPLE       r          ref     PRIMARY,IDX_RECORD_FGID                  IDX_RECORD_FGID           4        const                         527895  Using where  
     1  SIMPLE       rmt_18474  ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL)       
     1  SIMPLE       rmt_2780   ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL)       
     1  SIMPLE       rmt_2781   ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL) 

按订单查询

SELECT
    r.title AS col_title,
    rmt_2780.value AS col_2780,
    rmt_2781.value AS col_2781,
    rmt_18474.value AS col_18474
FROM 
    record r
INNER JOIN record_meta_text AS rmt_2780 ON rmt_2780.record_id = r.id AND rmt_2780.column_id = 2780
INNER JOIN record_meta_text AS rmt_2781 ON rmt_2781.record_id = r.id AND rmt_2781.column_id = 2781
INNER JOIN record_meta_text AS rmt_18474 ON rmt_18474.record_id = r.id AND rmt_18474.column_id = 18474
WHERE 
    r.file_group_id = 2350 
    AND r.status_id = 1
ORDER BY col_2780
LIMIT 0, 50 

输出和说明

执行时间:35.237秒

    id  select_type  table      type    possible_keys                            key                       key_len  ref                             rows  Extra                                         
------  -----------  ---------  ------  ---------------------------------------  ------------------------  -------  ----------------------------  ------  ----------------------------------------------
     1  SIMPLE       r          ref     PRIMARY,IDX_RECORD_FGID                  IDX_RECORD_FGID           4        const                         527895  Using where; Using temporary; Using filesort  
     1  SIMPLE       rmt_2780   ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL)                                        
     1  SIMPLE       rmt_2781   ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL)                                        
     1  SIMPLE       rmt_18474  ref     IDX_RMT_COL_ID,IDX_RMT_RECORD_ID_COL_ID  IDX_RMT_RECORD_ID_COL_ID  8        file_cabinet_data.r.id,const       1  (NULL)      

问题和解决方案

所以,我的问题是,我怎样才能让这件事不能永远带着订单。在几种情况下,我将根据条件以及where语句进行多次排序。 LIMIT / OFFSET用于分页。

record表目前有1,139,119条记录。

file_meta_text表目前有7,584,428条记录。

我有一个带有基本架构和查询的SQLFiddle,但没有添加数据(记录太多):

http://sqlfiddle.com/#!2/6ffc3/1

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我可以建议使用(因为where子句):

KEY `IDX_RECORD_FGID` (`file_group_id`,`status_id`)

而不是:

KEY `IDX_RECORD_FGID` (`file_group_id`)

并且(因为完整索引)

 KEY  `IDX_RMT_VALUE` (`value`)

而不是:

KEY  `IDX_RMT_VALUE` (`value`(800))

但请记住,在900字节的mysql没有索引之后,这就是关于order by的问题,这是一个使用未完全索引的字段的mysql操作。你确定你需要拥有"价值"长度为3000的字段?

我认为您可以使用这个更清晰的查询获得相同的信息:

SELECT
    r.title AS col_title,
    rmt.value AS value,
    rmt.column_id AS column
FROM 
    record r
INNER JOIN record_meta_text AS rmt ON rmt.record_id = r.id
WHERE 
    rmt.column_id IN(2780,2781,18474)
    AND
    r.file_group_id = 2350 
    AND r.status_id = 1
LIMIT 0, 50