簡體   English   中英

我可以使用 oracle 中的 where 子句為查詢創建索引嗎

[英]Can I create the index for a query by using where clause in oracle

在為以下查詢創建索引期間,我遇到了一些問題:

select * 
from table 
where date_col >= '1November 1995' 
  and (   regexp_like(col_name,'\s')
       or regexp_like(col_name,'\s')
      )

有人可以幫忙嗎?

只有當你告訴我們哪些問題...


看起來date_col應該被索引; 這很容易:

create index i1_tab_date on table_name (date_col);

我真誠地希望date_col的數據類型是DATE 如果是這樣,不要將它與字符串進行比較,因為'1November 1995'是一個字符串。 重寫為

where date_col >= date '1995-11-01'

你的兩個正則表達式都是一樣的; 是錯字嗎? 無論如何,這部分涵蓋了基於函數的索引(如果那是困擾您的問題)。 但是,您不能直接使用正則表達式創建它:

SQL> create index i2_tab_name on table_name (regexp_like(col_name, '\s'));
create index i2_tab_name on table_name (regexp_like(col_name, '\s'))
                                        *
ERROR at line 1:
ORA-00904: "REGEXP_LIKE": invalid identifier

但是,您可以改用CASE表達式( \s是一個空白字符,因此可以使用instr函數進行檢查):

SQL> create index i2_tab_name on table_name
  2    (case when instr(col_name, ' ') > 0 then 1 end);

Index created.

然后,您將其用作

where case when instr(col_name, ' ') > 0 then 1 end is not null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM