Function Based Index in SQL SERVER 2005

前端 未结 1 1561
感情败类
感情败类 2021-01-19 17:54

Please suppose you have a table called BEER_HERE, with two columns:

BEER_CODE VARCHAR(50)
BEER_DATE DATETIME

Please suppose also you have

相关标签:
1条回答
  • 2021-01-19 18:19

    You would need to add a computed column

    Alter Table BEER_HERE Add Column XBEER_DATE As dbo.TRUNCATE_DATE(BEER_DATE)
    

    You can then index it as you'd expect.

    However, your function needs to be deterministic and precise as defined in http://msdn.microsoft.com/en-us/library/ms189292(v=sql.90).aspx. Your function should meet these requirements, but you might need to add With SchemaBinding to the function definition.

    You might also be able to use a view

    Create View V_BEER_HERE As Select BEER_CODE, BEER_DATE, dbo.TRUNCATE_DATE(BEER_DATE) As XBEER_DATE From BEER_HERE
    Create Unique Clustered Index PK_V_BEER_HERE On V_BEER_HERE (BEER_CODE)
    Create Index I_XBEER_DATE On V_BEER_HERE (XBEER_DATE)
    

    Stuff that inserts writes to the table, stuff that reads reads from the view. This depends on BEER_CODE being a primary key.

    SQL Server doesn't have function based indexes the same way Oracle does.

    0 讨论(0)
提交回复
热议问题