Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.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_Postgresql_Sqlperformance - Fatal编程技术网

Sql 加入的顺序真的很麻烦吗?

Sql 加入的顺序真的很麻烦吗?,sql,postgresql,sqlperformance,Sql,Postgresql,Sqlperformance,我提出了这个问题 select L.some_variable, C.id from l_table L left join c_table C using (some_variable) where C.id is null 然后查看上述查询的更完整版本 select L.*, C.* from l_table L left join c_table C using (some_variable) where C.id is null order by L.some_variable 第二

我提出了这个问题

select L.some_variable, C.id from l_table L
left join c_table C using (some_variable)
where C.id is null
然后查看上述查询的更完整版本

select L.*, C.* from l_table L
left join c_table C using (some_variable)
where C.id is null
order by L.some_variable
第二次用了8倍的时间。我确信L.*和C.*不是“有罪的”。some_变量是一个字符串字段。但订购这项服务不可能是一个20分钟的操作。这么多差异的根源是什么

有些变量也不是索引,但这会影响这两个操作,或者排序需要某种索引才能在联接中很好地执行?

如果这是您的查询:

select L.*, C.*
from l_table L left join
     c_table C
     using (some_variable)
where C.id is null
order by L.some_variable;
我倾向于这样写:

select l.*
from l_table l
where not exists (select 1 from c_table c where () )
order by l.some_variable;
表c
中的列在
select
中似乎是多余的,因为它们可能是
NULL


对于此查询,
l\u表(l.some\u变量,)
上的索引可能能够阻止花费如此多时间的排序。

Show
explain analyze
请两者都使用。我认为“order by”会降低排序速度,它必须对结果进行排序,可能使用文件排序。除非可以使用索引按所需顺序检索数据,否则,
orderby
将导致排序。它可能只对字符串字段(和内部行指针)进行排序,但必须对所有结果行应用顺序。因此,有序列的大小、所选列的数量和大小以及行的数量都是重要的因素。正如CraigRinger所说,你需要一个解释来确定发生了什么。而且,当你试图理解某件事时,一次只改变一件事。在这里,您将添加所有列和order by。然后你说“我肯定L*,C.*不是‘有罪的’”——但你是不是两方面都试过了?