金融其它sql优化

如何优化like '%db2china%'这种谓词的SQL查询

众所周知,这种语句走不了索引,那么我们该如何尽可能优化该语句select          d.aid,          d.name,          d.article from         db2c...显示全部
众所周知,这种语句走不了索引,那么我们该如何尽可能优化该语句
select
         d.aid,
         d.name,
         d.article
from 
        db2china
where
        d.name like '%chao%'
收起
参与28

查看其它 26 个回答drdb2的回答

drdb2drdb2系统工程师se
As I mentioned previously, DB2 can use index in some cases.
Below is a simulation I performed and you can experiment as well:

[db2inst1@q1 ~]$ db2 "create table t like syscat.tables"
DB20000I  The SQL command completed successfully.
[db2inst1@q1 ~]$ db2 "insert into t select * from syscat.tables"
DB20000I  The SQL command completed successfully.
[db2inst1@q1 ~]$
[db2inst1@q1 ~]$ db2 "create index i2 on t (tabname)"
DB20000I  The SQL command completed successfully.
[db2inst1@q1 ~]$ db2 RUNSTATS ON TABLE "DB2INST1".T FOR SAMPLED DETAILED INDEX "DB2INST1"."I2" ;
DB20000I  The RUNSTATS command completed successfully.

[db2inst1@q1 ~]$ db2expln -d sample -q "select owner,status,card from t where tabname like '%EMPLO%'" -g -t
[......]
==================== STATEMENT ==========================================

        Isolation Level          = Cursor Stability
        Blocking                 = Block Unambiguous Cursors
        Query Optimization Class = 5

        Partition Parallel       = No
        Intra-Partition Parallel = No

        SQL Path                 = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
                                   "DB2INST1"


Statement:
  
  select owner, status, card
  from t
  where tabname like '%EMPLO%'


Section Code Page = 1208

Estimated Cost = 97.380775
Estimated Cardinality = 14.577477

Access Table Name = DB2INST1.T  ID = 3,23
|  Index Scan:  Name = DB2INST1.I2  ID = 1
|  |  Regular Index (Not Clustered)
|  |  Index Columns:
|  |  |  1: TABNAME (Ascending)
|  #Columns = 0
|  Skip Inserted Rows
|  Avoid Locking Committed Data
|  Currently Committed for Cursor Stability
|  #Key Columns = 0
|  |  Start Key: Beginning of Index
|  |  Stop Key: End of Index
|  Index-Only Access
|  Index Prefetch: Sequential(2), Readahead
|  Isolation Level: Uncommitted Read
|  Lock Intents
|  |  Table: Intent None
|  |  Row  : None
|  Sargable Index Predicate(s)
|  |  #Predicates = 1
|  |  Insert Into Integer Sorted Temp Table  ID = t1
|  |  |  #Columns = 1
|  |  |  #Sort Key Columns = 1
|  |  |  |  Key 1: (Ascending)
|  |  |  Sortheap Allocation Parameters:
|  |  |  |  #Rows     = 15.000000
|  |  |  |  Row Width = 20
|  |  |  Duplicate Elimination
Integer Sorted Temp Table Completion  ID = t1
List Prefetch Preparation
|  Access Table Name = DB2INST1.T  ID = 3,23
|  |  #Columns = 3
|  |  Skip Inserted Rows
|  |  Avoid Locking Committed Data
|  |  Currently Committed for Cursor Stability
|  |  RID List Fetch Scan
|  |  Fetch Using Prefetched List
|  |  |  Prefetch: 11 Pages
|  |  Lock Intents
|  |  |  Table: Intent Share
|  |  |  Row  : Next Key Share
|  |  Sargable Predicate(s)
|  |  |  #Predicates = 1
|  |  |  Return Data to Application
|  |  |  |  #Columns = 3
Return Data Completion

End of section


Optimizer Plan:

       Operator
         (ID)   
               
        RETURN
         ( 1)  
         |   
        FETCH
        (--)  
       /     \
  RIDSCN  Table:   
   ( 3)   DB2INST1
    |     T        
   SORT
   ( 4)
    |   
  IXSCAN
   ( 5)  
    |     
Index:   
DB2INST1
I2
互联网服务 · 2014-05-15
浏览1617

回答者

drdb2
系统工程师se

drdb2 最近回答过的问题

回答状态

  • 发布时间:2014-05-15
  • 关注会员:1 人
  • 回答浏览:1617
  • X社区推广