【问题标题】:MySQL, the index of text can`t workMySQL,文本的索引不能工作
【发布时间】:2015-08-25 00:27:09
【问题描述】:

我创建一个这样的表

CREATE TABLE `text_tests` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `text_st_date` text NOT NULL,
     `varchar_st_date` varchar(255) NOT NULL DEFAULT '2015-08-25',
     `text_id` text NOT NULL,
     `varchar_id` varchar(255) NOT NULL DEFAULT '0',
     `int_id` int(11) NOT NULL DEFAULT '0',
     `created_at` datetime DEFAULT NULL,
     `updated_at` datetime DEFAULT NULL,
     PRIMARY KEY (`id`),
     KEY `idx_of_text_st_date` (`text_st_date`(50),`id`),
     KEY `idx_of_varchar_st_date` (`varchar_st_date`,`id`),
     KEY `idx_of_text_id` (`text_id`(20),`id`),
     KEY `idx_of_varchar_id` (`varchar_id`,`id`),
     KEY `idx_of_int_id` (`int_id`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

然后我使用 Ruby 制作一些数据

(1..10000).each do |_i|
  item = TextTest.new
  item.text_st_date = (Time.now + _i.days).to_s
  item.varchar_st_date = (Time.now + _i.days).to_s
  item.text_id = _i
  item.varchar_id = _i
  item.int_id = _i

  item.save
end

最后,我尝试使用文本的索引,但它不能工作,它总是全表扫描。

EXPLAIN SELECT id
FROM text_tests
ORDER BY text_st_date DESC
LIMIT 20\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: text_tests
         type: ALL  
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 9797
        Extra: Using filesort
1 row in set (0.02 sec)

EXPLAIN SELECT id
FROM text_tests
ORDER BY text_id DESC
LIMIT 20\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: text_tests
         type: ALL 
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 9797
        Extra: Using filesort
1 row in set (0.00 sec)

varchar 很好用

EXPLAIN SELECT id
FROM text_tests
ORDER BY varchar_st_date DESC
LIMIT 20\G;

*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: text_tests
         type: index
possible_keys: NULL
          key: idx_of_varchar_st_date `enter code here`
      key_len: 771
          ref: NULL
         rows: 20
        Extra: Using index
1 row in set (0.00 sec)



EXPLAIN SELECT id
FROM text_tests
ORDER BY varchar_id DESC
LIMIT 20\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: text_tests
         type: index
possible_keys: NULL
          key: idx_of_varchar_id 
      key_len: 771
          ref: NULL
         rows: 20
        Extra: Using index
1 row in set (0.00 sec)

为什么文本索引不起作用,如何使用文本索引?

【问题讨论】:

    标签: mysql optimization text indexing


    【解决方案1】:

    索引并不能满足返回结果集中表的所有行的查询。它们的主要目的之一是加速WHEREJOIN ... ON 子句。如果您的查询没有 WHERE 子句,如果查询规划器决定扫描整个表,请不要感到惊讶。

    此外,您的第一个查询执行ORDER BY text_column。但是您的索引仅包含该列的前 50 个字符。因此,为了满足查询,MySql 对整个事情进行了排序。更重要的是,它必须在硬盘上进行排序,因为内存表支持无法处理 BLOB 或 Text Large Objects。

    【讨论】:

      【解决方案2】:

      MySQL 非常擅长处理日期,但你需要告诉它你有日期,而不是VARCHAR(255)

      对日期列使用DATE 数据类型!如果 Ruby 不能帮助您做到这一点,那么请摆脱 Ruby。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-20
        • 2011-04-03
        • 2012-04-24
        • 1970-01-01
        • 2014-01-08
        相关资源
        最近更新 更多