【发布时间】:2016-07-17 22:06:03
【问题描述】:
我有一个超过 500 万行的表 Term。每行由一个 ID 和一个“名称”组成。使用“名称”字段进行搜索大约需要 35 秒。
MariaDB [WordDS]> select * from Term where name="Google";
+---------+--------+
| id | name |
+---------+--------+
| 1092923 | Google |
+---------+--------+
1 row in set (35.35 sec)
Hibernate 生成的表中的脚本:
DROP TABLE IF EXISTS `Term`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Term` (
`id` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
Java注解:
@Id
@Column(unique=true)
private long id;
@NotNull
@Size(min = 1, max = 100)
private String name;
在“名称”字段上搜索太慢了,所以我猜没有索引。 Hibernate 会自动在此表中的 id 和 name 上创建索引吗?如果没有,如何让它为两者创建索引?
【问题讨论】: