Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql中未使用主索引_Mysql_Sql_Performance_Hibernate - Fatal编程技术网

mysql中未使用主索引

mysql中未使用主索引,mysql,sql,performance,hibernate,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, SysSta

我不是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);
我有一个开发人员系统mac os和mysql数据库5.6,我得到了以下和预期的结果:

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)
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)
另一台机器是安装了mysql 5.5的ubuntu 12.04测试服务器(4核7 gb ram)。如果在此计算机上运行查询,将得到以下意外结果:

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)
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)

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

尝试索引提示
从BVorgang使用索引(主)中选择计数(id),其中..
@Mihai:很遗憾,这是不可能的。查询是由hibernate生成的,我想做的最后一件事是更改在许多其他系统上正确运行的源代码。请发布生成此hibernate查询的代码以及正在计算的对象。这将帮助我们调试您的problem@JamesMassey:我认为您不需要hibernate代码,因为问题可以在纯SQL中重现。问题是——为什么mysql服务器会这样工作?它与hibernate无关,只是一个sql问题。