我有一个正在尝试针对 PostgreSQL 9.2 进行优化的查询:
select coalesce(max(id),0) as m from tbl
Run Code Online (Sandbox Code Playgroud)
它需要永远运行,所以我想我可以将其重写为
select id from tbl order by id desc limit 1
Run Code Online (Sandbox Code Playgroud)
0但如果表中没有行则需要返回。我尝试了几种 case 语句的组合,但它们似乎不起作用。有什么建议么?
计划空表:
Aggregate (cost=11.25..11.26 rows=1 width=4)
-> Seq Scan on tbl (cost=0.00..11.00 rows=100 width=4)
Run Code Online (Sandbox Code Playgroud)
具有 1,190,000 行的表的成本为 58k,但执行计划相同。
postgresql coalesce aggregate-functions postgresql-9.2 postgresql-performance
如果我想以递减精度搜索表中的单行,例如:
SELECT * FROM image WHERE name LIKE 'text' AND group_id = 10 LIMIT 1
Run Code Online (Sandbox Code Playgroud)
如果这没有给我结果,请尝试这个:
SELECT * FROM image WHERE name LIKE 'text' LIMIT 1
Run Code Online (Sandbox Code Playgroud)
当这没有给我结果时,尝试这个:
SELECT * FROM image WHERE group_id = 10 LIMIT 1
Run Code Online (Sandbox Code Playgroud)
是否可以只用一种表达方式来做到这一点?
当我没有两个而是例如三个或更多搜索参数时,也会出现问题。有通用的解决方案吗?当然,当搜索结果按相关性排序时,它会派上用场。
我有一个带有主表的数据库"DB_One",上面t_d_gate_out有8个索引.我创建了另一个带分区的数据库t_d_gate_out(让我们称之为"DB_Two").:它是由年份和月份(子表的例子分区t_d_gate_out09-2013有两个索引)(d _gate_out和每个孩子新列:i_trx_own)
这是我创建和插入子表的功能:
CREATE OR REPLACE FUNCTION ctm_test.gateout_partition_function()
RETURNS trigger AS
$BODY$
DECLARE new_time text;
tablename text;
seqname text;
seqname_schema text;
bulantahun text;
bulan text;
bulan2 text;
tahun text;
enddate text;
result record;
BEGIN new_time := to_char(NEW.d_gate_out,'MM-YYYY');
bulan:=to_char(NEW.d_gate_out,'MM');
bulan2:=extract(month from NEW.d_gate_out);
tahun:=to_char(NEW.d_gate_out,'YYYY');
bulantahun := new_time;
tablename := 't_d_gate_out'||bulantahun;
seqname := 't_d_gate_out'||bulantahun||'_seq';
seqname_schema := 'ctm_test.t_d_gate_out'||bulantahun||'_seq';
PERFORM 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE
c.relkind = 'r' AND …Run Code Online (Sandbox Code Playgroud) 第一次尝试使用JSONB数据类型(从(从JSONB列中的值联接表开始)讨论@Erwin的意见,开始新线程)
两个表(混淆的数据和表名):
1. Discussion table { discussion_id int, contact_id, group_id, discussion_updates jsonb } [has around 600 thousand rows]
2. Authorization table {user_id varchar , auth_contacts jsonb, auth_groups jsonb} [has around 100 thousand rows]
auth_contacts jsonb data has key value pairs data (as example)
- {"CC1": "rr", "CC2": "ro" }
auth_groups jsonb data has key value pairs data (as example)
- {"GRP1": "rr", "GRP2": "ro" }
Run Code Online (Sandbox Code Playgroud)
1-首先,通过Java JDBC在数据库中插入:我正在做的是:
JSONObject authContacts = new JSONObject();
for(each record in data){
authContacts.put(contactKey, contactRight); …Run Code Online (Sandbox Code Playgroud) 我正在尝试确定以下查询是否存在"低成本"优化.我们已经实施了一个系统,"门票"可以获得"积分",因此可以进行排名.为了支持分析类型的查询,我们将每个票证的等级(票证可以绑定)与票证一起存储.
我发现,在规模上,更新此排名非常缓慢.我试图在一组大约20k门票的"门票"上运行下面的场景.
我希望有人可以帮助确定原因并提供一些帮助.
我们在postgres 9.3.6
这是一个简化的票证表架构:
ogs_1=> \d api_ticket
Table "public.api_ticket"
Column | Type | Modifiers
------------------------------+--------------------------+---------------------------------------------------------
id | integer | not null default nextval('api_ticket_id_seq'::regclass)
status | character varying(3) | not null
points_earned | integer | not null
rank | integer | not null
event_id | integer | not null
user_id | integer | not null
Indexes:
"api_ticket_pkey" PRIMARY KEY, btree (id)
"api_ticket_4437cfac" btree (event_id)
"api_ticket_e8701ad4" btree (user_id)
"api_ticket_points_earned_idx" btree (points_earned)
"api_ticket_rank_idx" btree ("rank")
Foreign-key constraints:
"api_ticket_event_id_598c97289edc0e3e_fk_api_event_id" FOREIGN KEY (event_id) …Run Code Online (Sandbox Code Playgroud) sql postgresql correlated-subquery sql-update postgresql-performance
我需要删除一个非常大的表(例如,5米行)的大多数(比方说,90%).该表的另外10%经常被读取,但不写入.
从" 按ID删除数百万行的最佳方式 ",我收集到我应该删除90%我删除的任何索引,以加快进程(除了我用来选择要删除的行的索引) .
从" PostgreSQL锁定模式 ",我看到这个操作将获取ROW EXCLUSIVE整个表的锁定.但是,因为我只是读了另外的10%,这应该没有关系.
那么,在一个命令中删除所有内容是否安全(即DELETE FROM table WHERE delete_flag='t')?我担心如果删除一行失败,触发大量回滚,那么它将影响我从表中读取的能力.批量删除会更明智吗?
postgresql indexing locking transactions postgresql-performance
我有一个表(A 列,B 列)上有一个索引。我正在运行一个如下所示的查询:
SELECT * FROM table WHERE (A, B) IN ((a_1, b_1), (a_2, b_2), ..., (a_5000, b_5000))
Run Code Online (Sandbox Code Playgroud)
这个查询很慢!该计划如下所示:
Bitmap Heap Scan on table
Recheck Cond: (((A = a_1) AND (B = b_1)) OR ((A = a_2) AND (B = b_2)) OR ...
-> BitmapOr
-> Bitmap Index Scan on idx
Index Cond: ((A = a_1) AND (B = b_1))
-> Bitmap Index Scan on idx
Index Cond: ((A = a_2) AND (B = b_2))
...(5000 other Bitmax Index Scan) …Run Code Online (Sandbox Code Playgroud) 描述:这是性能问题的示例演示。
我们首先创建了两个表,启用了行级安全性,还创建了策略。
表定义:
create table sample_schema.sample_table1(ID numeric(38) PRIMARY KEY NOT NULL,
tenant_id VARCHAR(255) NOT NULL,
Description VARCHAR(255)
);
create table sample_schema.sample_table2(ID2 numeric(38) PRIMARY KEY NOT NULL,
tenant_id VARCHAR(255) NOT NULL,
table1_id numeric (38),
Description2 VARCHAR(255)
);
Run Code Online (Sandbox Code Playgroud)
索引创建:
CREATE UNIQUE INDEX sample_table1_idx1 ON sample_schema.sample_table1(tenant_id,id);
Run Code Online (Sandbox Code Playgroud)
启用行级安全性:
ALTER TABLE sample_schema.sample_table1 ENABLE ROW LEVEL SECURITY;
Run Code Online (Sandbox Code Playgroud)
创建角色:
CREATE ROLE tenant_grp_role_p_id;
Run Code Online (Sandbox Code Playgroud)
创建策略:我希望策略选择tenant_id列值具有与登录用户相同角色的数据。
CREATE POLICY Tenant_Roles ON sample_schema.sample_table1 TO tenant_grp_role_p_id USING ((tenant_id) IN ( SELECT rolname FROM pg_roles WHERE pg_has_role( current_user, oid, 'member')));
Run Code Online (Sandbox Code Playgroud)
创建样本数据:
insert into sample_schema.sample_table1 …Run Code Online (Sandbox Code Playgroud) sql postgresql row-level-security database-indexes postgresql-performance
我正在尝试优化 Postgres 中的查询,但没有成功。
这是我的表:
CREATE TABLE IF NOT EXISTS voc_cc348779bdc84f8aab483f662a798a6a (
id SERIAL,
date TIMESTAMP,
text TEXT,
themes JSONB,
meta JSONB,
canal VARCHAR(255),
source VARCHAR(255),
file VARCHAR(255)
);
Run Code Online (Sandbox Code Playgroud)
我有索引id和meta列:
CREATE UNIQUE INDEX voc_cc348779bdc84f8aab483f662a798a6a_id ON voc_cc348779bdc84f8aab483f662a798a6a USING btree(id);
CREATE INDEX voc_cc348779bdc84f8aab483f662a798a6a_meta ON voc_cc348779bdc84f8aab483f662a798a6a USING btree(meta);
Run Code Online (Sandbox Code Playgroud)
此表中有 62k 行。
我试图优化的请求是这样的:
SELECT meta_split.key, meta_split.value, COUNT(DISTINCT(id))
FROM voc_cc348779bdc84f8aab483f662a798a6a
LEFT JOIN LATERAL jsonb_each(voc_cc348779bdc84f8aab483f662a798a6a.meta)
AS meta_split ON TRUE
WHERE meta_split.value IS NOT NULL
GROUP BY meta_split.key, meta_split.value;
Run Code Online (Sandbox Code Playgroud)
在这个查询中,meta 是一个像这样的字典:
{
"Age":"50 to …Run Code Online (Sandbox Code Playgroud) sql postgresql json query-optimization postgresql-performance
这是我正在尝试做的一个简单示例:
CREATE TABLE daily_factors (
factor_date date,
factor_value numeric(3,1));
CREATE TABLE customer_date_ranges (
customer_id int,
date_from date,
date_to date);
INSERT INTO
daily_factors
SELECT
t.factor_date,
(random() * 10 + 30)::numeric(3,1)
FROM
generate_series(timestamp '20170101', timestamp '20210211', interval '1 day') AS t(factor_date);
WITH customer_id AS (
SELECT generate_series(1, 100000) AS customer_id),
date_from AS (
SELECT
customer_id,
(timestamp '20170101' + random() * (timestamp '20201231' - timestamp '20170101'))::date AS date_from
FROM
customer_id)
INSERT INTO
customer_date_ranges
SELECT
d.customer_id,
d.date_from,
(d.date_from::timestamp + random() * (timestamp '20210211' - …Run Code Online (Sandbox Code Playgroud) postgresql ×9
sql ×5
indexing ×2
json ×2
coalesce ×1
java ×1
jsonb ×1
locking ×1
partitioning ×1
sql-update ×1
transactions ×1
union ×1