AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-23688

Luan Huynh's questions

Martin Hope
Luan Huynh
Asked: 2017-07-14 02:52:48 +0800 CST

使用 postgres_fdw 在外部表中 ORDER BY 太慢

  • 3

PostgreSQL v9.6,postgres_fdw

国外表

CREATE FOREIGN TABLE user_info (
  id bigint ,
  info jsonb 
) 
SERVER server_test_fdw OPTIONS(SCHEMA_NAME 'public', TABLE_NAME 'user_info_raw' );
-- user_info_raw is a large table (100 million records, 200 GB)

info 列样本数据

{"key1": 1, "key2": 0.678}
{"key1": 1, "key2": 1.0}
{"key1": 1, "key2": 0.986} 
{"key1": 2, "key2": 0.75}
{"key1": 2, "key2": 0.639} 

外表查询 (更新)

SELECT id, info 
FROM user_info
WHERE info ->> 'key1'= '1' -- OR using jsonb_extract_path_text(info, 'key1')  = '1'
ORDER BY id 
LIMIT 10; 

Limit  (cost=10750829.63..10750829.65 rows=10 width=40) (actual time=550059.320..550059.326 rows=10 loops=1)
   ->  Sort  (cost=10750829.63..10751772.77 rows=377257 width=40) (actual time=550059.318..550059.321 rows=10 loops=1)
         Sort Key: id
         Sort Method: top-N heapsort  Memory: 26kB
         ->  Foreign Scan on user_info (cost=100.00..10742677.24 rows=377257 width=40) (actual time=1.413..536718.366 rows=68281020 loops=1)
               Filter: ((info ->> 'key1'::text) = '1'::text)
               Rows Removed by Filter: 7170443
 Planning time: 4.097 ms
 Execution time: 550059.597 ms

查询 user_info_raw

EXPLAIN ANALYSE
SELECT id, info 
FROM user_info_raw
WHERE info ->> 'key1'= '1'
ORDER BY id 
LIMIT 10;

 Limit  (cost=0.57..1296.95 rows=10 width=59) (actual time=0.043..0.073 rows=10 loops=1)
   ->  Index Scan using idx_user_info_raw_info on user_info_raw  (cost=0.57..68882850.88 rows=531346 width=59) (actual time=0.042..0.070 rows=10 loops=1)
         Filter: ((info ->> 'key1'::text) = '1'::text)
 Planning time: 0.192 ms
 Execution time: 0.102 ms 

select pg_size_pretty(pg_table_size('user_info_raw'));
 pg_size_pretty 
----------------
 223 GB

在user_info_raw(远程服务器)上执行查询需要10 毫秒。

但是,使用外部表需要很多时间。user_info当我删除ORDER BY id时,查询执行得非常快。

我认为我对外表的查询 应该发送到远程服务器执行,但不是,我不知道为什么,可能是由于postgres_fdw 文档的摘录

默认情况下,只有使用内置运算符和函数的WHERE 子句 才会被考虑在远程服务器上执行。获取行后,在本地检查涉及非内置函数的子句。如果这些函数在远程服务器上可用并且可以依赖它们产生与本地相同的结果,则可以通过发送这样的 WHERE 子句进行远程执行来提高性能

如何解决外部表中与 ORDER BY 相关的问题?


更新

添加use_remote_estimate并server没有foreign table帮助。

postgresql postgresql-9.6
  • 2 个回答
  • 1834 Views
Martin Hope
Luan Huynh
Asked: 2017-02-18 00:23:35 +0800 CST

错误:请求的字符对于编码而言太大

  • 3

我在 Linux 上使用 PostgreSQL 9.6。当我对chr()函数进行测试时出现错误。

postgres=# select chr(1199111);
ERROR:  requested character too large for encoding: 1199111
postgres=# select chr(55296);
ERROR:  requested character not valid for encoding: 55296
postgres=# select chr(100000);
 chr
-----
 ?
(1 row)

在这里,正如您所看到的 value100k它起作用(没有引发错误),但其他人没有。我对此很好奇。有人可以向我解释为什么吗?

我附上测试​​脚本

do
$$
declare 
str text ;
begin
for i in 1..1200000
loop
begin
select chr(i) into str;
exception when others then raise notice '=> i: %     => str: %', i , str ;
#exit; -- you can uncomment this star key
end ;
end loop;
end 
$$ ;

更新

postgres=# show server_encoding;
 server_encoding
-----------------
 UTF8
(1 row)
postgresql encoding
  • 1 个回答
  • 423 Views
Martin Hope
Luan Huynh
Asked: 2016-11-10 00:47:58 +0800 CST

过滤数组 text[] 并按时间戳排序

  • 8

描述

Linux 上的 PostgreSQL 9.6,tags_tmp表大小 ~ 30 GB(1000 万行),tags是一个text[]并且只有 6 个值。

tags_tmp(id int, tags text[], maker_date timestamp, value text)
id  tags        maker_date      value
1   {a,b,c}     2016-11-09      This is test 
2   {a}         2016-11-08      This is test 
3   {b,c}       2016-11-07      This is test 
4   {c}         2016-11-06      This is test 
5   {d}         2016-11-05      This is test 

我需要使用 filter ontags和order byon检索数据maker_date desc。我可以在两tags & maker_date desc列上创建索引吗?

如果没有,您能否提出其他想法?

查询示例

select id, tags, maker_date, value
from tags_tmp
where  tags && array['a','b']
order by maker_date desc
limit 5 offset 0

SQL 代码:

create index idx1 on tags_tmp using gin (tags);
create index idx2 on tags_tmp using btree(maker_date desc);

explain (analyse on, costs on, verbose)
select id, tags, maker_date, value
from tags_tmp
where tags && array['funny','inspiration']
order by maker_date desc
limit 5 offset 0 ;

解释结果:

Limit  (cost=233469.63..233469.65 rows=5 width=116) (actual time=801.482..801.483 rows=5 loops=1)
  Output: id, tags, maker_date, value
  ->  Sort  (cost=233469.63..234714.22 rows=497833 width=116) (actual time=801.481..801.481 rows=5 loops=1)
        Output: id, tags, maker_date, value
        Sort Key: tags_tmp.maker_date DESC
        Sort Method: top-N heapsort  Memory: 25kB
        ->  Bitmap Heap Scan on public.tags_tmp  (cost=6486.58..225200.81 rows=497833 width=116) (actual time=212.982..696.650 rows=366392 loops=1)
              Output: id, tags, maker_date, value
              Recheck Cond: (tags_tmp.tags && '{funny,inspiration}'::text[])
              Heap Blocks: exact=120034
              ->  Bitmap Index Scan on idx1  (cost=0.00..6362.12 rows=497882 width=0) (actual time=171.742..171.742 rows=722612 loops=1)
                    Index Cond: (tags_tmp.tags && '{funny,inspiration}'::text[])
Planning time: 0.185 ms
Execution time: 802.128 ms

更多信息

我测试了只对一个标签使用部分索引,当然,它更快。但是我有很多标签,例如:create index idx_tmp on tags_tmp using btree (maker_date desc) where (tags && array['tag1') or tags && array['tag2'] or ... or tags && array['tag6']. 我在tags && array['tag1']and之间进行了测试'tag1' = any(tags),性能是一样的。

  1. text[]只有 6 个值 = a, b, c, d, e, f。例如:tags={a,b,c}, tags={a}, tags={a,c}, tags={a,b,c,d,e,f}, tags={b,f}等等。但它不能具有价值g->z, A-Z等。

  2. create table tags_tmp(id int primary key not null, tags text[] not null, maker_date timestamp not null, value text)

  3. 在distinct数组值方面,tags其中包含a20% 行的 table where 'a' = any(tags), b=20% where 'b' = any(tags), c=20% where 'c' = any(tags), d=20% where 'd' = any(tags), e=10% where 'e' = any(tags),f=10% where 'f' = any(tags)。

  4. 另外,(tags, maker_date)也不是唯一的。

  5. 此表不是只读的。

  6. 是sort on timestamp,但我的示例显示了日期,对此感到抱歉。

现状:tags = 'a' or tags = 'b' or tags = 'c'还有更多

(1) with GIN indexor converttext[] to int[]以及 converttext[] to int等,它将在多标签上使用位图索引。最后,经过测试,我决定用老方案,OR改成很多UNION子句,每个子句UNION都会限制数据的数量。当然,我将为partial index每个标签值创建,并且可以与上面的 (1) 结合使用。就 而言OFFSET,它将使用一个或多个条件 inWHERE子句。

例子

EXPLAIN (ANALYSE ON, costs ON, VERBOSE)
SELECT rs.*
FROM (
        (SELECT tags,
                id,
                maker_date
         FROM tags_tmp
         WHERE 'a' = any(tags)
           AND maker_date <= '2016-03-28 05:43:57.779528'::TIMESTAMP
         ORDER BY maker_date DESC LIMIT 5)
      UNION
        (SELECT tags,
                id,
                maker_date
         FROM tags_tmp
         WHERE 'b' = any(tags)
           AND maker_date <= '2016-03-28 05:43:57.779528'::TIMESTAMP
         ORDER BY maker_date DESC LIMIT 5)
      UNION
        (SELECT tags,
                id,
                maker_date
         FROM tags_tmp
         WHERE 'c' = any(tags)
           AND maker_date <= '2016-03-28 05:43:57.779528'::TIMESTAMP
         ORDER BY maker_date DESC LIMIT 5)) rs
ORDER BY rs.maker_date DESC LIMIT 5 ;
postgresql index-tuning
  • 2 个回答
  • 8086 Views
Martin Hope
Luan Huynh
Asked: 2016-05-10 02:14:13 +0800 CST

一个事实有多少个日期维度

  • 6

关于设计星型模式:我在事实表中有三个日期列fact_table (insert_date, trade_date, close_date ...)。而且我不知道应该创建多少个日期维度?

在此处输入图像描述

案例 1: 暗淡 A 。这意味着:一行@fact_table 对 A 有三个 FK。

案例 2:Dim A(插入日期),Dim B(交易日期),Dim C(close_date)。这意味着:一行@fact_table 对 A 有一个 FK,对 B 有一个 FK,对 C 有一个 FK。

问题:应该创建多少个日期维度?

data-warehouse
  • 2 个回答
  • 12384 Views
Martin Hope
Luan Huynh
Asked: 2016-02-25 20:11:47 +0800 CST

生成并插入 100 万行到简单表中

  • 12

描述:

我尝试在 MSSQL 2012 Express 的空表中插入 100 万行。这是我的脚本:

-- set statistics time off
drop table t1
create table t1 (id int, a text, b text) 
go

-- #1 - 1,000,000 - 30s -> 45s
with ID(number) as
(
    select 1 as number
    union all
    select number + 1
    from ID
    where number < 1000000 + 1
)
insert into t1
    select number, 'a_' + cast (number as varchar), 'b_' + cast (number/2 as varchar)
    from ID  
    option(maxrecursion 0)


-- #2 - 1 million rows => ~140,000 rows = 120s (have to cancel query)
declare @count int
set @count = 0
while @count < 1000000
begin
    set @count = @count + 1
    insert into t1 
        values(@count, 'a_' + cast (@count as varchar), 'b_' + cast (@count/2 as varchar))
end

-- #3 - ~1,300,000 rows - 18s -> 20s  

with temp as 
(
    SELECT  ROW_NUMBER() OVER(ORDER BY a.object_id) as tcount 
    from sys.all_columns a,  sys.all_columns b
    where a.object_id = b.object_id  
) 
insert into t1
    select tcount, 'a_' + cast (tcount as varchar), 'b_' + cast (tcount/2 as varchar) 
    from temp 
go

declare @count int
set @count = 0
while @count < 3
begin
    with temp as (select max(id) + 1 as max_id from t1)
    insert into t1
        select max_id, 'a_' + cast (max_id as varchar), 'b_' + cast (max_id/2 as varchar) 
        from t1, temp 
    set @count = @count + 1
end

-- #4 -- 1,000,000 = 3s -> 4s (have to drop t1 first)
with a(k) as
(
select 1 as k
union all
select k + 1 from a where k < 99 + 1
) , 
t2 as (
select row_number() over(order by x.k) as k
from a x , a y , a z 
) 
select k as id , 'a_' + cast (k as varchar) as a, 'b_' + cast (k/2 as varchar) as b into t1
from t2

问题:

经过研究,我找到了4个解决方案。有没有更好的解决方案(不使用文件中的复制数据)?

sql-server
  • 4 个回答
  • 49061 Views
Martin Hope
Luan Huynh
Asked: 2015-12-29 02:57:12 +0800 CST

PostgreSQL翻译特殊字符?

  • 2

描述:

PostgreSQL 9.3

细绳:'ì ằ ú ề'

期望的结果: 'i a u e'

我的代码:

select translate ('ì ằ ú ề', 'ìằúề', 'iaue') ; -- it works. Result: i a u e

问题:

如果我以这种方式使用它,我必须在“ìằúề”和“iaue”之间定义手动翻译。有更好的解决方案吗?

参考:PG 文件

postgresql unaccent
  • 1 个回答
  • 2581 Views
Martin Hope
Luan Huynh
Asked: 2015-12-17 07:13:28 +0800 CST

长度 > 最大 varchar2 长度的列上的 Oracle 索引

  • 4

描述:

我们在 PostgreSQL 9.3 上有一个表,我们将它(数据和结构)迁移到 Oracle 11.0.2.4。这是我们的表格:

create table cstb_temp_log 
(
id NUMBER PRIMARY KEY,
log_info data_type(max_length) -- max_length < 10000
)
-- log_info : data is inserted/updated every second (**)
-- data_type: may be we can use "clob" because max_length of varchar2 in Oracle is 4000 (SQL).
-- cstb_temp_log : size = 1 GB, row = 400000

我们想在 log_info 列上索引和搜索“文本”,所以我们尝试了 Oracle Text 11g。

问题:

如果我们使用“data_type”是“clob”,我们可以使用“context index”并且它必须在 DML 之后同步。(我们不能使用这种方式,因为 (**) )

如果有的话,我们如何索引和搜索“log_info”列(max_length < 10000 并且数据每秒更改一次)?

oracle
  • 1 个回答
  • 1020 Views
Martin Hope
Luan Huynh
Asked: 2015-10-06 18:31:46 +0800 CST

Postgres 消耗大量 RAM(缓存)

  • 1

我在 Centos 6 x64 ( RAM: 8 GB ) 上有一个小型数据库 PostgreSQL (v9.3)。

postgresql.conf

max_connections = 512
shared_buffers = 3000MB  
temp_buffers = 8MB          
work_mem = 2MB          
maintenance_work_mem = 128MB         
effective_cache_size = 3000MB

大约 150 个连接,PostgreSQL 占用超过 6 GB 的 RAM(当然,其他应用程序使用大约 200 MB 的 RAM),这里是我的信息:

Mem:  7062.945M total, 6892.410M used,  170.535M free, 6644.000k buffers
Swap:    0.000k total,    0.000k used,    0.000k free, 5378.922M cached

问题:

  1. 为什么 PG 占用大量内存?

  2. 如何减少 PG 的缓存缓冲区?

postgresql configuration
  • 2 个回答
  • 7809 Views
Martin Hope
Luan Huynh
Asked: 2015-09-15 19:50:29 +0800 CST

使用以下参数调整查询:索引、窗口函数?

  • 2

描述

环境:Centos 6-x64,Postgres Plus Advanced Server 9.3。
我有一个包含 4 列数据的表格,如下所示:

id    code_id            effective_date                 name
24      12       "2015-09-15 02:57:47.626751+00"      "dtsc_12"
429     215      "2015-09-15 02:57:47.626751+00"      "dtsc_215"
430     215      "2015-09-15 02:57:47.626751+00"      "dtsc_215"
465     233      "2015-09-15 02:57:47.626751+00"      "dtsc_233"
466     233      "2015-09-15 02:57:47.626751+00"      "dtsc_233"
468     234      "2015-09-15 02:57:47.626751+00"      "dtsc_234"

我想获得所有符合条件的行:group by code_id和max(effective_date) < current_timestamp。所以,我想要的结果:

id     code_id             effective_date                name
24       12       "2015-09-15 02:57:47.626751+00"      "dtsc_12"
429      215      "2015-09-15 02:57:47.626751+00"      "dtsc_215"
465      233      "2015-09-15 02:57:47.626751+00"      "dtsc_233"
468      234      "2015-09-15 02:57:47.626751+00"      "dtsc_234"

我的编码

create table win_a (
   id int not null primary key,
   code_id int,
   effective_date timestamp with time zone,
   name text
);

insert into win_a
select a,  a/2,  now() + trunc(random()  * 100) * '1 day'::interval, 'dtsc_' || (a/2)::int
from generate_series(1, 500) a
ORDER BY random() ;

create index win_a_indx on win_a using btree ( code_id, effective_date desc);

-- query 1
select a.*
from (
select id, code_id, effective_date, name
     , rank() over (partition by code_id order by effective_date desc, id) as rank
from win_a
where effective_date < current_timestamp
) a
where rank = 1 ;

-- query 2 -- false if in the same code_id -> have more than two same effective_date value
select a.*
from win_a a 
join (
    select code_id, max(effective_date) as max_ef
    from win_a 
    where effective_date < current_timestamp
    group by code_id ) b 
on a.code_id = b.code_id and a.effective_date = b.max_ef;

-- query 3 -- false if in the same code_id -> have more than two same effective_date value
select a.*
from win_a a 
where (code_id, effective_date) in
     (select code_id, max(effective_date) as max_ef
    from win_a 
    where effective_date < current_timestamp
    group by code_id );

问题

  1. 我可以将索引与窗口函数一起使用吗?(对于查询 1,我尝试了一个索引,但 Postgres 仍然使用序列扫描而不是索引扫描)

  2. 我怎样才能改进我的编码?

postgresql optimization
  • 1 个回答
  • 443 Views
Martin Hope
Luan Huynh
Asked: 2015-08-04 20:17:45 +0800 CST

优先搜索列

  • 2

描述:

我们正在使用 PostgreSQL 9.3 - Centos 6 x64。我们有一个 dtsc_search_data 表如下:

dtsc_search_data 
id ---- c1 ---- c2 ---- c3
1  ----  1 ----  1 ----  1 
2  ----  1 ----  2 ----  2 
3  ----  1 ----  1 ----  3 

我们想在dtsc_search_data表上搜索“c1, c2, c3”列,条件是:如果在“c1”上找到seach_value,则返回;如果在“c1”上找不到 search_value,则在“c2”上找到,如果在“c2”上找到 search_value,则返回;否则返回(c3)。

例子:

search_value = 1 => "c1" = 1 => just search on "c1"
search value = 2 => "c1" != 2, "c2" = 2 => just search on "c2". 
search_value = 3 => "c3"

代码(更新)

create table dtsc_search_data (id int, c1 int, c2 int , c3 int) ;
 insert into dtsc_search_data values(1,1,1,1);
 insert into dtsc_search_data values(2,1,2,2);
 insert into dtsc_search_data values(3,1,1,3);

 -- search_value = 2
 -- find on c1 column first
     select * 
     from dtsc_search_data
     where c1 = 2
 -- if c1 is not found then c2 -- get value from here
     select * 
     from dtsc_search_data
     where c2 = 2   

问题:

我们的解决方案:编写 3 个 SQL 查询,搜索“c1”、“c2”、“c3”,后跟上述条件。我们怎样才能以最低的性能做到这一点?

postgresql
  • 1 个回答
  • 744 Views
Martin Hope
Luan Huynh
Asked: 2015-04-17 06:44:22 +0800 CST

删除父行时自动设置子行的值?

  • 0

描述

我们有一个表格和数据如下:

Version: PostgreSQL 9.3
Table name: tree_data (id int, code text, name text, parent_id int) 
Primary key: id 
Foreign key: parent_id (refer to id)

和数据:

insert into tree_data (id, code, name, parent_id) values (1, 'aaa','aaa', null);
insert into tree_data (id, code, name, parent_id) values (2, 'bbb','bbb', 1);
insert into tree_data (id, code, name, parent_id) values (3, 'ccc','ccc', 1); 

 id | code | name | parent_id
 1    aaa    aaa      null
 2    bbb    bbb      1
 3    ccc    ccc      1

这里我们的查询和我们想要的结果,它的意思是:删除id = 1(父行)时,表会自动在子行(第一级)中设置parent_id = null。

delete from tree_data where id = 1 ;
----> rows after deleting: 
 id | code | name | parent_id
 2    bbb    bbb      null
 3    ccc    ccc      null

我们的问题:

我们可以使用 postgresql 约束来做到这一点吗?如果没有,我们怎么办?

postgresql postgresql-9.3
  • 2 个回答
  • 903 Views
Martin Hope
Luan Huynh
Asked: 2015-03-06 18:06:08 +0800 CST

Pgpool II:无法读取两个网络接口之间的消息长度

  • 4

启动日志文件:

2015-03-06 01:57:56: pid 2760: LOG:  Setting up socket for 0.0.0.0:9999
2015-03-06 01:57:56: pid 2760: LOG:  Setting up socket for :::9999
2015-03-06 01:57:56: pid 2760: LOG:  pgpool-II successfully started. version 3.4.
0 (tataraboshi)
2015-03-06 01:57:56: pid 2760: LOG:  find_primary_node: checking backend no 0
2015-03-06 01:57:56: pid 2760: LOG:  find_primary_node: primary node id is 0
2015-03-06 01:58:02: pid 2792: ERROR:  unable to read message length
2015-03-06 01:58:02: pid 2792: DETAIL:  message length (8) in slot 1 does not mat
ch with slot 0(12)

调试日志文件:

2015-03-06 01:55:07: pid 2680: LOG:  find_primary_node: primary node id is 0
2015-03-06 01:55:42: pid 2712: DEBUG:  I am 2712 accept fd 6
2015-03-06 01:55:42: pid 2712: DEBUG:  reading startup packet
2015-03-06 01:55:42: pid 2712: DETAIL:  Protocol Major: 1234 Minor: 5679 database
:  user: 
2015-03-06 01:55:42: pid 2712: DEBUG:  selecting backend connection
2015-03-06 01:55:42: pid 2712: DETAIL:  SSLRequest from client
2015-03-06 01:55:42: pid 2712: DEBUG:  reading startup packet
2015-03-06 01:55:42: pid 2712: DETAIL:  application_name: psql
2015-03-06 01:55:42: pid 2712: DEBUG:  reading startup packet
2015-03-06 01:55:42: pid 2712: DETAIL:  Protocol Major: 3 Minor: 0 database: post
gres user: enterprisedb
2015-03-06 01:55:42: pid 2712: DEBUG:  creating new connection to backend
2015-03-06 01:55:42: pid 2712: DETAIL:  connecting 0 backend
2015-03-06 01:55:42: pid 2712: DEBUG:  creating new connection to backend
2015-03-06 01:55:42: pid 2712: DETAIL:  connecting 1 backend
2015-03-06 01:55:42: pid 2712: DEBUG:  reading message length
2015-03-06 01:55:42: pid 2712: DETAIL:  slot: 0 length: 12
2015-03-06 01:55:42: pid 2712: DEBUG:  reading message length
2015-03-06 01:55:42: pid 2712: DETAIL:  slot: 1 length: 8
2015-03-06 01:55:42: pid 2712: ERROR:  unable to read message length
2015-03-06 01:55:42: pid 2712: DETAIL:  message length (8) in slot 1 does not mat
ch with slot 0(12)

psql 命令:

[enterprisedb@testing_ppas93]$ psql -h localhost -p 9999 -d postgres
psql: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request

我使用 pgpool II 来控制两台服务器之间的故障转移事件(谷歌云上的主服务器,我本地的从属服务器,centos x64,数据库:Postgres Plus Advanced Server 9.3)。当我尝试通过端口 9999 (example: psql -h localhost -p 9999 -d postgres) 连接我的数据库时,pgpool 会引发错误

错误:无法读取消息长度

postgresql pgpool
  • 1 个回答
  • 7349 Views
Martin Hope
Luan Huynh
Asked: 2014-10-15 02:37:10 +0800 CST

后端的查询处理(磁盘上的 I/O)

  • 0

我有一个在 PostgreSQL 9.3 上创建的包含 100k 行的表

create table demo_bbb
(
  id numeric NOT NULL,
  code_bbb character varying,  
  column_02 character varying,  
  column_03 character varying,  
  column_04 character varying,  
  column_05 character varying,  
  column_06 character varying,  
  column_07 character varying,  
  column_08 character varying,  
  column_09 character varying,  
  column_10 character varying,  
  CONSTRAINT demo_bbb_pk PRIMARY KEY (id)
);

这是我的测试(每个查询运行 3 次):

(1) select id from demo_bbb b ;         -- database query time: ~26ms
(2) select column_10 from demo_bbb b ;  -- database query time: 46ms, 48ms, 51ms
(3) select column_05 from demo_bbb b ;  -- database query time: 37ms, 38 ms, 45ms
(4) select * from demo_bbb b ;          -- database query time: 28ms, 32ms, 37ms

结果: (4) 的平均时间 < (2)、(3) 的时间(32ms < 45 ms)

解释分析:

(1) "Seq Scan on demo_bbb b  (cost=0.00..4425.00 rows=100000 width=6) (actual time=0.008..22.088 rows=100000 loops=1)"
(2) "Seq Scan on demo_bbb b  (cost=0.00..4425.00 rows=100000 width=24) (actual time=0.008..38.702 rows=100000 loops=1)"
(3) "Seq Scan on demo_bbb b  (cost=0.00..4425.00 rows=100000 width=24) (actual time=0.008..32.098 rows=100000 loops=1)"
(4) "Seq Scan on demo_bbb b  (cost=0.00..4425.00 rows=100000 width=232) (actual time=0.007..17.702 rows=100000 loops=1)"

我很想知道发生了什么,但我无法理解。你能给我解释一下这些查询是如何进行的吗?

注意:我使用 SQL Profiler (从 Enterprisedb 下载)跟踪了这些查询

postgresql profiler
  • 1 个回答
  • 406 Views
Martin Hope
Luan Huynh
Asked: 2014-06-18 19:27:35 +0800 CST

pg_column_size(table.*) 和 pg_column_size(table.col1) + pg_column_size (table.col2) 的区别

  • 11

来自PG DOC

pg_column_size(any) :用于存储特定值的字节数(可能已压缩) pg_column_size 显示用于存储任何单个数据值的空间

例子:

select pg_column_size(5::smallint);    -- 2 bytes 
select pg_column_size(5::int);         -- 4 bytes 

使用 pg_column_size 的输入,它可以是列或行,所以我创建了一个测试来检查它。这是我的测试:

我的桌子

CREATE TABLE index_test
(
  id integer NOT NULL,  -- 4  bytes 
  a integer,            -- 4  bytes 
  b integer,            -- 4  bytes 
  CONSTRAINT index_test_id PRIMARY KEY (id)
)

1/ 第一个查询: sum(pg_column_size(table.rows))

with abc as 
(
 select id,a,b
 from index_test where b > 100
)
select pg_size_pretty(sum(pg_column_size(abc.*))) from abc  -- "348 kB", abc.* = record

和查询的解释:

"Aggregate  (cost=427.55..427.56 rows=1 width=24) (actual time=9.171..9.171 rows=1 loops=1)"
"  CTE abc"
"    ->  Seq Scan on index_test  (cost=0.00..180.00 rows=9902 width=12) (actual time=0.039..2.882 rows=9902 loops=1)"
"          Filter: (b > 100)"
"  ->  CTE Scan on abc  (cost=0.00..198.04 rows=9902 width=24) (actual time=0.047..7.151 rows=9902 loops=1)"
"Total runtime: 9.376 ms"

2/ 第二个查询: sum(pg_column_size(table.id)) + sum(pg_column_size(table.a)) + sum(pg_column_size(table.b))

with abc as 
(
 select id, a, b 
 from index_test where b > 100
)
select  pg_size_pretty((sum(pg_column_size(id)))  + (sum(pg_column_size(b))) + (sum(pg_column_size(a))))
from abc  -- "116 kB"

和查询的解释:

"Aggregate  (cost=526.57..526.59 rows=1 width=12) (actual time=10.959..10.959 rows=1 loops=1)"
"  CTE abc"
"    ->  Seq Scan on index_test  (cost=0.00..180.00 rows=9902 width=12) (actual time=0.035..2.780 rows=9902 loops=1)"
"          Filter: (b > 100)"
"  ->  CTE Scan on abc  (cost=0.00..198.04 rows=9902 width=12) (actual time=0.039..5.623 rows=9902 loops=1)"
"Total runtime: 11.173 ms"

3/ 结果:

第一个查询:348 KB

第二个查询:116 KB ( pg_column_size(id) = 39 KB ...)

我认为两个查询都必须返回相同的结果,但是第一个查询的大小 = 3 * 第二个查询的大小,这让我感到困惑。在第一个解释中,“宽度 = 24 字节/行”(而不是 12),我想知道它为什么会增加,我认为这是问题的线索。到目前为止,我无法找到我的问题的明确答案,请帮助我。

postgresql
  • 1 个回答
  • 14218 Views
Martin Hope
Luan Huynh
Asked: 2014-03-21 19:37:01 +0800 CST

如何求和先前的总和,例如 N = (N-1) + (N-2) + ... + 1?

  • 7

我有一个表名“ TABLE_A ( id integer, no integer) ”。

我想通过“id”和当前“no sum of no”=之前的“sum of no”将“no”与 group 相加

这是我的代码:

1/ 创建表和插入数据:

create table table_a (id int, no int);

insert into table_a values(1, 10);
insert into table_a values(1, 20);
insert into table_a values(1, 30);
insert into table_a values(2, 100);
insert into table_a values(2, 200);
insert into table_a values(2, 300);
insert into table_a values(3, 1);
insert into table_a values(3, 2);
insert into table_a values(3, 3);
insert into table_a values(3, 3);

2/ 期望的结果:

id | sum_of_no
--------------
1  | 60
2  | 660
3  | 669

3/ 我的解决方案(好的):

with t_report_code_temp as
(   
select id, sum(no) as t_code
from table_a
group by id 
)
select a.id, sum(b.t_code)
from t_report_code_temp a 
join t_report_code_temp b on b.id <= a.id
group by a.id
order by 1

我的问题:

你能给我更好的解决方法吗?

postgresql join
  • 3 个回答
  • 13053 Views
Martin Hope
Luan Huynh
Asked: 2014-03-12 18:04:54 +0800 CST

函数 quote_nullable(timestamp without time zone) 不是唯一的

  • 1

当我在“enterprisedb 9.2”上运行这个查询时,它引发了一个错误:

select quote_nullable(to_date('09-02-2014 ','dd-MM-yyyy'))::date;

错误:

LINE 1: select quote_nullable(to_date('09-02-2014 ','dd-MM-yyyy'))::...
               ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.
********** Error **********

ERROR: function quote_nullable(timestamp without time zone) is not unique
SQL state: 42725
Hint: Could not choose a best candidate function. You might need to add explicit type casts.
Character: 8

在 Postgresql 9.1 中,上面的查询运行正常。

请告诉我这里发生了什么。为什么函数quote_nullable(timestamp without time zone)不唯一?

postgresql postgresql-9.2
  • 1 个回答
  • 1213 Views
Martin Hope
Luan Huynh
Asked: 2013-08-09 18:35:15 +0800 CST

创建语言 plperl - 错误:无法加载库 plperl.dll

  • 1

当我create language plperl,我得到错误:错误:无法加载库“C:/Program Files/PostgreSQL/9.1/lib/plperl.dll”:找不到指定的模块。

但是在我的电脑中,“plperl.dll”文件存在于“C:/Program Files/PostgreSQL/9.1/lib/...”文件夹中(我不能发布说明性图片,这个论坛需要 >= 10 个信誉)

如果我select * pg_pltemplate,我得到:

-[ RECORD 4 ]-+-------------------------
tmplname      | plperl
tmpltrusted   | t
tmpldbacreate | t
tmplhandler   | plperl_call_handler
tmplinline    | plperl_inline_handler
tmplvalidator | plperl_validator
tmpllibrary   | $libdir/plperl
postgresql postgresql-9.1
  • 1 个回答
  • 9205 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve