mysql中没有使用主索引

时间:2014-04-12 09:03:49

标签: mysql sql performance hibernate

我不是mysql的专家,我不明白为什么没有使用主要索引。

创建表脚本:

create table BVorgang(
    Id bigint NOT NULL,
    Status varchar(2) NOT NULL,
... 
    SysVersion int NOT NULL,
    SysUserIn varchar(32) NULL,
    SysUserUp varchar(32) NULL,
    SysStampIn datetime NULL,
    SysStampUp datetime NULL);
alter table BVorgang add constraint PK_BVorgang primary key (Id);
create index IND_BVorgang_Status on BVorgang (Status);

hibernate生成的查询:

select count(id) from BVorgang where id in (select vorgang1_.id from BVorgang vorgang1_ where vorgang1_.status in ('20' , '30' , '70' , '40' , '75') group by vorgang1_.id);

我有一个带有mysql数据库5.6的开发人员系统mac os,我得到以下和预期的结果:

mysql> explain extended select count(id) from BVorgang where id in (select vorgang1_.id from BVorgang vorgang1_ where vorgang1_.status in ('20' , '30' , '70' , '40' , '75') group by vorgang1_.id);
+----+-------------+-----------+--------+-----------------------------+---------------------+---------+--------------------+------+----------+-------------+
| id | select_type | table     | type   | possible_keys               | key                 | key_len | ref                | rows | filtered | Extra       |
+----+-------------+-----------+--------+-----------------------------+---------------------+---------+--------------------+------+----------+-------------+
|  1 | SIMPLE      | BVorgang  | index  | PRIMARY                     | IND_BVorgang_Status | 4       | NULL               | 5144 |   100.00 | Using index |
|  1 | SIMPLE      | vorgang1_ | eq_ref | PRIMARY,IND_BVorgang_Status | PRIMARY             | 8       | fmtest.BVorgang.Id |    1 |   100.00 | Using where |
+----+-------------+-----------+--------+-----------------------------+---------------------+---------+--------------------+------+----------+-------------+
2 rows in set, 1 warning (0,00 sec)

mysql> select count(id) from BVorgang where id in (select vorgang1_.id from BVorgang vorgang1_ where vorgang1_.status in ('20' , '30' , '70' , '40' , '75') group by vorgang1_.id);
+-----------+
| count(id) |
+-----------+
|      5074 |
+-----------+
1 row in set (0,02 sec)

另一台机器是ubuntu 12.04测试服务器(4核7 gb ram),安装了mysql 5.5。如果我在这台机器上运行查询,我会得到以下结果:

mysql> explain extended select count(id) from BVorgang where id in (select vorgang1_.id from BVorgang vorgang1_ where vorgang1_.status in ('20' , '30' , '70' , '40' , '75') group by vorgang1_.id);
+----+--------------------+-----------+-------+---------------------+---------------------+---------+------+------+----------+------------------------------------------+
| id | select_type        | table     | type  | possible_keys       | key                 | key_len | ref  | rows | filtered | Extra                                    |
+----+--------------------+-----------+-------+---------------------+---------------------+---------+------+------+----------+------------------------------------------+
|  1 | PRIMARY            | BVorgang  | index | NULL                | IND_BVorgang_Status | 4       | NULL | 2228 |   100.00 | Using where; Using index                 |
|  2 | DEPENDENT SUBQUERY | vorgang1_ | range | IND_BVorgang_Status | IND_BVorgang_Status | 4       | NULL | 2039 |   100.00 | Using where; Using index; Using filesort |
+----+--------------------+-----------+-------+---------------------+---------------------+---------+------+------+----------+------------------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> select count(id) from BVorgang where id in (select vorgang1_.id from BVorgang vorgang1_ where vorgang1_.status in ('20' , '30' , '70' , '40' , '75') group by vorgang1_.id);
+-----------+
| count(id) |
+-----------+
|      2036 |
+-----------+
1 row in set (1.59 sec)

我不知道为什么主要索引不在第二台机器上使用。也许有人可以帮助我。

0 个答案:

没有答案