newid() 肯定会起作用。但也会产生“碎片化”的价值观。
这可能对您的需求有益或有害(将 guid 视为主键)。
这是我不久前写的一个程序,用于处理碎片的“有点帮助”。
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[uspNewSequentialUUIDCreateSingle]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[uspNewSequentialUUIDCreateSingle]
GO
/*
--START TEST
declare @returnCode int
declare @ReturnUUID uniqueidentifier
EXEC @returnCode = dbo.uspNewSequentialUUIDCreateSingle @ReturnUUID output
print @ReturnUUID
print '/@returnCode/'
print @returnCode
--loop test,,,loop exists for TESTING only fyi
declare @counter int
select @counter = 1000
while @counter > 0
begin
EXEC @returnCode = dbo.uspNewSequentialUUIDCreateSingle @ReturnUUID output
print @ReturnUUID
select @counter = @counter - 1
end
--END TEST CODE
*/
CREATE PROCEDURE [dbo].[uspNewSequentialUUIDCreateSingle] (
@ReturnUUID uniqueidentifier output --return
)
AS
--//You can use NEWSEQUENTIALID() to generate GUIDs to reduce page contention at the leaf level of indexes.
SET NOCOUNT ON
-- declare @ReturnUUID uniqueidentifier
declare @t table ( id int , uuid uniqueidentifier default newsequentialid() )
insert into @t ( id ) values (0)
select @ReturnUUID = uuid from @t
SET NOCOUNT OFF
GO
GRANT EXECUTE ON dbo.uspNewSequentialUUIDCreateSingle TO public
GO