为什么 mySQL 不在我的测试查询中使用我的索引?

发布于 2024-11-14 13:50:27 字数 4704 浏览 2 评论 0原文

我有一个简单的 mysql 表,我想用它来进行日常统计跟踪。在将一些信息放入其中并测试我要使用的示例查询后,它没有使用我的索引!我不明白为什么。

我尝试放入更多测试数据 - 200 行。我还尝试将 select * 更改为 select created,value1。没有骰子。

mysql> show create table stat_general\G
*************************** 1. row ***************************
       Table: stat_general
Create Table: CREATE TABLE `stat_general` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `nameid` smallint(6) NOT NULL,
  `value1` int(11) NOT NULL,
  `value2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `created` (`created`,`nameid`,`value2`)
) ENGINE=MyISAM AUTO_INCREMENT=49 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> select * from stat_general;
+----+---------------------+--------+--------+--------+
| id | created             | nameid | value1 | value2 |
+----+---------------------+--------+--------+--------+
|  1 | 2011-06-09 09:43:41 |      1 | 511803 |   NULL |
|  2 | 2011-06-09 09:43:41 |      2 |     44 |   NULL |
|  3 | 2011-06-09 09:43:41 |      3 |   9128 |   NULL |
|  4 | 2011-06-09 09:43:41 |      4 |    219 |   NULL |
|  5 | 2011-06-09 09:43:41 |      5 |      0 |   NULL |
|  6 | 2011-06-09 09:43:41 |      6 |      0 |   NULL |
|  7 | 2011-06-09 09:43:41 |      7 |      0 |   NULL |
|  8 | 2011-06-09 09:43:41 |      8 |      0 |   NULL |
|  9 | 2011-06-09 09:43:41 |      9 |      0 |   NULL |
| 10 | 2011-06-09 09:43:41 |     10 |    140 |   NULL |
| 11 | 2011-06-09 09:43:41 |     11 |      0 |   NULL |
| 12 | 2011-06-09 09:43:41 |     12 |    146 |   NULL |
| 13 | 2011-06-09 09:43:41 |     13 |      0 |   NULL |
| 14 | 2011-06-09 09:43:41 |     14 |      1 |   NULL |
| 15 | 2011-06-09 09:43:41 |     15 |   8981 |   NULL |
| 16 | 2011-06-09 09:43:41 |     16 |      1 |   -127 |
| 17 | 2011-06-09 09:43:41 |     16 |      2 |     -2 |
| 18 | 2011-06-09 09:43:41 |     16 |    939 |     -1 |
| 19 | 2011-06-09 09:43:41 |     16 |    146 |      1 |
| 20 | 2011-06-09 09:43:41 |     16 |   8011 |      3 |
| 21 | 2011-06-09 09:43:41 |     16 |     28 |    127 |
| 22 | 2011-06-09 09:43:41 |     16 |      1 |    128 |
| 23 | 2011-06-09 09:43:41 |     17 |    146 |      1 |
| 24 | 2011-06-09 09:43:41 |     18 |    146 |      1 |
| 25 | 2011-06-09 09:44:08 |      1 | 511803 |   NULL |
| 26 | 2011-06-09 09:44:08 |      2 |     44 |   NULL |
| 27 | 2011-06-09 09:44:08 |      3 |   9128 |   NULL |
| 28 | 2011-06-09 09:44:08 |      4 |    219 |   NULL |
| 29 | 2011-06-09 09:44:08 |      5 |      0 |   NULL |
| 30 | 2011-06-09 09:44:08 |      6 |      0 |   NULL |
| 31 | 2011-06-09 09:44:08 |      7 |      0 |   NULL |
| 32 | 2011-06-09 09:44:08 |      8 |      0 |   NULL |
| 33 | 2011-06-09 09:44:08 |      9 |      0 |   NULL |
| 34 | 2011-06-09 09:44:08 |     10 |    140 |   NULL |
| 35 | 2011-06-09 09:44:08 |     11 |      0 |   NULL |
| 36 | 2011-06-09 09:44:08 |     12 |    146 |   NULL |
| 37 | 2011-06-09 09:44:08 |     13 |      0 |   NULL |
| 38 | 2011-06-09 09:44:08 |     14 |      1 |   NULL |
| 39 | 2011-06-09 09:44:08 |     15 |   8981 |   NULL |
| 40 | 2011-06-09 09:44:08 |     16 |      1 |   -127 |
| 41 | 2011-06-09 09:44:08 |     16 |      2 |     -2 |
| 42 | 2011-06-09 09:44:08 |     16 |    939 |     -1 |
| 43 | 2011-06-09 09:44:08 |     16 |    146 |      1 |
| 44 | 2011-06-09 09:44:08 |     16 |   8011 |      3 |
| 45 | 2011-06-09 09:44:08 |     16 |     28 |    127 |
| 46 | 2011-06-09 09:44:08 |     16 |      1 |    128 |
| 47 | 2011-06-09 09:44:08 |     17 |    146 |      1 |
| 48 | 2011-06-09 09:44:08 |     18 |    146 |      1 |
+----+---------------------+--------+--------+--------+
48 rows in set (0.00 sec)

mysql> select * from stat_general where created>'2011-06-09 9:44' AND nameid=1;
+----+---------------------+--------+--------+--------+
| id | created             | nameid | value1 | value2 |
+----+---------------------+--------+--------+--------+
| 25 | 2011-06-09 09:44:08 |      1 | 511803 |   NULL |
+----+---------------------+--------+--------+--------+
1 row in set (0.00 sec)

mysql> explain select * from stat_general where created>'2011-06-09 9:44' AND nameid=1;
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | stat_general | ALL  | created       | NULL | NULL    | NULL |   48 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

I have a simple mysql table I want to use for daily stat tracking. After putting some information into it and testing out a samply query I'd use on it, it's not using my index! I can't figure out why.

I tried putting in more test data - 200 rows. I also tried changing select * to select created,value1. No dice.

mysql> show create table stat_general\G
*************************** 1. row ***************************
       Table: stat_general
Create Table: CREATE TABLE `stat_general` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `nameid` smallint(6) NOT NULL,
  `value1` int(11) NOT NULL,
  `value2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `created` (`created`,`nameid`,`value2`)
) ENGINE=MyISAM AUTO_INCREMENT=49 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> select * from stat_general;
+----+---------------------+--------+--------+--------+
| id | created             | nameid | value1 | value2 |
+----+---------------------+--------+--------+--------+
|  1 | 2011-06-09 09:43:41 |      1 | 511803 |   NULL |
|  2 | 2011-06-09 09:43:41 |      2 |     44 |   NULL |
|  3 | 2011-06-09 09:43:41 |      3 |   9128 |   NULL |
|  4 | 2011-06-09 09:43:41 |      4 |    219 |   NULL |
|  5 | 2011-06-09 09:43:41 |      5 |      0 |   NULL |
|  6 | 2011-06-09 09:43:41 |      6 |      0 |   NULL |
|  7 | 2011-06-09 09:43:41 |      7 |      0 |   NULL |
|  8 | 2011-06-09 09:43:41 |      8 |      0 |   NULL |
|  9 | 2011-06-09 09:43:41 |      9 |      0 |   NULL |
| 10 | 2011-06-09 09:43:41 |     10 |    140 |   NULL |
| 11 | 2011-06-09 09:43:41 |     11 |      0 |   NULL |
| 12 | 2011-06-09 09:43:41 |     12 |    146 |   NULL |
| 13 | 2011-06-09 09:43:41 |     13 |      0 |   NULL |
| 14 | 2011-06-09 09:43:41 |     14 |      1 |   NULL |
| 15 | 2011-06-09 09:43:41 |     15 |   8981 |   NULL |
| 16 | 2011-06-09 09:43:41 |     16 |      1 |   -127 |
| 17 | 2011-06-09 09:43:41 |     16 |      2 |     -2 |
| 18 | 2011-06-09 09:43:41 |     16 |    939 |     -1 |
| 19 | 2011-06-09 09:43:41 |     16 |    146 |      1 |
| 20 | 2011-06-09 09:43:41 |     16 |   8011 |      3 |
| 21 | 2011-06-09 09:43:41 |     16 |     28 |    127 |
| 22 | 2011-06-09 09:43:41 |     16 |      1 |    128 |
| 23 | 2011-06-09 09:43:41 |     17 |    146 |      1 |
| 24 | 2011-06-09 09:43:41 |     18 |    146 |      1 |
| 25 | 2011-06-09 09:44:08 |      1 | 511803 |   NULL |
| 26 | 2011-06-09 09:44:08 |      2 |     44 |   NULL |
| 27 | 2011-06-09 09:44:08 |      3 |   9128 |   NULL |
| 28 | 2011-06-09 09:44:08 |      4 |    219 |   NULL |
| 29 | 2011-06-09 09:44:08 |      5 |      0 |   NULL |
| 30 | 2011-06-09 09:44:08 |      6 |      0 |   NULL |
| 31 | 2011-06-09 09:44:08 |      7 |      0 |   NULL |
| 32 | 2011-06-09 09:44:08 |      8 |      0 |   NULL |
| 33 | 2011-06-09 09:44:08 |      9 |      0 |   NULL |
| 34 | 2011-06-09 09:44:08 |     10 |    140 |   NULL |
| 35 | 2011-06-09 09:44:08 |     11 |      0 |   NULL |
| 36 | 2011-06-09 09:44:08 |     12 |    146 |   NULL |
| 37 | 2011-06-09 09:44:08 |     13 |      0 |   NULL |
| 38 | 2011-06-09 09:44:08 |     14 |      1 |   NULL |
| 39 | 2011-06-09 09:44:08 |     15 |   8981 |   NULL |
| 40 | 2011-06-09 09:44:08 |     16 |      1 |   -127 |
| 41 | 2011-06-09 09:44:08 |     16 |      2 |     -2 |
| 42 | 2011-06-09 09:44:08 |     16 |    939 |     -1 |
| 43 | 2011-06-09 09:44:08 |     16 |    146 |      1 |
| 44 | 2011-06-09 09:44:08 |     16 |   8011 |      3 |
| 45 | 2011-06-09 09:44:08 |     16 |     28 |    127 |
| 46 | 2011-06-09 09:44:08 |     16 |      1 |    128 |
| 47 | 2011-06-09 09:44:08 |     17 |    146 |      1 |
| 48 | 2011-06-09 09:44:08 |     18 |    146 |      1 |
+----+---------------------+--------+--------+--------+
48 rows in set (0.00 sec)

mysql> select * from stat_general where created>'2011-06-09 9:44' AND nameid=1;
+----+---------------------+--------+--------+--------+
| id | created             | nameid | value1 | value2 |
+----+---------------------+--------+--------+--------+
| 25 | 2011-06-09 09:44:08 |      1 | 511803 |   NULL |
+----+---------------------+--------+--------+--------+
1 row in set (0.00 sec)

mysql> explain select * from stat_general where created>'2011-06-09 9:44' AND nameid=1;
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | stat_general | ALL  | created       | NULL | NULL    | NULL |   48 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-11-21 13:50:27

原因有两个:

首先,你的桌子太小。有了这几行,无论查询如何,打开整个表并逐一过滤行都会更快。

接下来,即使您有更多具有非常相似数据的行,您当前的索引也没有足够的选择性。在这种情况下,随机打开磁盘页面是不值得的;按顺序打开它们并过滤掉无效行会更快。

Two reasons:

First and foremost, your table is too small. With this few rows, it's faster to open the entire table and filter the rows one by one irrespective of the query.

Next, even if you had more rows with very similar data, your current index is not selective enough. In such situations, it's not worth randomly opening disk pages; it'll be faster to open the them sequentially and filter invalid rows out.

执着的年纪 2024-11-21 13:50:27

只是为了笑...您的查询主要基于给定的“nameID”或“Created”。如果NameID(即:特定人发生了什么),我会更改索引和条件,

KEY `byName` ( `nameid`,`created`,`value2`)

然后您的查询...

where nameid = 1 AND created > '2011-06-09 9:44'

我不是肯定的,但如果条件是匹配索引的自然顺序,它可能会利用它直接与猜测索引...(特别是对于较大的数据集)...否则@Denis是对的,它只是查询数据页,因为它太小并且会遍历它们,而不是尝试考虑应该使用哪个索引。

Just for grins... Would your queries be primarily based on a given "nameID" or "Created". If NameID (ie: whats happening for a specific person), I would alter the index and criteria to

KEY `byName` ( `nameid`,`created`,`value2`)

then your query to...

where nameid = 1 AND created > '2011-06-09 9:44'

I'm not positive, but if the criteria is in natural order of the matching index, it might utilize it directly vs guessing an index... (especially with larger dataset)... Otherwise @Denis is right that it just queries the data pages since its so small and blows through them regardless instead of trying to think of which index it should use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文