Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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 大型SQL动态查询使系统崩溃_Mysql_Oracle - Fatal编程技术网

Mysql 大型SQL动态查询使系统崩溃

Mysql 大型SQL动态查询使系统崩溃,mysql,oracle,Mysql,Oracle,我们有一个类似下面的查询,具有动态值 Select * from table where dynamic value 所以在这里,部分动态值实际上取决于用户的某个值,所以假设用户和一家公司关联,那么查询将是 Select * from where company is 1 and id is 1 or ... company is n and id is n 从表中选择*,其中company是xyz,id是123,但现在让我们假设,若用户与百万公司关联,那个么查询将是 Select * fr

我们有一个类似下面的查询,具有动态值

Select * from table where dynamic value
所以在这里,部分动态值实际上取决于用户的某个值,所以假设用户和一家公司关联,那么查询将是

Select * from where company is 1 and id is 1 or ... company is n and id is n
从表中选择*,其中company是xyz,id是123
,但现在让我们假设,若用户与百万公司关联,那个么查询将是

Select * from where company is 1 and id is 1 or ... company is n and id is n
这里n是公司价值的数量。在这里,执行这个查询显然需要时间。 我们有没有办法优化它,使系统不会崩溃。也许在设计方面还有其他一些方法,但现在我们正在寻找优化查询选项的方法

编辑:

变量名是company和id,它不会改变,但它们的值会动态变化。所以,基本上,如果用户有1000家公司,那么for循环将运行1000次,每次值都会添加到where条件下的company和id中 这里只有一个前男友

StringBuilder sql = "select * from table where ";
StringBuffer query = new StringBuffer();
for(int i=0;i<user.company.size;i++){
   query+="(company is" + user.company[i]+"AND id is" + user.id[i]+") OR";
}
sql.append(query)
StringBuilder sql=“从表中选择*”;
StringBuffer查询=新建StringBuffer();

对于(inti=0;i,您可以在Oracle和MySQL中使用
IN
条件

select * from where company IN (1, ..., n) and id in (1, ..., n);

剩下的就是如何在
中生成
中的内容最好的过程取决于您对数字“n”的期望:如果大多数时候它是高的,那么您最好创建一个临时表,将这些值放入其中,并与数据表联接。即使对于较小的“n”值,这也应该是可以的。这也有好处适合您的查询始终相同。

是否“动态值”表示变量?正在使用哪些DMB-您已经标记了mysql和oracle。这两种情况下都会发生吗?您是否可以添加您正在使用的实际查询(company is 1不是有效的sql)作为猜测,听起来您应该在(Id,company)或(company,Id)上建立索引