简体   繁体   English

MySQL:为什么简单查询不使用索引,而是执行文件排序

[英]MySQL: Why is simple query not using index, performing filesort

I have a table defined like so: 我有这样定义的表:

`id` int(10) NOT NULL AUTO_INCREMENT,
`slug` varchar(150) NOT NULL,
`title` varchar(150) NOT NULL,
`description` text NOT NULL,
`ordinal` int(10) NOT NULL DEFAULT '0'

Let's call it t1 叫它t1

In t1 , I have an index on ordinal . t1中 ,我有序数索引。

This table only contains a few rows, it's a definitions table so I usually do this, to get the definitions in the order I want them 该表仅包含几行,这是一个定义表,因此我通常这样做,以便按我想要的顺序获取定义

SELECT * FROM t1 WHERE 1 ORDER BY ordinal;

If I perform an EXPLAIN on that statement, I get the following: 如果我对该语句执行EXPLAIN ,则会得到以下信息:

id? select_type?    table?  partitions? type?   possible_keys?  key?    key_len?    ref?    rows?   Extra?
1   SIMPLE  t1  NULL    ALL NULL    NULL    NULL    NULL    5   Using where; Using filesort

It doesn't really matter that the row above is screwed up in alignment. 上面的行对齐对齐并不重要。 The important part is that it's using filesort and I can't figure out why. 重要的是它正在使用文件排序,我不知道为什么。

Since it's only 5-10 rows in this table, it can feel like it's not important but the filesort makes my open_tables go a bit bananas since MySQL (according to the mighty internet) opens TWO tables for each filesort query it needs to perform. 由于该表中只有5-10行,因此感觉不重要,但由于MySQL(根据强大的互联网)针对需要执行的每个文件排序查询打开了两个表,因此文件排序使我的open_tables变得有点香蕉了。

So, greatful for any help here. 因此,非常感谢您的帮助。 Thanks. 谢谢。

Your table does not have any index on the "ordinal" column that would possibly get utilized. 您的表在“普通”列上没有任何可能被利用的索引。 Also, since your WHERE clause is on a fixed "1" value which is always true, but no column to compare or even LOOK for an index to help, it can't pick anything... So you are going through a filesort... no index applicable to the where and no index applicable to the order by clause. 另外,由于您的WHERE子句始终为固定的“ 1”值,但没有可比较的列,甚至没有索引来帮助索引,因此它无法选择任何内容……因此,您正在对文件进行排序。 ..没有适用于where的索引,也没有适用于order by子句的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM