相关疑难解决方法(0)

功能表现

来自 MySQL 背景,其中存储过程性能(旧文章)可用性值得怀疑,我正在评估 PostgreSQL 为我公司的新产品。

我想做的一件事是将一些应用程序逻辑移到存储过程中,所以我在这里询问有关在 PostgreSQL (9.0) 中使用函数的 DO 和 DON'T(最佳实践),特别是关于性能陷阱。

postgresql best-practices plpgsql

63
推荐指数
4
解决办法
4万
查看次数

空间索引可以帮助“范围-排序-限制”查询吗

问这个问题,特别是针对 Postgres,因为它对 R 树/空间索引有很好的支持。

我们有下表,其中包含单词及其频率的树结构(嵌套集模型):

lexikon
-------
_id   integer  PRIMARY KEY
word  text
frequency integer
lset  integer  UNIQUE KEY
rset  integer  UNIQUE KEY
Run Code Online (Sandbox Code Playgroud)

和查询:

SELECT word
FROM lexikon
WHERE lset BETWEEN @Low AND @High
ORDER BY frequency DESC
LIMIT @N
Run Code Online (Sandbox Code Playgroud)

我认为覆盖索引(lset, frequency, word)会很有用,但我觉得如果范围内的lset值太多,它可能表现不佳(@High, @Low)

(frequency DESC)有时,当使用该索引的搜索早期产生@N与范围条件匹配的行时,一个简单的索引也可能就足够了。

但似乎性能在很大程度上取决于参数值。

有没有办法让它快速执行,不管范围(@Low, @High)是宽还是窄,也不管高频词是否幸运地在(窄)选择的范围内?

R-tree/空间索引有帮助吗?

添加索引,重写查询,重新设计表,没有限制。

postgresql performance index database-design query-performance

28
推荐指数
2
解决办法
4312
查看次数

如何对两列或更多列进行条件排序

在 MS SQL Server 2005 中,我正在编写一个带有条件排序的查询,我的问题是我不知道如何使用两列进行条件排序?

如果我写了这样的代码,它就可以正常工作

select
    *
from 
    table
order by 
    case @pkr 
           when 'kol' then kol
           when 'nci' then nci
    end
Run Code Online (Sandbox Code Playgroud)

我不知道如何对两列或更多列进行条件排序

select
    *
from 
    table
order by 
    case @pkr
        when 'KOL-NCI' then kol,nci
        when 'kol-MPCI' then kol,mpci
    end
Run Code Online (Sandbox Code Playgroud)

有一个制作动态 TSQL 并使用的想法,sp_executesql但我仍在寻找更好的想法?

sql-server order-by

11
推荐指数
2
解决办法
6万
查看次数

使用多个表中的列提高 order by 的性能

使用 PostgreSQL 8.4,我试图使用 order by 和两个表的索引列查询两个包含 100 万条记录的表,并且我正在失去性能(1 列需要 30 毫秒,两列需要 5 分钟)。例如:

select r.code, r.test_code, r.sample_code, s.barcode, s.registry_date
from requests r
inner join samples s on (s.code = r.sample_code)
order by s.barcode  asc , r.code asc
limit 21;
Run Code Online (Sandbox Code Playgroud)

表信息:

CREATE TABLE public.samples (
  code BIGINT NOT NULL,
  barcode VARCHAR(40) NOT NULL,
  registry_date TIMESTAMP WITH TIME ZONE NOT NULL,
  CONSTRAINT samples_pkey PRIMARY KEY(code)
);

CREATE INDEX idx_samp_barcode ON public.samples (barcode);
CREATE INDEX idx_samp_barcode_code ON public.samples (barcode, code);
CREATE INDEX …
Run Code Online (Sandbox Code Playgroud)

postgresql performance postgresql-8.4 recursive postgresql-performance

6
推荐指数
1
解决办法
2237
查看次数

使用小 LIMIT 优化查询,对一列进行谓词并按另一列排序

我使用的是 Postgres 9.3.4,我有 4 个查询,它们的输入非常相似,但响应时间却大不相同:

查询#1

EXPLAIN ANALYZE SELECT posts.* FROM posts
WHERE posts.source_id IN (19082, 19075, 20705, 18328, 19110, 24965, 18329, 27600, 17804, 20717, 27598, 27599)
AND posts.deleted_at IS NULL
ORDER BY external_created_at desc
LIMIT 100 OFFSET 0;
                                                                                 QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.43..585.44 rows=100 width=1041) (actual time=326092.852..507360.199 rows=100 loops=1)
   ->  Index Scan using index_posts_on_external_created_at on posts  (cost=0.43..14871916.35 rows=2542166 width=1041) (actual time=326092.301..507359.524 rows=100 loops=1)
         Filter: (source_id = ANY ('{19082,19075,20705,18328,19110,24965,18329,27600,17804,20717,27598,27599}'::integer[]))
         Rows Removed by Filter: 6913925
 Total runtime: 507361.944 ms
Run Code Online (Sandbox Code Playgroud)

查询#2

EXPLAIN …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index optimization postgresql-9.3 postgresql-performance

5
推荐指数
1
解决办法
895
查看次数