SQL Tips1

1. Optimizer Goals :

  (1). First_rows: 
   (a). Used for applications
   (b). The first row are delivered fast to the application screen
   (c). It takes longer until all rows are fetched

  (2). All_rows:
   (a). Used for batch jobs
   (b). Faster to fetch all rows
   (c). Longer to deliver the first row

2. A database sequence is very fast (values are pre-generated in a memory cache)

3. Don’t update, if you don’t change anything!
   OLD: UPDATE T1 R SET STATUS=’1’ 
             WHERE NOT EXISTS (SELECT ‘FOUND’ FROM T2 OL WHERE OL.ID=R.ID);
   NEW: UPDATE T1 R SET STATUS=’1’
              WHERE COALESCE ( R.STATUS, ‘#’)<>’1’ 
              AND 
              NOT EXISTS(SELECT ‘FOUND’ FROM T2 OL WHERE OL.ID=R.ID);
  Note:  COALESCE returns the first non-null expr in the expression list. You must specify at 
              least two expressions. If all occurrences of expr evaluate to null, then the function 
              returns null.
             
4. Bitmap indexes should not be used if two transactions update/insert rows with identical values 

   on the index column. (Contention issue: no row level locking here). If that is no issue, a 
   bitmap can be used if bitmap index rule-of-thumb: (number of distinct values)<10.000 or 
   (number of distinct values)/(number of all values)<0.01

5. Bitmap indexes can be used in combination:
   SELECT * FROM T WHERE T.COL1=:X AND T.COL2=:Y; --bitmap index on col1 and col2 
   can be merged (bit-and)
   If we had normal indexes on col1 and col2, only one of them could be used or a (costly) 
   bitmap conversion is performed.

6. SELECT col_1, ..., col_n FROM T1
   WHERE name LIKE ‘Ham%’  --Index on name is used
   AND ROWNUM<2001
   ORDER BY name   --Index on name is used

7. SELECT col_1, ..., col_n FROM T1
    WHERE name LIKE ‘Ham%’  --Index on name is used
    AND ROWNUM<2001
    ORDER BY place   --Can’t use index on place (because the condition in the where clause is name)

8. For large result set, the order-by should only be used on columns that are also selective in the where-clause

9. How can we search efficiently for ‘%Nagel%’?
    Solution1: 
    RAM: Pin the index on table.name into the RAM. Database still needs to scan the complete index, but it is in RAM
    Solution2:
    Do-it-yourself-tokenize: Create a new table with all possible prefixes: ‘K’,’u’, ..., ‘N’,’a’,’g’,’e’
    Solution3:
    Oracle Intermedia Indexes: Can also be used for fuzzy searches. Different index types available

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值