Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Sql 在语句中查询最理想的方法_Sql_Database_Postgresql - Fatal编程技术网

Sql 在语句中查询最理想的方法

Sql 在语句中查询最理想的方法,sql,database,postgresql,Sql,Database,Postgresql,我们从另一个服务接收到一组数字,这些数字应该在中的中,但问题是,这些数字最多可能是100k 在中使用时,查询速度非常慢。大约需要一分钟 从包含数字的消息中选择*(111222333… 你有什么建议可以改变,以便优化 PS:目标是过滤使用我们从其他服务接收的号码的消息 谢谢 非常频繁地对values子句使用联接比IN子句更快: select m.* from messages m join ( values (111),(222),(333),(444),(...) ) as t

我们从另一个服务接收到一组数字,这些数字应该在中的
中,但问题是,这些数字最多可能是100k

中使用
时,查询速度非常慢。大约需要一分钟

从包含数字的消息中选择*(111222333…

你有什么建议可以改变,以便优化

PS:目标是过滤使用我们从其他服务接收的号码的消息


谢谢

非常频繁地对
values
子句使用联接比
IN
子句更快:

select m.*
from messages m
  join (
     values (111),(222),(333),(444),(...)
  ) as t(id) on m.numbers = t.id;

但是,如果您可以确保ID列表中没有重复值,则此仅适用于

将数据存储在表中,并使用
连接
@GordonLinoff抱歉。。我的意思是,我们收到的数字不是来自我们的,而是来自另一种服务。目标是只过滤包含来自其他服务的数字的消息。将更新我的问题,使其更清楚。谢谢请尝试在字段
numbers
Sir上创建索引,这里的
t
是什么?t是子查询的别名。@mengmeng:看到了吗?我现在就知道了。但是有一种可能性是
m.numbers
不是唯一的:(@mengmeng:messages
中的值。
中的数字不相关。这是一个值列表,您需要在其中进行比较,以确保不会出现重复。根据
值(111)、(111)
进行连接将返回与(111111)
中的
不同的结果