获取计数(*)与实际数据具有性能影响

时间:2016-03-09 20:02:18

标签: mysql indexing database-performance full-text-indexing

我无法理解以下两个查询。第一个只获得整个结果集的计数。

第二个获取实际数据,但将结果集限制为10行。

  • 不知何故,第一个人不能使用索引。我尝试使用USE INDEX ( timestamp_index , Fulltext_title , Fulltext_description )但无济于事。
  • 计数查询不需要订单,但我只是想看看它是否可以使用索引。
  • 据我所知,WHERE子句是相同的,据我所知,这是选择索引的最大因素。

获取COUNT

SELECT count(*) as total FROM table1 
WHERE 1=1
AND type in ('category1','category3','category2') 
AND ( 
    MATCH(title) AGAINST (' +"apple"' IN BOOLEAN MODE)
    OR 
    MATCH(description) AGAINST (' +"apple"' IN BOOLEAN MODE) 
    )
ORDER BY timestamp DESC
;
+-------+
| total |
+-------+
|   798 |
+-------+
1 row in set (3.75 sec)

EXPLAIN EXTENDED

+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref      | rows   | filtered | Extra       |
+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | table1   | ALL  | NULL          | NULL | NULL    | NULL | 669689 |   100.00 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+--------+----------+-------------+

获取实际结果

SELECT id, title,desciption,timestamp  FROM table1
WHERE 1=1
AND type in ('category1','category3','category2') 
AND ( 
    MATCH(title) AGAINST (' +"apple"' IN BOOLEAN MODE)
    OR 
    MATCH(description) AGAINST (' +"apple"' IN BOOLEAN MODE) 
    )
ORDER BY timestamp DESC
LIMIT 0, 10 ;

10行(0.06秒)

EXPLAIN EXTENDED

+----+-------------+----------+-------+---------------+------+---------+------+------+------------+-------------+
| id | select_type | table    | type  | possible_keys | key  | key_len | ref      | rows | filtered   | Extra       |
+----+-------------+----------+-------+---------------+------+---------+----   --+------+------------+-------------+
|  1 | SIMPLE      | table1 index | NULL          | timestamp_index   | 21          | NULL |   10 | 6696890.00 | Using where |
+----+-------------+----------+-------+---------------+------+---------+------+------+------------+-------------+

2 个答案:

答案 0 :(得分:1)

第二次查询。你想要前10个元素。所以优化器使用时间戳索引,对表进行排序并继续检查行,直到找到与您的WHERE匹配的10个元素

在第一次查询时,数据库必须扫描整个数据库以查找与查询匹配的元素,因此您的ORDER BY没有帮助,因为您需要计算与yor匹配的总行数。

现在还取决于您如何定义索引。您有TypeTitleDescription的一个索引吗?你有compisite索引吗?

检查一个MySQL索引TIPS

答案 1 :(得分:0)

我找到了答案......我将两个索引结合起来。因此我们不必仅仅因为我们正在进行计数(*)

而进行全表扫描
SELECT count(*) as total FROM table1 WHERE 1=1
    AND type in ('category1','category2','category3') 
    AND MATCH(title, description) AGAINST (' +"apple"' IN BOOLEAN MODE)  
;

+----+-------------+----------+----------+----------------------+----------------------+---------+------+------+----------+-------------+
| id | select_type | table    | type     | possible_keys        | key                  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+----------+----------------------+----------------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | table1   | fulltext | FT_title_description | FT_title_description | 0       | NULL |    1 |   100.00 | Using where |
+----+-------------+----------+----------+----------------------+----------------------+---------+------+------+----------+-------------+
+-------+
| total |
+-------+
|   798 |
+-------+
1 row in set (0.83 sec)