我尽力解决以下两个简单查询,但对于每10行结果集,它会扫描整个表或至少1万行。当前books表中有20000行。

ALTER TABLE books ADD INDEX search_INX (`book_status`, `is_reviewed`,`has_image`,`published_date`)

mysql> EXPLAIN SELECT book_id FROM books ORDER BY published_date DESC LIMIT 10;
+----+-------------+-------+-------+---------------+------------+---------+------+-------+-----------------------------+
| id | se ref  |lect_type | table | type  | possible_keys | key        | key_len | rows  | Extra                       |
+----+-------------+-------+-------+---------------+------------+---------+------+-------+-----------------------------+
|  1 | SIMPLE      | books | index | NULL          | search_INX | 11      | NULL | 20431 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+------------+---------+------+-------+-----------------------------+

mysql> EXPLAIN SELECT book_id FROM books WHERE book_status='available' AND is_reviewed=true AND has_image=true ORDER BY published_date DESC LIMIT 10;
+----+-------------+-------+------+---------------+------------+---------+-------------------+-------+--------------------------+
| id | select_type | table | type  ref               || possible_keys | key        | key_len | rows  | Extra                    |
+----+-------------+-------+------+---------------+------------+---------+-------------------+-------+--------------------------+
|  1 | SIMPLE      | books | ref  | search_INX    | search_INX | 3       | const,const,const | 10215 | Using where; Using index |
+----+-------------+-------+------+---------------+------------+---------+-------------------+-------+--------------------------+

mysql> EXPLAIN SELECT book_id FROM books WHERE book_status='available' AND is_reviewed=true AND has_image=true ORDER BY published_date DESC LIMIT 10\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: books
         type: ref
possible_keys: search_INX
          key: search_INX
      key_len: 3
          ref: const,const,const
         rows: 10215
        Extra: Using where; Using index
1 row in set (0.00 sec)

Create Table: CREATE TABLE `books` (
  `book_id` int(10) unsigned NOT NULL auto_increment,
  `has_image` bit(1) NOT NULL default '',
  `is_reviewed` bit(1) NOT NULL default '\0',
  `book_status` enum('available','out of stock','printing') NOT NULL default 'available',
  `published_date` datetime NOT NULL,
  PRIMARY KEY  (`book_id`),
  KEY `search_INX` (`is_reviewed`,`has_image`,`book_status`,`published_date`)
) ENGINE=InnoDB AUTO_INCREMENT=162605 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)


有没有人知道如何解决这个问题?

最佳答案

另外,rows列中的mysql不会显示受影响的行数,但会显示可能受影响的行数(不包括LIMIT子句)。

07-24 22:03