查询1:
select c_bh,c_xzdm,c_twhbm,d_tjrq
from (
select c_bh,c_xzdm,c_twhbm,d_tjrq
from t_table
order by d_tjrq desc
) t1
limit 10
查询2:
select c_bh,c_xzdm,c_twhbm,d_tjrq
from t_table
order by d_tjrq desc
limit 10
同样的计划:
Limit (cost=0.44..18.79 rows=10 width=59)
-> Index Scan using i_t_table_d_tjrq on t_table (cost=0.44..32135085.58 rows=17507700 width=59)
在第一种情况下,PostgreSQL“扁平化”子查询,以便更好地进行优化。结果等同于第二个查询。
如果您想避免这种情况,请将OFFSET 0
等优化障碍放入子查询中。