转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> SyBase >> 正文
Dealing with identity gaps (Freebob)         ★★★★

Dealing with identity gaps (Freebob)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3621 更新时间:2009/4/22 23:09:54
e works in all Sybase versions from 10.0 onwards. Note that table partitioning is only available in version 11.0 and later. 

Proactive reparation of identity gaps 
Designing a database to allow quick reparation of identity gaps is a major improvement compared to the "classical" situation. Still, it might be preferable to ensure identity gaps will never strike an operational application at any time. 
This can be achieved by always running a program directly after server startup, which performs the following actions: 

It inserts an "empty" row in invoices_keytable to obtain the next identity value through @@identity . 


It retrieves the highest existing key value from the invoices table; hopefully, there would be an index to support this query. 


It then compares these two values. If everything is normal, then the difference between these two values is not more than a few units (small gaps can always exist because individual insert operations can have been rolled back). If the difference is bigger than, say 100 units, this means an identity gap exists. A reparation procedure could then be run automatically, which drops and recreates the invoices_keytable. 



Note that this check will cause one invoice number value to be missing from invoices , in case no identity gap exists. If this is not desirable, a variation on this procedure is to always rebuild the invoices_keytable, using the highest invoice number from invoices. 
Assuming these actions are performed directly after server startup, and before any applications are using the database, an identity gap (if present) will not yet have affected the values in the invoices table because no new invoices have been inserted yet. 
The situation will now be corrected immediately before wrong invoice numbers are generated. This ensures that identity gaps do not get a chance to go unnoticed until the first serious application problems start to appear. The downloadable example stored procedure mentioned earlier also works for this situation. 


Conclusion 
It is possible to avoid the problems caused by identity gaps, the risk of which is implied when identity columns are used. Using the two-table design technique described in this article, identity gaps can be repaired quickly and even automatically, in contrast with the much more inefficient classical approach. This results in a significant improvement in application availability at a negligible performance cost. 


存储过程sp_fix_idgap.sql 

/* 
* This script contains everything you need to run a demo of how 
* to use a two-table database design technique that allows 
* identity gaps to be fixed quickly using a stored procedure. 
* This script set up the following objects: 

*    - a table 'invoices', holding application data 
*    - a table 'invoices_keytable', for generating invoice numbers 
*    - a procedure 'sp_fix_idgap_invoices' to repair identity gaps 
*      in the 'invoices' table 
*    - a procedure 'sp_insert_invoice' to simulate the application 
*      inserting invoices 


if not exists(select * from master.dbo.sysdatabases 
             where name = "my_db") 
begin 
print "***************************************************" 
print "*** You should edit this script first !         ***" 
print "*** Change 'my_db' to the name of your database ***" 
print "*** in which you want to run this script.       ***" 
print "***************************************************" 
end 
go 

use my_db 
go 

/* 
* create the application table holding invoice data 
*/ 
if object_id("invoices") !=  NULL 
begin 
   drop table invoices 
end 
go 

create table invoices 
      (invoice_nr   numeric(10,0), 
       customer_nr  int, 
       amount       money) 
go 
create unique index ix1 on invoices(invoice_nr) 
go 
grant all on invoices to public 
go 

/* 
* Create a separate procedure for creating the keytable. 
* This is required because it's not allowed to drop 
* and recreate an object with the same name in one procedure 
*/ 
if object_id("invoices_keytable") !=  NULL 
begin 
   drop table invoices_keytable 
end 
go 

if object_id("sp_create_invoices_keytable") !=  NULL 
begin 
   drop procedure sp_create_invoices_keytable 
end 
go 

create procedure sp_create_invoices_keytable 
/* Copyright (c) 1998 Rob Verschoor/Sypron B.V. */ 
as 
begin 
  create table invoices_keytable 
         (dummy_key numeric(10,0) identity) 

  /* 
   * Partitioning only works in system 11.0 and later 
   * The number of partitions can be increased if there 
   * will be many concurrent inserts. 
   */ 
  alter table invoices_keytable partition 10 
end 
go 
grant execute on sp_create_invoices_keytable to public 
go 

/* 
* create the keytable 
*/ 
exec sp_create_invoices_keytable 
go 

/* 
* create a procedure to simulate application activity: 
* first generate a new invoice number, then insert a new invoice 
*/ 
if object_id("sp_insert_invoice") !=  NULL 
begin 
   drop procedure sp_insert_invoice 
end 
go 

create procedure sp_insert_invoice 
/* Copyright (c) 1998 Rob Verschoor/Sypron B.V. */ 
 @p_customer_nr int, 
 @p_amount money 
as 
begin 
  insert invoices_keytable values&nbs

上一页  [1] [2] [3] [4] [5] [6]  下一页


[办公软件]excel中的VBA中的With语句的使用介绍及实例  [系统软件]OLE with the internet explorer
[Delphi程序]override deal with window closing in database …  [Delphi程序]Building ActiveX Controls with Delphi 3
[VB.NET程序]Socket Programming with VB  [VB.NET程序]Managing Windows with WMI
[Web开发]ASP.NET with C#使用md5,sha1加密初探(10月21日首…  [Web开发]XML with asp.net(1)
[Web开发]Send Email and Attachments with ASP.Net...  [Web开发]Chapter 7. Working with ASP.NET and VB .NET
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台