數據類型的不匹配可能會導致索引失效

        Sybase和SQL Server在這一點上有所不同,如果條件比較中的數據類型不匹配的話,可能會引起索引失效,導致潛在的Performance問題。
       簡單說明如下:
      
None.gifCreate Table Test(
None.gifc1 
int not null,
None.gifc2 
money default 0,
None.gifc3 
varchar(20),
None.gif
constraint PK_Test primary key(c1))
None.gif
go
None.gif
None.gif
create index ind_c2_Test on Test(c2)
None.gif
go

插入一些數據後,我們可以測試如下:
1> set showplan on
2> go
1> declare @var_int int
2> select @var_int=2
3> select * from Test where c1=@var_int
4> go

QUERY PLAN FOR STATEMENT 1 (at line 1).


    STEP 1
        The type of query is DECLARE.


QUERY PLAN FOR STATEMENT 2 (at line 2).


    STEP 1
        The type of query is SELECT.


QUERY PLAN FOR STATEMENT 3 (at line 3).


    STEP 1
        The type of query is SELECT.

        FROM TABLE
            Test
        Nested iteration.
        Using Clustered Index.
        Index : PK_Test
        Forward scan.
        Positioning by key.
        Keys are:
            c1  ASC
        Using I/O Size 2 Kbytes for data pages.
        With LRU Buffer Replacement Strategy for data pages.

(1 row affected)
 c1          c2                       c3
 ----------- ------------------------ --------------------
           2                   129.14 Hellen

(1 row affected)
我們看到,sybase的執行計劃會使用clustered index來讀取數據。

下面,採用money類型來進行測試
1> declare @var_money money
2> select @var_money=2
3> select * from Test where c1=@var_money
4> go

QUERY PLAN FOR STATEMENT 1 (at line 1).


    STEP 1
        The type of query is DECLARE.


QUERY PLAN FOR STATEMENT 2 (at line 2).


    STEP 1
        The type of query is SELECT.


QUERY PLAN FOR STATEMENT 3 (at line 3).


    STEP 1
        The type of query is SELECT.

        FROM TABLE
            Test
        Nested iteration.
        Table Scan.
        Forward scan.
        Positioning at start of table.
        Using I/O Size 2 Kbytes for data pages.
        With LRU Buffer Replacement Strategy for data pages.

(1 row affected)
 c1          c2                       c3
 ----------- ------------------------ --------------------
           2                   129.14 Hellen

(1 row affected)

我們可以看到,sybase沒有采用索引,而是採用了全表掃描。

實際上,Sybase並不是類型不一致就一定不會使用索引,而是有一個匹配原則,原則上是隻要索引列的類型優先級高於搜索條件的數據類型,就會使用索引。
這個優先級,可以通過查詢系統表master.dbo.systypes.
1> select hierarchy,name from master.dbo.systypes
2> order by 1
3> go
 hierarchy name
 --------- ------------------------------
         1 floatn
         2 float
         3 datetimn
         4 datetime
         5 real
         6 numericn
         7 numeric
         8 decimaln
         9 decimal
        10 moneyn
        11 money
        12 smallmoney
        13 smalldatetime
        14 intn
        15 int
        16 smallint
        17 tinyint
        18 bit
        19 univarchar
        20 unichar
        22 sysname
        22 varchar
        22 nvarchar
        23 char
        23 nchar
        24 timestamp
        24 varbinary
        25 binary
        26 text
        27 image
        99 extended type

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章