English 中文(简体)
B. 在什么地方条款需要时间
原标题:Inner query in where clause is taking time in postgres

我试图从具有最高效力但日期不应大于目前日期的表格中提取行文,因此,我增加了内部询问,但在增加了内部质询之后,它要花很长时间才能返回。

select
* 
from 
   mytable 
where 
   empno =  1234  
and  
   effect_date = (select
        max(effect_date)
   from 
        mytable 
   where 
        empno =  1234  
   and 
        effect_date <= current_date)

任何人都可以建议我如何优化这一机制。

提前感谢。

问题回答

您可以避免植被封顶以下的分顺序:

select * 
  from mytable 
 where empno =  1234 
   and effect_date > current_date 
 order by effect_date desc
 limit 1;

这虽然涉及分类。 这也许不是更快的,但是在你尝试之前,你无法知道。 不过,该指数将使用<编码>>empno和effect_date的索引。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签