小编Den*_*nys的帖子

PostgreSQL 中的子查询魔法

我有一个疑问:

update product_product 
set (write_date, default_code) = (LOCALTIMESTAMP, 'update') 
where product_tmpl_id in (
    select distinct product_tmpl_id 
    from product_template 
    where type='import');
Run Code Online (Sandbox Code Playgroud)

而且需要7个小时才能完成。但是,当我执行子查询时:

select distinct product_tmpl_id 
from product_template 
where type='import';
Run Code Online (Sandbox Code Playgroud)

我收到错误:

列“product_tmpl_id”不存在

product_tmpl_id表中没有列product_template

这是我的错误。第一个查询应该是:

update product_product set (write_date,default_code) = (LOCALTIMESTAMP,'update') 
where product_tmpl_id in
 (select distinct id from product_template where type='import');
Run Code Online (Sandbox Code Playgroud)

它只需要几秒钟即可运行。

所以我的问题如下:

  • 为什么第一个查询没有失败?
  • 为什么要运行这么长时间?
  • 它到底做了什么?

结果select version();

PostgreSQL 9.4.3 on x86_64-unknown-linux-gnu, 
compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
Run Code Online (Sandbox Code Playgroud)

postgresql subquery postgresql-9.4

4
推荐指数
1
解决办法
480
查看次数

标签 统计

postgresql ×1

postgresql-9.4 ×1

subquery ×1