免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1503 | 回复: 0
打印 上一主题 下一主题

如何用一个SQL就生成创建所有索引的脚本? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-09 09:49 |只看该作者 |倒序浏览
如果一个索引的索引列超过了4个列,那么这个索引本身就不是一个好的索引,不是一个合格的优化的索引,所以我们在生成索引脚本的时候,根本就不需要考虑超过4列的情况:
select lower('create '||decode(x.uniqueness,'UNIQUE','unique',' ')||' index '||x.index_name||' on '||x.table_name||'('||y.fields||') tablespace '||tablespace_name||' pct_free '||to_char(pct_free)||';')
from User_indexes x,
(select a.index_name,replace(replace(replace(a.column_name||','||b.column_name||','||c.column_name||','||d.column_name||']',',,,]',''),',,]',''),',]','') fields
from User_ind_columns a,
   (select index_name,column_name from User_ind_columns where column_position=2) b,
   (select index_name,column_name from User_ind_columns where column_position=3) c,
   (select index_name,column_name from User_ind_columns where column_position=4) d
where a.column_position=1
and a.index_name=b.index_name(+)
and a.index_name=c.index_name(+)
and a.index_name=d.index_name(+)
) y
where x.index_name=y.index_name
order by x.table_name;

结果:
1        create unique index pk_attachment on attachment(attachmentid) tablespace dmsindex pct_free 10;
2        create   index authority_entityid on authority(entityid) tablespace dmsindex pct_free 10;
3        create unique index pk_authority on authority(authorityid) tablespace dmsindex pct_free 10;
4        create   index authority_idx1 on authority(privlistid) tablespace dmsindex pct_free 10;
5        create unique index authority_idx2 on authority(entitytype,entityid,privlistid) tablespace dmsindex pct_free 10;
6        create   index authority_idx3 on authority(sourceauthorityid) tablespace dmsindex pct_free 10;
7        create unique index pk_batchmaster on batchmaster(batchmasterid) tablespace users pct_free 10;
8        create   index billing_idx2 on billing(partnerid) tablespace dmsindex pct_free 10;
9        create   index billing_idx3 on billing(relatedtrantype) tablespace dmsindex pct_free 10;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP