数据类型的不匹配可能会导致索引失效
来源:互联网 发布:linux svn 钩子 同步 编辑:程序博客网 时间:2024/04/28 05:18
简单说明如下:
c1 int not null,
c2 money default 0,
c3 varchar(20),
constraint PK_Test primary key(c1))
go
create index ind_c2_Test on Test(c2)
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
- 数据类型的不匹配可能会导致索引失效
- 导致索引失效的情况
- 数据类型不一致导致的SQL不走索引
- 图:试图播放的文件扩展名与文件格式不匹配。播放该文件可能会导致意外行为...的解决办法
- 所生成项目的处理器架构“MSIL”与引用“***”的处理器架构“x86”不匹配。这种不匹配可能会导致运行时失败。请考虑通过配置管理器...
- 导致索引失效的一些情况
- 导致索引失效的可能情况
- 导致索引失效的一些情况
- listview的子布局如果有HorizontalScrollView、CheckBox 、button,可能会导致onListItemClick点击事件失效。
- 项目中与php后台交接接口 javabean与请求返回的数据类型不匹配导致的出错
- 数据类型不匹配引起的bug
- SpringMVC+Hibernate4 导致事务失效不提交的可能原因
- SpringMVC+Hibernate4 导致事务失效不提交的可能原因
- WPF的RadioButton若界面重载可能会导致RadioButton不可用
- ORACLE 不走索引(失效)的原因以及解决办法
- Where语句设置不当导致索引失效
- 隐式转换导致索引失效
- Where语句设置不当导致索引失效
- 类在运行时是如何构造生成的
- 类在运行时是如何构造生成的
- 每个开发人员现在应该下载的十种必备工具(转)
- Dragging and Dropping DataGrid Columns
- PTF3的修正报告
- 数据类型的不匹配可能会导致索引失效
- 在eclipse3.0中安装pydev0.5插件
- The rainy weekend
- vc+ado动态创建access数据库
- PTF workshop的修正历史
- 集合与通用集合
- 一个人事经理眼中的国人劣根性
- 加菲的经典语录
- Web框架设计与实现(一):Maverick