如何为动态搜索字符串创建索引

发布于 2024-08-19 15:18:25 字数 357 浏览 5 评论 0原文

我有一点数据库,仅用于学术目的,我最多有对象表。 我在 Power Designer 中创建了一个实体关系模型 (ERM),并且该程序默认为每个表的序列 ID 创建索引。

  1. 我想知道如何使用索引 就像在查询中那样。说我会 想要通过 id 查找产品, 但使用它的索引。
  2. 是否可以进行选择 来自供应商的值,其中 s.name LIKE '%search%' order by s.name 使用索引进行搜索,例如 那?我知道可以创建 名称索引,但用于搜索 我不知道事情是怎样的 工作。

让我说,我确实知道 Oracle 决定何时或是否值得在查询中使用索引,但我可能至少必须尝试在我的 BD 项目中使用索引

I have a little DB, for academic purpose only, and I have object tables at most.
I've created a entity-relationship model (ERM) in Power Designer and the program, by default, creates index for the serial id's for each table.

  1. I want to know how do I use a index
    like that on a query.Say I would
    want to find a product by its id,
    but using its index.
  2. Is it possible to do select
    value(s) from supplierf where s.name
    LIKE '%search%' order by s.name

    using a index to do a search like
    that? I know it's possible to create
    index for the name, but for a search
    like that I don't know how things
    work.

Let me say, that I do know that Oracle decides when or if it's worth using index in a query, but I may have to give, at least, a try on using indexs in my BD project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-08-26 15:18:25

1.
通过将列定义为 PRIMARY KEY(这很可能是您的 id 列),Oracle 会隐式地为此列创建索引。当您使用 WHERE id=123 进行选择时,它很可能会决定使用该索引。您可以在查询中提供 提示 以使 Oracle 使用索引(在大多数情况下) ),但这对您来说不是必需的。

2.
Oracle 不太可能使用 LIKE 索引(除非您知道文本以搜索字符串开头并且可以使用“xyz%”)。有关详细信息,请参阅Tony Andrews 的帖子关于何时以及如何使用索引进行全表扫描。

关于使用文本索引进行 Oracle LIKE 子句搜索的文章应该提供有关处理方法的信息全文搜索。

1.
By defining a column as PRIMARY KEY (that's what your id column most likely is), Oracle implicitely creates an index for this column. It will most likely decide to use that index when you have a select with WHERE id=123). You can provide a hint in your query to make Oracle use the index (in most cases), but that should not be necessary for you.

2.
It is unlikely for Oracle to use an index for LIKE (unless you know that your text starts with the searched string and you can use 'xyz%'). See Tony Andrews' post for more information about when and how to use an index for full table scans.

The article about Oracle LIKE clause searches with text indexes should provide information about a way to handle full text searches.

混浊又暗下来 2024-08-26 15:18:25

关于你的观点1.):我不清楚你的意思:如果你合理地分配索引,你可以使用索引提示来强制使用索引,但让优化器来做是一个更好的主意首先它可以工作,然后,如果您的索引没有被使用,请分析原因(可能是在特定情况下使用索引不是最快的方法)。例如,如果您将按 id 进行的搜索与使用通配符匹配的搜索相结合,优化器可能会决定是否必须进行完整的表扫描(因为您的 '%search%' 术语)表明使用 id 列上的索引没有任何额外的好处。

关于您的观点2.):如果您在搜索词的开头使用通配符匹配,则不太可能使用索引。对于此类搜索,请查看此处的 Oracle 全文语法:

http:// /www.oracle.com/technology/products/text/index.html

Regarding your point 1.): I'm not clear what you mean: if you assign indexes sensibly, you can use index hints to force index usage, but it's a far better idea to let the optimzer do it's work first and then, if your index is not being used, analyse why (it could be that index usage under specific circumstances is not the quickest way). For example, if you are combining a search by id with a search using the wildcard match,the optimizer may decide that, if it has to be a complete table scan anyway (because of your '%search%' term) that there is no added benefit using the index on your id column.

Regarding your point 2.): it is (very) unlikely that an index can be used if you are using a wildcard match at the beginning of your search term. For searches like that, take a look at the Oracle fulltext syntax here:

http://www.oracle.com/technology/products/text/index.html

半边脸i 2024-08-26 15:18:25

是否可以使用索引从supplierf where s.name LIKE '%search%' order by s.name中选择值来进行类似的搜索?我知道可以为该名称创建索引,但对于这样的搜索,我不知道事情是如何工作的。

是的,但是Oracle可能会根据统计数据选择不使用索引。您可以通过提示告诉 Oracle 使用索引,但索引是否真正有帮助将取决于您的数据。假设您有此表和索引:

create table t (id integer primary key, text varchar2(50), other_cols...);
create index t_i on t (text);

然后执行此选择:

select * from t where text like '%something%';

有两种明显的方法可以回答此查询:

  1. 对 T 进行全表扫描 对
  2. T_I 进行全索引扫描,然后对 T_I 中找到的每个结果进行 1 个 ROWID 查找。

假设 T 有 100,000 行,其中只有 5 行符合您的搜索条件。还假设表T占用5000个块,索引T_I占用1000(即仅T大小的20%)。

就读取而言,查询的实际成本为:

  1. 5000 次读取(T)
  2. 1000 次读取(T_I),然后按 ROWID 对 T 进行 5 次读取 = 1005 次读取

显然,在这种情况下索引更好。然而,Oracle 倾向于假设 LIKE 查询将返回 5% 的行(即 5000 行),因此其估计成本(以读取为单位)将为:

  1. 5000 次读取(T)
  2. 1000 次读取(T_I),然后是 5000 次读取T by ROWID = 6000 个读取

因此,在此示例中,Oracle 将进行全表扫描,尽管索引搜索会更快。您可以提示查询使用索引:

select /*+ index(t t_i) */ from t where text like '%something%';

但是,请注意,只有当您确定查询在大多数情况下返回的行数少于 5% 时,这才会更好。

Is it possible to do select value(s) from supplierf where s.name LIKE '%search%' order by s.name using a index to do a search like that? I know it's possible to create index for the name, but for a search like that I don't know how things work.

Yes, but Oracle may choose not to use the index based on statistics. You can tell Oracle to use the index via a hint, but whether the index actually helps will depend on your data. Suppose you have this table and index:

create table t (id integer primary key, text varchar2(50), other_cols...);
create index t_i on t (text);

You then do this select:

select * from t where text like '%something%';

There are two obvious ways this query can be answered:

  1. Full table scan on T
  2. Full index scan on T_I, then 1 ROWID lookup of T per result found in T_I.

Suppose T has 100,000 rows, and only 5 of them match your search criteria. Suppose also that table T occupies 5000 blocks, and the index T_I occupies 1000 (i.e. only 20% of the size of T).

The actual cost of the queries in terms of reads is then:

  1. 5000 reads (of T)
  2. 1000 reads (of T_I) followed by 5 reads of T by ROWID = 1005 reads

Clearly in this case the index is better. However, Oracle tends to assume that the LIKE query will return 5% of the rows (i.e. 5000 rows), so its estimated costs (in reads) will be:

  1. 5000 reads (of T)
  2. 1000 reads (of T_I) followed by 5000 reads of T by ROWID = 6000 reads

Hence in this example, Oracle will go for the full table scan although the index search would be quicker. You could hint the query to use the index:

select /*+ index(t t_i) */ from t where text like '%something%';

However, note that this is only better if you are sure the query will return less than 5% of the rows most of the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文