Orientdb 简明教程

OrientDB - Indexes

Index 是指向数据库中数据位置的指针。 Indexing 是一个概念,用于快速找到数据而无需搜索数据库中的每条记录。OrientDB 支持四种索引算法和其中的若干类型。

Index is a pointer which points to a location of data in the database. Indexing is a concept used to quickly locate the data without having to search every record in a database. OrientDB supports four index algorithms and several types within each.

四种索引类型是:

The four types of index are −

SB-Tree Index

它提供了一种从其他索引类型中获得的良好功能组合。最好将其用于常规实用工具。它耐用,可进行事务处理并支持范围查询。它是默认索引类型。支持此算法的不同类型插件包括:

It provides a good mix of features available from other index types. Better to use this for general utility. It is durable, transactional and supports range queries. It is default index type. The different type plugins that support this algorithm are −

  1. UNIQUE − These indexes do not allow duplicate keys. For composite indexes, this refers to the uniqueness of the composite keys.

  2. NOTUNIQUE − These indexes allow duplicate keys.

  3. FULLTEXT − These indexes are based on any single word of text. You can use them in queries through the CONTAINSTEXT operator.

  4. DICTIONARY − These indexes are similar to those that use UNIQUE, but in the case of duplicate keys, they replace the existing record with the new record.

Hash Index

它的性能更快,并且磁盘占用量非常小。它耐用、可进行事务处理,但不支持范围查询。它的工作原理类似于 HASHMAP,这使得它在点查找方面更快速并且比其他索引类型消耗更少的资源。支持此算法的不同类型插件包括:

It performs faster and is very light in disk usage. It is durable, transactional, but does not support range queries. It works like HASHMAP, which makes it faster on punctual lookups and it consumes less resources than other index types. The different type plugins that support this algorithm are −

  1. UNIQUE_HASH_INDEX − These indexes do not allow duplicate keys. For composite indexes, this refers to the uniqueness of the composite keys.

  2. NOTUNIQUE_HASH_INDEX − These indexes allow duplicate keys.

  3. FULLTEXT_HASH_INDEX − These indexes are based on any single word of text. You can use them in queries through the CONTAINSTEXT operator.

  4. DICTIONARY_HASH_INDEX − These indexes are similar to those that use UNIQUE_HASH_INDEX, but in cases of duplicate keys, they replace the existing record with the new record.

Lucene Full Text Index

它提供了良好的全文索引,但不能用于对其他类型进行索引。它耐用、可进行事务处理并支持范围查询。

It provides good full-text indexes, but cannot be used to index other types. It is durable, transactional, and supports range queries.

Lucene Spatial Index

它提供了良好的空间索引,但不能用于对其他类型进行索引。它耐用、可进行事务处理并支持范围查询。

It provides good spatial indexes, but cannot be used to index other types. It is durable, transactional, and supports range queries.

Creating Indexes

Create index 是在特定架构上创建索引的命令。

Create index is a command to create an index on a particular schema.

以下语句是创建索引的基本语法。

The following statement is the basic syntax to create an index.

CREATE INDEX <name> [ON <class-name> (prop-names)] <type> [<key-type>]
[METADATA {<metadata>}]

以下是上文中选项的详细信息。

Following are the details about the options in the above syntax.

<name> - 定义索引的逻辑名称。您还可以使用 <class.property> 表示法创建绑定到架构属性的自动索引。<class> 使用架构的类,<property> 使用在类中创建的属性。

<name> − Defines the logical name for the index. You can also use the <class.property> notation to create an automatic index bound to a schema property. <class> uses the class of the schema and <property> uses the property created in the class.

<class-name> - 提供用于为其创建自动索引索引的类的名称。此类必须存在于数据库中。

<class-name> − Provides the name of the class that you are creating the automatic index to index. This class must exist in the database.

<prop-names> - 提供自动索引要索引的属性列表。这些属性必须已存在于架构中。

<prop-names> − Provides the list of properties, which you want the automatic index to index. These properties must already exist in schema.

<type> - 提供要创建的算法和索引类型。

<type> − Provides the algorithm and type of index that you want to create.

<key-type> - 在自动索引中提供可选键值。

<key-type> − Provides the optional key type with automatic indexes.

<metadata> - 提供 JSON 表示。

<metadata> − Provides the JSON representation.

Example

尝试以下查询以创建自动索引,该索引绑定到用户 sales_user 的属性“ID”。

Try the following query to create automatic index bound to the property ‘ID’ of the user sales_user.

orientdb> CREATE INDEX indexforID ON sales_user (id) UNIQUE

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Creating index...
Index created successfully with 4 entries in 0.021000 sec(s)

Querying Indexes

可以使用 select 查询获取索引中的记录。

You can use select query to get the records in the index.

尝试以下查询以检索名为“indexforId”的索引的键。

Try the following query to retrieve the keys of index named ‘indexforId’.

SELECT FROM INDEX:indexforId

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

----+------+----+-----
#   |@CLASS|key |rid
----+------+----+-----
0   |null  |1   |#11:7
1   |null  |2   |#11:6
2   |null  |3   |#11:5
3   |null  |4   |#11:8
----+------+----+-----

Drop Indexes

如果要删除特定索引,可以使用此命令。此操作不会删除链接记录。

If you want to drop a particular index, you can use this command. This operation does not remove linked records.

以下陈述是对一个索引进行删除的基本语法。

The following statement is the basic syntax to drop an index.

DROP INDEX <name>

<name> 提供要删除的索引的名称。

Where <name> provides the name of the index you want to drop.

尝试以下查询以删除用户 sales_user 的名为“ID”的索引。

Try the following query to drop an index named ‘ID’ of user sales_user.

DROP INDEX sales_users.Id

如果成功执行了以上查询,您会获得以下输出。

If the above query is executed successfully, you will get the following output.

Index dropped successfully