如何为动态搜索字符串创建索引
我有一点数据库,仅用于学术目的,我最多有对象表。 我在 Power Designer 中创建了一个实体关系模型 (ERM),并且该程序默认为每个表的序列 ID 创建索引。
- 我想知道如何使用索引 就像在查询中那样。说我会 想要通过 id 查找产品, 但使用它的索引。
- 是否可以进行
选择 来自供应商的值,其中 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.
- 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. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 withWHERE 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.
关于你的观点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
是的,但是Oracle可能会根据统计数据选择不使用索引。您可以通过提示告诉 Oracle 使用索引,但索引是否真正有帮助将取决于您的数据。假设您有此表和索引:
然后执行此选择:
有两种明显的方法可以回答此查询:
假设 T 有 100,000 行,其中只有 5 行符合您的搜索条件。还假设表T占用5000个块,索引T_I占用1000(即仅T大小的20%)。
就读取而言,查询的实际成本为:
显然,在这种情况下索引更好。然而,Oracle 倾向于假设 LIKE 查询将返回 5% 的行(即 5000 行),因此其估计成本(以读取为单位)将为:
因此,在此示例中,Oracle 将进行全表扫描,尽管索引搜索会更快。您可以提示查询使用索引:
但是,请注意,只有当您确定查询在大多数情况下返回的行数少于 5% 时,这才会更好。
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:
You then do this select:
There are two obvious ways this query can be answered:
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:
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:
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:
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.