简体   繁体   English

在链接表上创建分区视图

[英]Create partitioned view on linked tables

I'm learning partitioning tables in SQL Server and I got stuck with this problem: I have 2 linked tables - parent and child. 我正在学习SQL Server中的分区表,并且陷入了这个问题:我有2个链接表-父级和子级。 One of them (or maybe even both) is a partitioned table. 其中之一(或什至两者)都是分区表。 When I'm implementing a partitioned view do I need to include 2 identical columns in it - the one that references parental table (from child) and the primary key that is being referenced (from parent)? 当我实现分区视图时,是否需要在其中包含2个相同的列-引用父表(来自子级)和被引用的主键(来自父级)的一列?

I'm having troble with it since MSDN says: 自从MSDN说:

Column Rules: 列规则:

All columns in each member table must be included in the select list. 每个成员表中的所有列都必须包含在选择列表中。 SELECT * FROM is acceptable syntax. SELECT * FROM是可接受的语法。

But views should make the representation of (linked) tables easier, so not all the columns should be included in view. 但是视图应该使(链接的)表的表示更加容易,因此并非所有列都应包含在视图中。

And in my case, according to MSDN I have to include all the columns of both tables into a view (and 2 of them would be identical). 就我而言,根据MSDN,我必须将两个表的所有列都包含在视图中(其中两个是相同的)。 It seems to me as not very logical solution. 在我看来,这不是很合乎逻辑的解决方案。

For example: 例如:

Database 1: 数据库1:

create table manufacturer 
(
    id                 int,
    manufacturer_name  varchar(35),

    PRIMARY KEY (id),
    CONSTRAINT CHK_manufacturer_id
        CHECK (id < 1000)
);

create table product 
(
    pid           int,
    product_name  varchar(35),
    mid           int,

    PRIMARY KEY (pid),
    CONSTRAINT CHK_product_pid
        CHECK (pid < 1000),
    CONSTRAINT FK_product_mid
        FOREIGN KEY (mid)
        REFERENCES manufacturer(id)
);

Database 2: 数据库2:

Same tables with CHECK constraints (id >= 1000)

View: 视图:

create view dist_view as
    select * 
    from db1.product p1
    inner join db1.manufacturer m1 on p1.mid = m1.id

    UNION ALL

    select * 
    from db2.product p2
    inner join db2.manufacturer m2 on p2.mid = m2.id

So the view with look like 所以视图看起来像

pid | prod_name | mid | id | manufact_name

and mid = id. 中= ID。

In my opinion a view like this contradicts the main advantage of using views - simple representation of tables. 在我看来,这样的视图与使用视图的主要优势(表的简单表示)相矛盾。 So I would like to have a view like this: 所以我想有一个这样的观点:

(pid) | prod_name | manufact_name

How do I solve this? 我该如何解决?

First, you probably should not bother learning about partitioned views. 首先,您可能不应该学习分区视图。 The correct way to do partitioning is using partitioned tables, rather than views. 进行分区的正确方法是使用分区表,而不是视图。

This is repeated in the documentation . 文档中重复此步骤。 For instance: 例如:

Note 注意

The preferred method for partitioning data local to one server is through partitioned tables. 对一台服务器本地数据进行分区的首选方法是通过分区表。 For more information, see Partitioned Tables and Indexes. 有关更多信息,请参见分区表和索引。

(And I note that your queries are all on one server.) (我注意到,您的查询全部在一台服务器上。)

Second, a partitioned view is created by union all on base tables. 其次,通过union all 表来创建分区视图。 You are using join s, so that is just a regular view. 您正在使用join ,所以这只是一个常规视图。

I would suggest that you re-evaluate your data structures and think more about partitioned tables than partitioned views. 我建议您重新评估数据结构,并多考虑分区表而不是分区视图。

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

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