简体   繁体   English

在Oracle中排除变量的表?

[英]Table for variable exclusions in Oracle?

Sorry if this explanation is unclear in any points. 很抱歉,如果在任何时候都不清楚此解释。 I'm working in Oracle. 我在甲骨文工作。

I have a large table which is structured with columns for Country, Division, SubDivision, SpendCategory, etc. I need to create potentially flexible exclusion criteria for each country for this large table, and was thinking about creating a second 'exclusions table' like so. 我有一个大表,其中包含“国家”,“部门”,“细分”,“支出类别”等列。我需要为此表为每个国家/地区创建潜在的灵活排除标准,并正在考虑创建第二个“排除表”,如下所示。

 CREATE TABLE EXCLUSIONS

 (COUNTRY VARCHAR(50), EXCLUSIONFIELD VARCHAR(50), FIELDVALUE VARCHAR(50));

My idea was that I could populate this table with rows like 我的想法是我可以在表格中填充如下行

 Mexico - Division - "Central Division"

 Mexico - SubDivision - "Mexico City"

 France - Division - "Paris Division"

... ...

And with this table, automatically filter my table to exclude data where (country=Mexico AND division="central division"), (country=Mexico AND subdivision="Mexico City"), (country=France AND division="paris division"), 并使用此表自动过滤我的表,以排除以下数据:(国家/地区=墨西哥AND分区=“中央部门”),(国家/地区=墨西哥AND分区“墨西哥城””,(国家=法国AND部门=“巴黎部门”) ),

... ...

So I guess I have two questions. 所以我想我有两个问题。 First, is this the best way to do this? 首先,这是最好的方法吗? I like this idea because the exclusion list is potentially long and having a third party maintain a table seems more feasible than manually updating large numbers of where conditions. 我喜欢这个想法,因为排除列表可能很长,并且让第三方维护一个表似乎比手动更新大量的where条件更可行。 Second, how would I write the query to exclude all conditions from the Exclusions table from my main table? 其次,如何编写查询以从主表中排除“排除”表中的所有条件?

What you are doing can work, but the query will look something like this: 您正在执行的操作可以工作,但是查询将类似于以下内容:

select t.*
from t
where not exists (select 1
                  from exclusions e
                  where e.exclusionfield = 'country' and e.value = t.country or
                        e.exclusionfield = 'district' and e.value = t.district or
                        . . .
                  );

This can be a very hard query to optimize. 这可能是很难优化的查询。 You might want to express this using separate subqueries: 您可能要使用单独的子查询来表达这一点:

select t.*
from t
where not exists (select 1
                  from exclusions e
                  where e.exclusionfield = 'country' and e.value = t.country 
                 ) or
      not exists (select 1

from exclusions e where e.exclusionfield = 'district' and e.value = t.district ); 来自排除项e,其中e.exclusionfield ='district'和e.value = t.district);

You should be able to get reasonable performance with an index on exclusions(value, exclusionfield) . 您应该能够通过exclusions(value, exclusionfield)的索引获得合理的性能。

An alternative would be to replicate the columns . 另一种方法是复制列。 . . country , district and so on, removing the exclusionfield column: countrydistrict等,删除exclusionfield列:

select t.*
from t
where exists (select 1
              from exclusions e
              from e.country = t.country or
                   e.district = t.district or
                   . . .
             );

This might also optimize better with separate exists subqueries. 通过单独exists子查询,这也可能会更好地优化。

To exclude data you can use not exist(select 1 from condition_table where ....) 要排除数据,您可以使用不存在(从condition_table中选择1,其中....)
You can also use condition with (country, subdivision) not in(....) 您还可以将条件用于(国家/地区)不在(....)
To filter select with some data, you can define table dynamically using 'with' clause before select... 要使用某些数据过滤选择,您可以在选择之前使用“ with”子句动态定义表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM