《IQ中使用join功能汇总课件》由会员分享,可在线阅读,更多相关《IQ中使用join功能汇总课件(33页珍藏版)》请在金锄头文库上搜索。
1、Administering Adaptive Server IQfor the Data WarehouseJoinsJoins2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 1Module OverviewThis module discusses ad-hoc joins and predefined join indexes. It also explores when and how to create an ASIQ join index.ObjectivesAfter completing this
2、module, you should be able to:nCompare ad-hoc joins to join indexesnCreate a join indexnSynchronize a join index2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 2Join的类型的类型n即席joinsl查询SQL中实现Joinedn使用Join索引的joinl此类型的join会在 磁盘上保存表之间的连接索引l保存两张或者多张表间的连接索引, 包括inner, left outer, 以及right out
3、er类型的索引l提供比即席join更高的查询性能l需要额外的磁盘空间保存join索引2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 3何时使用即席何时使用即席Joins以下情况建议不建立以下情况建议不建立join索引索引n在创建join索引之前,必须先检测使用即席join的性能效果n参与关联的两表数据比率大于10 :1l例如, 其中一表为很小的lookup表lAd-hoc joins recommended due to data explosionn当关联属于自关联(self-joins)n关联属于多对多的关系
4、2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 4何时创建何时创建Join索引索引n在使用即席关联发现性能达不到要求n某些表在查询中经常性的需要关联n拥有足够的磁盘空间n建立连接索引的表数据相对叫稳定n建立join索引的表数据比率小于10 : 1l当比率较大时,基于性能的考虑,建议使用即席joinl出于性能的考虑建议此值小于 5 : 12002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 5使用即席使用即席Joins与与Join索引的比较
5、索引的比较n可以在多表间根据特定的规则建立Join索引2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 6创建创建Join索引索引步骤步骤nStep 1: 创建用于join索引的表CREATE TABLE.nStep 2: 定义表间关系nStep 3: 创建表的索引CREATE INDEX.nStep 4: 将数据装载到表中LOAD TABLE. or INSERT.nStep 5: 创建join索引CREATE JOIN INDEX.nStep 6: 假如需要的话, 同步join索引SYNCHRONIZE JOI
6、N INDEX.2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 7Step 1: 创建表创建表n创建要使用join索引的表l使用CREATE TABLE命令建表l使用Sybase Central建表n参见Module 6, “Creating Tables and Indexes”n例如:CREATE TABLE customer(customer_idintnot null,cust_typechar(4)null,organizationchar(30)null.)2002 Sybase, Inc. and
7、its subsidiaries. All rights reserved.13 - 8Step 2: 确定关联表间的关系确定关联表间的关系n必须定义好关联表之间的关系n类型l一对多(ONEMANY)l多对一 (MANYONE)l一对一 (ONEONE)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 9Join层次层次n所有的ASIQ关联关系必须有对应的关联层次l此层次表明关联的表的连接关系l定义顶层( top)表, 它处于 “多”(MANY)的一方n规则l任何表在层次中只能出现一次l每张表只能有一个出去的流向l
8、层次中所有的表都必须相连l流向的目标表是表关联中的“多”(MANY)的一方2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 10Join层次类型层次类型n线性joinsl每张表都与层次中的上一张表关联, 直到到达顶层表( top)为止l层次中表间都是”一对多“的关系n星形joinsl层次中关联都要涉及中间的表l中间的表即代表“多”方的表, 就是常说的事实表( fact table)l代表“一”方的表即是常说的维表( dimension tables)n对星形或者线形连接,可以用join索引实现,也可以通过即席joi
9、n实现2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 11关联实体关系图关联实体关系图nThis example is used throughout this module2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 12Join层次中的层次中的Top表表norder 是 “Top表” (continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.1
10、3 - 13创建创建Join索引索引Step OverviewnStep 1: Create the tables involved in the join indexCREATE TABLE.nStep 2: Define the relationshipsnStep 3: 创建表的非join索引CREATE INDEX.nStep 4: Load the data into the tablesLOAD TABLE. or INSERT.nStep 5: Create the join indexCREATE JOIN INDEX.nStep 6: Synchronize the join
11、 index, if requiredSYNCHRONIZE JOIN INDEX .2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 14Step 3: 创建表的索引创建表的索引n对表中要用来创建join索引的字段预先创建索引n用来创建joins索引的字段必须首先创建HG或者LF类型的索引n例如:CREATE HG index customer_id_HG on customer(customer_id);CREATE LF index cust_type_LF on customer(cust_type);.(
12、continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 15Step 4: 装载数据装载数据n将数据装载到表中l可以使用LOAD TABLE或者INSERT命令l参见Modules 8, 9, and 10n例如:LOAD TABLE customer(customer_id|.)FROM .;(continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 16创建创建Join索引索引Step OverviewnS
13、tep 1: Create the tables involved in the join indexCREATE TABLE.nStep 2: Define the relationshipsnStep 3: Create the indexes for the tables being joinedCREATE INDEX.nStep 4: Load the data into the tablesLOAD TABLE. or INSERT.nStep 5: 创建 join索引CREATE JOIN INDEX.nStep 6: Synchronize join index, if req
14、uiredSYNCHRONIZE JOIN INDEX.2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 17Step 5: 创建创建Join索引索引n必须是DBA用户或者是表的拥有者,同时拥有 CREATE JOIN INDEX的权限n语法:CREATE JOIN INDEX join-index-nameFOR join-clausen例如:CREATE JOIN INDEX customer_order_joinFOR customer FULL OUTER JOIN orderON customer.cus
15、tomer_id = order.customer_id(continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 18Step 5:创建创建Join索引索引指定指定Join类型类型nJoin类型通常是FULL OUTER; OUTER关键字是可选项n假如想通过同名字段进行关联,使用NATURAL JOINn假如关联通过主外键, 必须建立主外键n在ON关键字后指定两表的关联字段(continued)2002 Sybase, Inc. and its subsidiaries. All rights re
16、served.13 - 19(continued)Step 5:创建创建Join索引索引n例如 (使用natural join):CREATE JOIN INDEX nat_customer_order_joinFOR customer NATURAL FULL OUTER JOIN ordern关联表与被关联表必须拥有同名的字段2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 20Step 5:创建创建Join索引索引n结果:l当创建join索引时, 会在日志文件产生以下信息: Join Index custom
17、er_order_joincreated from the following join relations.:Table NameRelationship:-:1. customer joined to order One Many:The ultimate/top table is order:-(continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 21创建创建Join索引索引Step OverviewnStep 1: Create the tables involved in the joi
18、n indexCREATE TABLE.nStep 2: Define the relationshipsnStep 3: Create the indexes for the tables being joinedCREATE INDEX.nStep 4: Load the data into the tablesLOAD TABLE. or INSERT.nStep 5: Create the join indexCREATE JOIN INDEX.nStep 6: 同步join索引SYNCHRONIZE JOIN INDEX.2002 Sybase, Inc. and its subsi
19、diaries. All rights reserved.13 - 22Step 6: 同步同步Join索引索引n此步确保数据以正确的顺序装载到join索引存放空间n以下情况自动同步:l创建join索引时 l索引层次中top表的数据发生了变化n否则, 必须手工完成同步, 同步会消耗时间l此步工作最好在系统空闲时进行l同步前最好完成所有的非top表的更改 (不要逐次更改,再逐次同步)(continued)2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 23Step 6:同步同步Join索引索引n语法:SYNCHRO
20、NIZE JOIN INDEX join-index-name , join-index-namen假如不指定join索引名, ASIQ会同步所有的join索引l这样做会非常耗时, 最好指定join索引名,逐一将它们单独同步n例如:SYNCHRONIZE JOIN INDEX customer_order_join2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 24多表建多表建Join索引举例索引举例nJoin层次:2002 Sybase, Inc. and its subsidiaries. All right
21、s reserved.13 - 25创建多表创建多表Join索引索引CREATE JOIN INDEX cust_prod_order_joinFOR customer FULL OUTER JOIN orderON customer.customer_id = order.customer_idFULL OUTER JOIN productON order.product_id = product.product_idn假如没在表中创建主外键,缺省认为关联的两表从左到右是 ONEMANY的关系2002 Sybase, Inc. and its subsidiaries. All rights
22、 reserved.13 - 26Join索引的规则索引的规则条件与约束条件与约束n必须是数据库的系统管理员或则关联表的拥有者n第一步和第二步可以同时进行,通过创建表时指定主外键 (define keys) n对非top表的数据更改后,都必须手动同步join索引n当关联表的数据比率大于10 : 1时建议不要建立join索引 (在数据比率小于 5 : 1时更能显示其效率的提高)nJoin索引不可以建立“多对多”的关联关系2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 27估计估计Join索引大小索引大小n使用存储过
23、程语法:sp_iqestjoin table1, #ofrows, table2, #ofrows , relation , iq_page_size n估计用来存放join索引的空间n例如:sp_iqestjoin customer, 10000, order, 30000n必须输入参与关联的所有表的信息2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 28实际实际Join索引大小索引大小n使用脚本语法:sp_iqjoinindexsize (join-index-name)n返回以字节为单位的join索引占用的
24、空间n例如:sp_iqjoinindexsize customer_order_join2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 29删除删除Join索引索引n语法:DROP JOIN INDEX join-index-namen例如:DROP JOIN INDEX customer_order_joinn在删除与join索引相关的表前,必须先删除其join索引2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 30数据更改对数据
25、更改对Join索引影响索引影响n数据的更改是指join索引相关的表的数据更改,而不是join索引本身的数据更改n不可以插入部分字段的记录到其表中,发生此情况时必须这样处理l删除 join索引, 进行部分字段的记录插入操作, 然后重建join索引l不要忘记必须的同步2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 31nSELECT语句决定是否使用join索引l由FROM和WHERE的参数确定l在查询中不需要指出join索引的名称就可以使用它们nASIQ的join索引可以在对其所有表的查询关联中使用,也可以被用于部分表的关联使用Join索引何时被使用索引何时被使用2002 Sybase, Inc. and its subsidiaries. All rights reserved.13 - 32总结总结n首先测试即席查询是否可以满足性能要求.n当满足下列条件时才考虑是否创建join索引:l关联表的数据大小比例小于10 : 1 (建议最好小于5 : 1).l表间不存在多对多的关系.l同样的表关联经常在查询中出现.l用于足够的磁盘空间.