【问题标题】:Bulk/batch search in MySql using JPA/Native query使用 JPA/Native 查询在 MySql 中进行批量/批量搜索
【发布时间】:2021-02-27 08:04:31
【问题描述】:

我有一个包含 1000 万个实体的 MySQL 数据库表。我必须搜索具有多个参数的实体。 示例:

select * from result_table where roll=12345 and reg=6789 and Exam='ABC';

我正在寻找一种有效的方法来使用 JPA 批量实体搜索或使用单个查询或任何其他优化选项的本机查询。 示例:

select * from result_table where roll=1234 and reg=1234 and exam='ABC';
select * from result_table where roll=1234 and reg=1234 and exam='DEF';
select * from result_table where roll=2222 and reg=3333 and exam='XYZ';

Mysql IN Operator 无法解决我的问题,因为 roll 和 reg 在不同的考试中可以相同。谢谢。

【问题讨论】:

  • 为了改进显示的查询,通过组合 WHERE 中使用的所有 2 列以任意顺序创建复合索引。 CREATE INDEX idx ON result_table (roll, reg, exam);
  • roll+reg+exam 唯一

标签: mysql hibernate jpa persistence


【解决方案1】:

请试试这个,

创建表

create table Test(id integer,roll integer, reg integer, exam varchar(100));

插入记录

insert into Test(id,roll,reg, exam) values(1,1234,1234, "ABC");

insert into Test(id,roll,reg, exam) values(1,1234,1234, "DEF");

insert into Test(id,roll,reg, exam) values(1,2222,3333, "XYZ");

选择查询

select * from Test where CONCAT(roll,reg,exam) in ('12341234ABC','12341234DEF','22223333XYZ');

【讨论】:

  • CONCAT(1234,1234,'ABC')=CONCAT(123,41234,'ABC')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 1970-01-01
  • 2012-01-04
  • 2013-10-16
  • 1970-01-01
  • 2010-10-01
相关资源
最近更新 更多