Oracle查询条件的类型对索引的影响

环境:

order表中的order_code字段类型是varchar2类型,并且已为该字段建立了索引

执行以下sql语句,效率极低,查看F5执行计划发现进行了全表扫描,没有使用索引。

select *
  from order where order_code in(99991105591519,
                                 99991124680834,
                                 99991009460103,
                                 99991031568127,
                                 99991112623988,
                                 99991030562533,
                                 99991124681919,
                                 99991129707738,
                                 99991121668579,
                                 99991121668756,
                                 99991025537956,
                                 99991025541129,
                                 99991112625011,
                                 99991124684937,
                                 99991022524864,
                                 99991005441430,
                                 99991117648880,
                                 99991020515153,
                                 99991019513005,
                                 99991019510299,
                                 99991029559018,
                                 99991010467350,
                                 99991016495058,
                                 99991022525674,
                                 99991022527867,
                                 99991022528321,
                                 99991010467044,
                                 99991013481297,
                                 99991013482759,
                                 99991014487385,
                                 99991017498606,
                                 99991017499561,
                                 99991019513302,
                                 99991126691662,
                                 99991128704352,
                                 99991027547137,
                                 99991118655700,
                                 99991015491626,
                                 99991019512920,
                                 99991028554465,
                                 99991021520570,
                                 99991114636661,
                                 99991104586827,
                                 99991121669675,
                                 99991113632424,
                                 99991031567500,
                                 99991031569757,
                                 99991101572492,
                                 99991105591238,
                                 99991105592870,
                                 99991105593895,
                                 99991105594374,
                                 99991106595115)

所耗时间:
在这里插入图片描述
将流水号使用 ’ ’ 括起来,再次执行查看执行计划,使用了索引。

select *
  from order
 where order_code in ('99991105591519',
                      '99991124680834',
                      '99991009460103',
                      '99991031568127',
                      '99991112623988',
                      '99991030562533',
                      '99991124681919',
                      '99991129707738',
                      '99991121668579',
                      '99991121668756',
                      '99991025537956',
                      '99991025541129',
                      '99991112625011',
                      '99991124684937',
                      '99991022524864',
                      '99991005441430',
                      '99991117648880',
                      '99991020515153',
                      '99991019513005',
                      '99991019510299',
                      '99991029559018',
                      '99991010467350',
                      '99991016495058',
                      '99991022525674',
                      '99991022527867',
                      '99991022528321',
                      '99991010467044',
                      '99991013481297',
                      '99991013482759',
                      '99991014487385',
                      '99991017498606',
                      '99991017499561',
                      '99991019513302',
                      '99991126691662',
                      '99991128704352',
                      '99991027547137',
                      '99991118655700',
                      '99991015491626',
                      '99991019512920',
                      '99991028554465',
                      '99991021520570',
                      '99991114636661',
                      '99991104586827',
                      '99991121669675',
                      '99991113632424',
                      '99991031567500',
                      '99991031569757',
                      '99991101572492',
                      '99991105591238',
                      '99991105592870',
                      '99991105593895',
                      '99991105594374',
                      '99991106595115')

在这里插入图片描述

总结:

查询条件列如果有索引,那么将查询条件列的类型和表定义的类型保持一致。

你可能感兴趣的:(数据库)