如何使用 Oracle 索引

发布于 2024-09-24 09:57:20 字数 336 浏览 5 评论 0原文

我是一名 PHP 开发人员,几乎没有 Oracle 经验,我的任务是使用 Oracle 数据库。

我注意到的第一件事是,这些表似乎没有像我习惯在 MySQL 中看到的那样有自动编号索引。相反,他们似乎从两个字段中创建了一个索引。

例如,我注意到其中一个索引是日期字段和外键 ID 字段的组合。日期字段似乎存储了整个日期和时间戳,因此组合相当独特。

如果索引名称是 PLAYER_TABLE_IDX,我将如何在 PHP 代码中使用该索引?

我想通过此索引引用唯一记录(而不是在 SQL 查询的 WHERE 部分中使用两个 AND 子句)

Oracle/PHP 专家有什么建议吗?

I am a PHP developer with little Oracle experience who is tasked to work with an Oracle database.

The first thing I have noticed is that the tables don't seem to have an auto number index as I am used to seeing in MySQL. Instead they seem to create an index out of two fields.

For example I noticed that one of the indexes is a combination of a Date Field and foreign key ID field. The Date field seems to store the entire date and timestamp so the combination is fairly unique.

If the index name was PLAYER_TABLE_IDX how would I go about using this index in my PHP code?

I want to reference a unique record by this index (rather than using two AND clauses in the WHERE portion of my SQL query)

Any advice Oracle/PHP gurus?

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

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

发布评论

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

评论(2

完美的未来在梦里 2024-10-01 09:57:20

我想通过此索引引用唯一记录(而不是在 SQL 查询的 WHERE 部分中使用两个 AND 子句)

没有办法,您必须使用引用复合主键中的所有列来获取唯一行。

您不能直接在 SQL 查询中使用索引。
在 Oracle 中,您可以使用 提示语法 来建议索引应该使用它,但希望使用索引的唯一方法是在 SELECT、JOIN、WHERE 和 ORDER BY 子句中指定与其关联的列。

我注意到的第一件事是,这些表似乎没有像我习惯在 MySQL 中看到的那样具有自动编号索引。

Oracle(和 PostgreSQL)有所谓的“序列”。它们是与表分离的对象,但用于类似于 MySQL 的 auto_increment 的功能。与 MySQL 的 auto_increment 不同,每个表可以使用多个序列(它们从不关联),并且可以单独控制每个序列。

相反,他们似乎从两个字段中创建了一个索引。

这就是表的设计,与 Oracle 没有什么特别的关系。

但我认为现在是时候解决索引在数据库中的含义与您使用该术语的方式不同的含义了。索引是一个额外的步骤,可以更快地从表中选择数据(但由于维护它们而使 INSERT/UPDATE/DELETE 变慢)。

您所说的实际上称为主键,在本示例中,它被称为复合键,因为它涉及多个列。在这种情况下,其中一列(DATE(将其视为 DATETIME)或外键)可以有重复项。但由于键基于两列,因此两个值的组合使它们成为表中唯一记录的键。

I want to reference a unique record by this index (rather than using two AND clauses in the WHERE portion of my SQL query)

There's no way around that you have to use reference all the columns in a composite primary key to get a unique row.

You can't use an index directly in a SQL query.
In Oracle, you use the hint syntax to suggestion an index that should be used, but the only means of hoping to use an index is by specifying the column(s) associated with it in the SELECT, JOIN, WHERE and ORDER BY clauses.

The first thing I have noticed is that the tables don't seem to have an auto number index as I am used to seeing in MySQL.

Oracle (and PostgreSQL) have what are called "sequences". They're separate objects from the table, but are used for functionality similar to MySQL's auto_increment. Unlike MySQL's auto_increment, you can have more than one sequence used per table (they're never associated), and can control each one individually.

Instead they seem to create an index out of two fields.

That's what the table design was, nothing specifically Oracle about it.

But I think it's time to address that an index has different meaning in a database than how you are using the term. An index is an additional step to make SELECTing data out of a table faster (but makes INSERT/UPDATE/DELETE slower because of maintaining them).

What you're talking about is actually called a primary key, and in this example it'd be called a composite key because it involves more than one column. One of the columns, either the DATE (consider it DATETIME) or the foreign key, can have duplicates in this case. But because of the key being based on both columns, it's the combination of the two values that makes them the key to a unique record in the table.

始终不够爱げ你 2024-10-01 09:57:20

http://use-the-index-luke.com/ 是我的网络书这解释了如何在 Oracle 中使用索引。

这对你的问题来说有点过分了,但是,如果你想了解事情是如何运作的,它可能值得一读。

http://use-the-index-luke.com/ is my Web-Book that explains how to use indexes in Oracle.

It's an overkill to your question, however, it is probably worth reading if you want to understand how things work.

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