Where条件导致查询不走索引的情况

创建测试用表:test,test2,test3

SQL> create table test as select object_id, owner, object_name, created from dba_objects;

Table created
SQL >create table test2 as select * from test where owner in ('SYS','OAK');

Table created.

SQL >create index test_ind1 on test(object_id);

Index created.
SQL >create index  test2_ind1 on test2(owner);

SQL >exec dbms_stats.gather_table_stats(user,'test');

PL/SQL procedure successfully completed.


SQL >exec dbms_stats.gather_index_stats(user,'test_ind1');

PL/SQL procedure successfully completed.

SQL >exec dbms_stats.gather_table_stats(user,'test2');

PL/SQL procedure successfully completed.


SQL >exec dbms_stats.gather_index_stats(user,'test2_ind1');

PL/SQL procedure successfully completed.
SQL >exec dbms_stats.gather_table_stats(user,'test3');

PL/SQL procedure successfully completed.

SQL >exec dbms_stats.gather_index_stats(user,'test3_ind1');

PL/SQL procedure successfully completed.

SQL >select count(*) from test;

  COUNT(*)
----------
     61684

SQL >select count(*) from test where object_id is null;

  COUNT(*)
----------
        40

SQL >select count(*),owner from test2 group by owner;

  COUNT(*) OWNER
---------- ------------------------------
     23225 SYS
         2 OAK

SQL >create table test3(object_id varchar2(20), owner varchar2(30));

Table created.

SQL >insert into test3 select object_id,owner from test;

61684 rows created.

SQL >create index test3_ind1 on test3(object_id);

Index created.

情况一:
Where条件中存在不等于操作(<>,!=)
SQL >select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------
SQL_ID  9zfw20r27fb6j, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ * from test2 where wner='OAK'

Plan hash value: 2486100180

------------------------------------------------------------------------------------------
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |            |       |       |     2 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST2      |     1 |    40 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TEST2_IND1 |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OWNER"='OAK')

SQL >select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
SQL_ID  1zpaqum6fznd8, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ * from test2 where owner !='SYS'

Plan hash value: 300966803

---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |       |       |    35 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST2 |     1 |    40 |    35   (3)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OWNER"<>'SYS')


情况二:
Where条件中存在is null 或 is not null

SQL >select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------
SQL_ID  fyqmqv95dy7yx, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ * from test where object_id is null

Plan hash value: 1357081020

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |    90 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST |    40 |  1640 |    90   (3)| 00:00:02 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID" IS NULL)


情况三:
使用函数,而没有基于函数的索引
SQL >select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
SQL_ID  2apc04k69zvww, child number 0
-------------------------------------
select /* gather_plan_statistics */ * from test2 where
lower(owner)='oak'

Plan hash value: 300966803

---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |       |       |    35 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST2 |   232 |  9280 |    35   (3)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(LOWER("OWNER")='oak')


情况四:比较条件类型字段类型不匹配
SQL >select * from table(dbms_xplan.display_cursor);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
SQL_ID  fjj3yqs8dcq7z, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ * from test3 where object_id=60

Plan hash value: 3306317399

---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |       |       |    36 (100)|          |
|*  1 |  TABLE ACCESS FULL| TEST3 |     1 |    11 |    36   (9)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(TO_NUMBER("OBJECT_ID")=60)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7419833/viewspace-675169/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7419833/viewspace-675169/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL模糊查询索引的原因可能有以下点: 1. 查询条件使用了通配:当查询条件中使用了通配(如%或_)作为模糊匹的标识时,MySQL无法利用B-Tree索引进行快速查找,而是需要进行全表扫描匹配所有可能的结果。 2. 字符串前缀模糊查询:如果查询条件是以通配符开头的模糊查询(如WHERE column LIKE '%abc'),MySQL无法使用B-Tree索引进行范围查找,而是需要进行全表扫描。 3. 索引选择性低:索引选择性指的是索引列中不重复的值占总记录数的比例。如果索引列的选择性很低,即大部分记录都具有相同的值,那么MySQL可能会选择进行全表扫描而不是使用索引。 4. 数据类型不匹配:如果查询条件的数据类型与索引列的数据类型不匹配,MySQL无法使用索引进行查询。 5. 索引统计信息不准确:MySQL会根据索引的统计信息来决定是否使用索引。如果统计信息不准确,可能导致MySQL误判索引的选择性,从而选择进行全表扫描。 为了优化模糊查询性能,可以考虑以下方法: 1. 尽量避免在模糊查询中使用通配符,或者将通配符放在查询条件的末尾。 2. 考虑使用全文索引(Full-Text Indexing)来支持模糊查询,全文索引可以提供更高效的文本匹配能力。 3. 确保索引列的数据类型与查询条件的数据类型一致。 4. 更新索引统计信息,可以使用ANALYZE TABLE命令来更新表的统计信息。 5. 考虑优化索引设计,确保索引选择性较高,避免重复值过多的情况。 需要注意的是,MySQL的查询优化是一个综合性的问题,以上只是一些常见的原因和优化方法,具体情况需要根据具体的表结构、查询条件和数据分布来进行分析和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值