通过检查特殊规则集优化 SQL 查询

发布于 2024-09-09 07:28:46 字数 1540 浏览 2 评论 0原文

(首先对我的英语感到抱歉。我不是母语人士)

我在选择数据时遇到问题。

我的问题是我不知道如何优化表“组件”中的 SELECT。据我了解 - 我无法创建任何索引来帮助 Postgres 根据其他表中的一组规则进行选择。

我有以下架构: 组件可以有多个参数和一个所有者。参数和所有者可以添加到任何组。该组件应该出现在多个传送带上。有特殊的过滤规则,基于组件的组和所有者。该规则可以允许组件或阻止它。 规则可以是任意数量,并且可以对它们应用一些附加逻辑。

表“组件”可以包含大量数据。每个传送带应按部分(或按页)处理组件。

要知道我正在这样做: 1.获取传送带ID并将其所有规则选择到临时表中。 2.遍历所有组件并为每个组件调用我的函数,该函数采用 component_id 和 Owner_id 并使用步骤 1 中的临时表来检查规则。

看起来像这样:

SELECT *
FROM component
WHERE check_rules(component_id, owner_id)
LIMIT p_limit OFFSET p_offset

但在这种情况下,Postgres 每次都应该遍历表中的所有记录,这会影响性能。 这种结构应该工作在大数据量的高负载环境中。所以我需要一种方法来优化这种情况或者有能力扩展它。 在这种情况下使用 Postgres 可能不是一个好的解决方案,我需要使用其他一些系统(例如 MapReduce 或 BigTable,但据我所知,我不使用它们)。

请推荐一些东西。 多谢。

如果我被告知不清楚的话,这里是一些sql代码。

CREATE TABLE component(
    component_id int,
    component_name varchar,
    owner_id int);

CREATE TABLE parameter(
    parameter_id int,
    name varchar);

–component can have several parameters
CREATE TABLE component_parameter(
    component_id int,
    parameter_id int);

--tables for grouping parameters and owners 
CREATE TABLE parameter_group(
    group_id int,
    parameter_id int);

CREATE TABLE owner_group(
    group_id int,
    owner_id int)

CREATE TABLE conveyor(
    conveyor_id int,
    conveyor_name varchar)

CREATE TABLE conveyor_rule(
    conveyor_id int,
    rule_id int,
    allow_work boolean)

CREATE TABLE conveyor_rule_item(
    rule_id int,
    parameter_group_id int,
    owner_group_id int)

(First of all sorry for my English. I am not native speaker)

I have a problem with selecting data.

My problem is that I don't know how to optimize SELECT from table "component". As I understand – I cannot create any indexes which help Postgres make select based on set of rules from other table.

I have following schema:
Component can have several parameters and one owner. Parameters and owners can be added to any groups. There is several conveyors where this components should appear. There is special filtration rules, which based on component's groups and owners. This rule can allow component or block it.
Rules may be any number and some additional logic can be applied for them.

Table “component” can contains large amount of data. Each conveyor should process components by portions (or by pages).

For know I am doing so:
1.Take conveyor id and select all it's rules into the temporary table.
2.Go through all components and call my function for each which takes component_id and owner_id and use temporary table from step 1 for checking rules.

look like this:

SELECT *
FROM component
WHERE check_rules(component_id, owner_id)
LIMIT p_limit OFFSET p_offset

But in this case Postgres every time should go through all records in table which makes performance hit.
This structure should work in high load environment with large amount of data. So I need way to optimize this situation or have ability to scale it.
Probably using Postgres in this case is not good solution and I need use some other system (like MapReduce or BigTable, but for know I am not work with them).

Recommend something please.
Thanks a lot.

Here is some sql code if I am told it is not clear.

CREATE TABLE component(
    component_id int,
    component_name varchar,
    owner_id int);

CREATE TABLE parameter(
    parameter_id int,
    name varchar);

–component can have several parameters
CREATE TABLE component_parameter(
    component_id int,
    parameter_id int);

--tables for grouping parameters and owners 
CREATE TABLE parameter_group(
    group_id int,
    parameter_id int);

CREATE TABLE owner_group(
    group_id int,
    owner_id int)

CREATE TABLE conveyor(
    conveyor_id int,
    conveyor_name varchar)

CREATE TABLE conveyor_rule(
    conveyor_id int,
    rule_id int,
    allow_work boolean)

CREATE TABLE conveyor_rule_item(
    rule_id int,
    parameter_group_id int,
    owner_group_id int)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-09-16 07:28:46

我建议去掉 WHERE 语句中的函数和临时表的使用。尝试使用 INNER JOINS 代替它们。如果这是不可能的,至少尝试删除其中一个,例如通过创建临时表并对其进行联接,或者创建一个不使用临时表的函数。

I would suggest to get rid of both the function in the WHERE-statement and the use of temporary tables. Instead of them, try to use INNER JOINS. If this is not possible try at least to take one of them out, for example by creating a temporary table and join against that or create a function that does not use a temporary table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文