我正在使用 PostgreSQL,但我认为大多数高端数据库必须具有一些类似的功能,而且,它们的解决方案可能会启发我的解决方案,所以不要考虑这个特定于 PostgreSQL 的解决方案。
我知道我不是第一个尝试解决这个问题的人,所以我认为这里值得一问,但我正在尝试评估建模会计数据的成本,以便从根本上平衡每笔交易。会计数据是仅附加的。此处的总体约束(以伪代码编写)可能大致如下:
CREATE TABLE journal_entry (
id bigserial not null unique, --artificial candidate key
journal_type_id int references journal_type(id),
reference text, -- source document identifier, unique per journal
date_posted date not null,
PRIMARY KEY (journal_type_id, reference)
);
CREATE TABLE journal_line (
entry_id bigint references journal_entry(id),
account_id int not null references account(id),
amount numeric not null,
line_id bigserial not null unique,
CHECK ((sum(amount) over (partition by entry_id) = 0) -- this won't work
);
Run Code Online (Sandbox Code Playgroud)
显然,这样的检查约束永远不会起作用。它按行操作,可能会检查整个数据库。所以它总是会失败并且做起来很慢。
所以我的问题是对这种约束进行建模的最佳方法是什么?到目前为止,我基本上已经研究了两个想法。想知道这些是否是唯一的,或者是否有人有更好的方法(除了将其留给应用程序级别或存储过程)。
让我们考虑以下示例(从 psql 脚本开始):
\c :db_to_run_on
TRUNCATE the_most_important_table;
-- tried to avoid similarities to anything that exists out there
Run Code Online (Sandbox Code Playgroud)
现在,如果它通过命令运行
psql [connection details] -v db_to_run_on=\'dev_database\'
Run Code Online (Sandbox Code Playgroud)
然后它就会运行并且用户很高兴。但是,如果(她)他决定指定-v db_to_run_on=production_database怎么办?(让我们假设这可能发生,就像人们rm -rf / # don't try this at home!!!偶尔运行一样。)希望该表有一个新的备份......
所以问题出现了:如何检查传递给脚本的变量并根据它们的值停止进一步处理?
我是 PostgreSQL 的完全菜鸟,所以我可能遗漏了一些非常明显的东西。
通过终端上的 psql 连接到我的本地数据库。我运行的任何命令来进行数据库更改,甚至一个简单的选择都不做任何事情。没有错误,只是没有结果。
我可以使用 shell 命令 createdb、dropdb 就好了。
有一个在 Ruby on Rails、RefineryCMS 中运行的站点,使用本地 PostgreSQL db,所以我知道该 db 有数据并且正在运行 - 我似乎无法通过 psql 查询或修改它。
我错过了什么?
Mac OS X Lion,第 9.2 页。
我有一个包含 720 万个元组的表,如下所示:
table public.methods
column | type | attributes
--------+-----------------------+----------------------------------------------------
id | integer | not null DEFAULT nextval('methodkey'::regclass)
hash | character varying(32) | not null
string | character varying | not null
method | character varying | not null
file | character varying | not null
type | character varying | not null
Indexes:
"methods_pkey" PRIMARY KEY, btree (id)
"methodhash" btree (hash)
Run Code Online (Sandbox Code Playgroud)
现在我想选择一些值,但查询速度非常慢:
db=# explain
select hash, string, count(method)
from methods
where hash not in
(select hash from nostring) …Run Code Online (Sandbox Code Playgroud) 我对在 PostgreSQL 中设置权限有点困惑。
我有这些角色:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
admin | Superuser, Create role, Create DB, Replication | {}
meltemi | Create role, Create DB | {rails}
rails | Create DB, Cannot login | {}
myapp | | {rails}
Run Code Online (Sandbox Code Playgroud)
和数据库:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------------+--------+----------+-------------+-------------+-------------------
myapp_production | rails | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
...
Run Code Online (Sandbox Code Playgroud)
用户myapp在查询myapp_production数据库添加和删除记录时没有问题。我希望meltemi也能够查询相同的数据库。所以,我创建了一个角色 …
我正在将现有测试环境迁移到 Amazon RDS PostgreSQL。测试框架具有将某些表中的数据重新加载到早期状态的功能。为此,它禁用外键、删除现有数据、加载保存状态并再次启用外键。
目前,测试框架通过禁用所有触发器来禁用外键(当然,这需要超级用户):
alter table tablename disable trigger all;
Run Code Online (Sandbox Code Playgroud)
在 RDS 上,这失败了:
错误:权限被拒绝:“RI_ConstraintTrigger_a_20164”是系统触发器
如何在 Amazon RDS PostgreSQL 中临时禁用外键?
注意:已经提出了类似的问题(RDS 上的 PostgreSQL:如何使用 FK 约束批量导入数据?)但它专门针对离线导入,并且该解决方案也特定于离线导入。
我有一张这样的表:
CREATE TABLE products (
id serial PRIMARY KEY,
category_ids integer[],
published boolean NOT NULL,
score integer NOT NULL,
title varchar NOT NULL);
Run Code Online (Sandbox Code Playgroud)
一个产品可以属于多个类别。category_ids列包含所有产品类别的 id 列表。
典型的查询看起来像这样(总是搜索单个类别):
SELECT * FROM products WHERE published
AND category_ids @> ARRAY[23465]
ORDER BY score DESC, title
LIMIT 20 OFFSET 8000;
Run Code Online (Sandbox Code Playgroud)
为了加快速度,我使用以下索引:
CREATE INDEX idx_test1 ON products
USING GIN (category_ids gin__int_ops) WHERE published;
Run Code Online (Sandbox Code Playgroud)
除非某一类别中的产品太多,否则这会很有帮助。它会快速过滤掉属于该类别的产品,但随后必须以艰难的方式完成排序操作(没有索引)。
已安装的btree_gin扩展允许我像这样构建多列 GIN 索引:
CREATE INDEX idx_test2 ON products USING GIN (
category_ids gin__int_ops, score, title) WHERE published; …Run Code Online (Sandbox Code Playgroud) 我有一个使用 PostgreSQL 表的应用程序。该表非常大(数十亿行)并且有一列是整数。
该integer可高达6个位数,即0-999,999,没有底片。
我想把它改成numeric(6,0).
这是个好主意吗?会numeric(6,0)占用更少的字节吗?性能怎么样(这个表被查询了很多)?
我目前在两个实体之间有一个外键,我想让这种关系以其中一个表的 entityType 为条件。这是表的层次结构,这是通过从子级到父级的FK 引用完成的
Store
/ \
Employees \
TransactionalStores
/ | \
Kiosks | BrickMortars
Onlines
Run Code Online (Sandbox Code Playgroud)
我目前有从员工到商店的 FK 关系
ALTER TABLE Employees ADD CONSTRAINT Employee_Store
FOREIGN KEY (TransStoreId)
REFERENCES TransactionalStores(StoreId)
Run Code Online (Sandbox Code Playgroud)
我想添加条件:
WHERE TransactionalStores.storeType != 'ONLINE_TYPE'
Run Code Online (Sandbox Code Playgroud)
这是可能的还是我必须将 TransactionalStores 子类化为两个新的子类型(例如 PhysicalStores 和 VirtualStores)
我试图在 postgresql 数据库中选择记录,其中用户名不像字符串列表。
SELECT * FROM rails_db WHERE username NOT LIKE 'j%' AND username NOT LIKE '%eepy%';
Run Code Online (Sandbox Code Playgroud)
问题是有很多这样的值。有没有办法创建一个数组并说如下:
SELECT * FROM rails_db WHERE username NOT LIKE ARRAY[my values];
Run Code Online (Sandbox Code Playgroud) postgresql ×10
foreign-key ×2
index ×2
performance ×2
psql ×2
amazon-rds ×1
constraint ×1
group-by ×1
optimization ×1
permissions ×1
role ×1
sorting ×1