SQL Server case insensitive collation

前端 未结 5 1714
忘了有多久
忘了有多久 2021-02-07 22:29

What are the benefits/drawbacks of using a case insensitive collation in SQL Server (in terms of query performance)?

I have a database that is currently using a case-ins

5条回答
  •  天涯浪人
    2021-02-07 22:54

    I can't find anything to confirm whether properly constructed queries work faster on a case-sensitive vs case-insensitive database (although I suspect the difference is negligible), but a few things are clear to me:

    1. If your business requirements don't ask for it, you are putting yourself up to a lot of extra work (this is the crux of both HLGEM and Damien_The_Unbeliever's answers).
    2. If your business requirements don't ask for it, you are setting yourself up for a lot of possible errors.
    3. Its way too easy to construct poorly performing queries in a case-insensitive database if a case sensitive lookup is required:

    A query like:

    ... WHERE UPPER(GivenName) = 'PETER'
    

    won't use an index on GivenName. You would think something like:

    ... WHERE GivenName = 'PETER' COLLATE SQL_Latin1_General_CP1_CS_AS
    

    would work better, and it does. But for maximum performance you'd have to do something like:

    ... WHERE GivenName = 'PETER' COLLATE SQL_Latin1_General_CP1_CS_AS
        AND GivenName LIKE 'PETER'
    

    (see this article for the details)

提交回复
热议问题