表选择算法
下面我使用简单的示例,真实的表更大,我无法将设备存储在一个表中,我无法合并 device1 和 device2 表。
我有三个表:device1、device2、computer(device3)、device1 和 device2 具有相同序列的 id,device3 是计算机。表device3可以与device1和device2连接(多对多),所以我有两个有关系的表:rel1with3和rel2with3。
CREATE SEQUENCE device_1or2;
CREATE SEQUENCE device_3;
CREATE TABLE device1
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device1_pk PRIMARY KEY (d_id));
CREATE TABLE device2
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device2_pk PRIMARY KEY (d_id));
CREATE TABLE computer
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device3_pk PRIMARY KEY (d_id));
CREATE TABLE rel1with3
( d_id_1 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel13_fk_1 FOREIGN KEY (d_id_1) REFERENCES device1 (d_id)
, CONSTRAINT rel13_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_13 ON rel1with3 (d_id_1, d_id_3);
CREATE TABLE rel2with3
( d_id_2 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel23_fk_2 FOREIGN KEY (d_id_2) REFERENCES device2 (d_id)
, CONSTRAINT rel23_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_23 ON rel2with3 (d_id_2, d_id_3);
我想创建有两个参数的存储过程(device1 或 device2 作为第一个参数,计算机作为第二个参数)并添加/删除关系(在关系表中插入/删除行)。
CREATE OR REPLACE PROCEDURE Add_Relation
( COMPUTER NUMBER
, DEVICE NUMBER
, ERR OUT NUMBER
) IS
PRAGMA AUTONOMOUS_TRANSACTION;
device_data VARCHAR2(20);
BEGIN
-- detect which device it is, insert record
-- stored procedure, the same for both device type, BUT uses device data from additional table field
WRITE_LOG(device, device_data, computer);
COMMIT;
EXCEPTION
WHEN OTHERS THEN ERR := TO_CHAR(SQLCODE);
END;
问题是:如何更好地组织这个?使用另一个函数?使用 IF-ELSE?执行速度等如何更快?
Below I'm using simple example, real tables are bigger, I cannot store devices in one table, I can not merge device1 and device2 tables.
I have three tables: device1, device2, computer(device3), device1 and device2 have id from the same sequence, device3 is computer. Table device3 can be connected with device1 and device2 (many-to-many), so I have two tables with relations: rel1with3 and rel2with3.
CREATE SEQUENCE device_1or2;
CREATE SEQUENCE device_3;
CREATE TABLE device1
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device1_pk PRIMARY KEY (d_id));
CREATE TABLE device2
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device2_pk PRIMARY KEY (d_id));
CREATE TABLE computer
( d_id NUMBER, d_data VARCHAR2(20)
, CONSTRAINT device3_pk PRIMARY KEY (d_id));
CREATE TABLE rel1with3
( d_id_1 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel13_fk_1 FOREIGN KEY (d_id_1) REFERENCES device1 (d_id)
, CONSTRAINT rel13_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_13 ON rel1with3 (d_id_1, d_id_3);
CREATE TABLE rel2with3
( d_id_2 NUMBER, d_id_3 NUMBER
, CONSTRAINT rel23_fk_2 FOREIGN KEY (d_id_2) REFERENCES device2 (d_id)
, CONSTRAINT rel23_fk_3 FOREIGN KEY (d_id_3) REFERENCES computer (d_id)
);
CREATE UNIQUE INDEX I_REL_23 ON rel2with3 (d_id_2, d_id_3);
I want to create stored procedure which have two in parameters (device1 or device2 as first parameter, computer as second) and add/remove relation (insert/delete row in relation table).
CREATE OR REPLACE PROCEDURE Add_Relation
( COMPUTER NUMBER
, DEVICE NUMBER
, ERR OUT NUMBER
) IS
PRAGMA AUTONOMOUS_TRANSACTION;
device_data VARCHAR2(20);
BEGIN
-- detect which device it is, insert record
-- stored procedure, the same for both device type, BUT uses device data from additional table field
WRITE_LOG(device, device_data, computer);
COMMIT;
EXCEPTION
WHEN OTHERS THEN ERR := TO_CHAR(SQLCODE);
END;
Question is: HOW better organize this? Using another function? Using IF-ELSE? How is faster for execute etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是尝试插入
rel1with3
,如果失败尝试使用rel2with3
:一些建议:
rel1with3
和rel2with3 上对两个引用列进行索引
(通常您将在(d_id_1, d_id_3)
上进行 PK,因此您需要在d_id_3
上添加一个索引)I would simply try to insert in
rel1with3
, if it fails try withrel2with3
:A few suggestions:
rel1with3
andrel2with3
(usually you will have a PK on(d_id_1, d_id_3)
so you will need an additional index ond_id_3
)我只使用一个序列对象并拥有一个设备表。在设备表中,我会这样设置:
对于类型 3(多对多)的设备,您将拥有parent_device_id 和 child_device_id 的值(这些将是设备 3 连接到的设备)。如果没有任何与此设备相关的设备,则对于给定设备,这些列将为空。
如果有比父/子更好的名称,您可以使用任何合适的名称。这个想法是,您只需将设备存储在一张表中,并相应地映射关系。
Parent_device_id 和 child_device_id 会将外键返回到 id 列上的设备表,这样可以确保您永远不会“孤立”任何设备记录。
您可以在插入设备记录时使用相同的序列对象生成新的 ID。
I would just use one sequence object and have one device table. In the device table, I'd have it like this:
For devices of type 3 (many-to-many) I you would have values for parent_device_id and child_device_id (these would be the devices that device 3 is connected to). If there aren't any devices related to this device, then these columns would be null for the given device.
If there is a better name than parent/child, you can use whatever is proper. The idea is that you just store the devices in one table, and map the relationships accordingly.
parent_device_id and child_device_id would have foreign keys back to the device table on the id column, so that would ensure you never "orphan" any device records.
You can use the same sequence object to generate new id's as you insert device records.