Substring-indexing in Oracle

一世执手 提交于 2021-02-07 13:18:25

问题


I just found that our current database design is a little inefficient according to the SELECT queries we perform the most. IBANs are positional coordinates, according to nation-specific formats.

Since we mostly perform JOINs and WHEREs on a precise substring of IBAN columns in some tables, my question is about assigning an index to the substring(s) of a column

Are we forced to add redundant and indexed columns to the table? Ie. add columns NATION_CODE, IBAN_CIN, IT_CIN, IT_ABI, IT_CAB, IT_ACCOUNT (where the IT_ fields are considered only for accounts starting in ITXX) each one with appropriate secondary indexing or is there any special kind of secondary index that can be applied only on a substring of a column?

The first solution could make the DB more complex since IBAN accounts are used all along the DBMS (and, obviously, I have no full control over design).

Thank you

[Edit] Typical query

SELECT * FROM DELEGATIONS WHERE SUBSTR(IBAN, 6, 5) IN (SELECT ABI FROM BANKS WHERE ANY_CONDITION)

Extracts all payment delegations where the target account belongs to any of the banks that match CONDITION. Should be changed to

SELECT * FROM DELEGATIONS WHERE SUBSTR(IBAN, 1, 2) = 'IT' AND SUBSTR(IBAN, 6, 5) IN (SELECT ABI FROM BANKS WHERE ANY_CONDITION)

to make sure that BBAN really holds the bank code in digits [6-11]


回答1:


You're looking for a function based index:

create index ix_substring on TABLE (substr(COLUMN, 4, 9))



回答2:


If you're using Oracle 11g, you could look at using virtual columns and then indexing those. This is pretty much equivalent to René's answer but might provide a bit more formality around the use of the data so that only the correct characters are used.

Virtual columns won't take up any additional space in the database, although any index you create on that virtual column will.



来源:https://stackoverflow.com/questions/8487520/substring-indexing-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!