Your cart is currently empty!
Author: alien
-
Khóa học miễn phí OrientDB – Transactions nhận dự án làm có lương
OrientDB – Transactions
Like RDBMS, OrientDB supports transactions ACID properties. A transaction comprises a unit of work performed within a database management system. There are two main reasons to maintain transactions in a database environment.
-
To allow concurrent recovery from failures and keep a database consistent even in case of system failures.
-
To provide isolation between programs accessing a database concurrently.
By default, the database transaction must follow ACID properties such as Atomic, Consistent, Isolated, and Durable properties. But OrientDB is an ACID compliant database, which means it does not contradict or negate the concept ACID, but it changes its perception while handling the NoSQL database. Take a look at how ACID properties work along with NoSQL database.
Atomic − When you do something to change the database the change should work or fail as a whole.
Consistent − The database should remain consistent.
Isolated − If other transaction executions are executing at the same time, then the user will not be able to see the records in concurrent execution.
Durable − If the system crashes (hardware or software), the database itself should be able to take a backup.
Database transaction can be achieved by using Commit and Rollback commands.
Commit
Commit means closing the transaction by saving all changes to the database. Rollback means recover the database state to the point where you opened the transaction.
The following statement is the basic syntax of the COMMIT database command.
COMMIT
Note − You can use this command only after connecting to a particular database and after beginning the transaction.
Example
In this example, we will use the same database named ‘demo’ that we created in an earlier chapter of this tutorial. We will see the operation of commit transaction and store a record using transactions.
You need to first start the transaction using the following BEGIN command.
orientdb {db = demo}> BEGIN
Insert a record into an employee table with the values id = 12 and name = satish.P using the following command.
orientdb> INSERT INTO employee (id, name) VALUES (12, ''satish.P'')
You can use the following command to commit the transaction.
orientdb> commit
If this transaction successfully committed, you will get the following output.
Transaction 2 has been committed in 4ms
Rollback
Rollback means recovering the database state to the point where you opened the transaction.
The following statement is the basic syntax of the ROLLBACK database command.
ROLLBACK
Note − You can use this command only after connecting to a particular database and after beginning the transaction.
Example
In this example, we will use the same database named ‘demo’ that we created in an earlier chapter of the tutorial. We will see the operation of rollback transaction and store a record using transactions.
You have to first start the transaction using the following BEGIN command.
orientdb {db = demo}> BEGIN
Insert a record into an employee table with the values id = 12 and name = satish.P using the following command.
orientdb> INSERT INTO employee (id, name) VALUES (12, ''satish.P'')
You can use the following command to retrieve the records of the table employee.
orientdb> SELECT FROM employee WHERE name LIKE ''%.P''
If this command is executed successfully, you will get the following output.
---+-------+-------------------- # | ID | name ---+-------+-------------------- 0 | 12 | satish.P ---+-------+-------------------- 1 item(s) found. Query executed in 0.076 sec(s).
You can use the following command to Rollback this transaction.
orientdb> ROLLBACK
Check the select query again to retrieve the same record from the Employee table.
orientdb> SELECT FROM employee WHERE name LIKE ''%.P''
If the Rollback is executed successfully, you will get 0 records found in the output.
0 item(s) found. Query executed in 0.037 sec(s).
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí OrientDB – Indexes nhận dự án làm có lương
OrientDB – Indexes
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 −
-
UNIQUE − These indexes do not allow duplicate keys. For composite indexes, this refers to the uniqueness of the composite keys.
-
NOTUNIQUE − These indexes allow duplicate keys.
-
FULLTEXT − These indexes are based on any single word of text. You can use them in queries through the CONTAINSTEXT operator.
-
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
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 −
-
UNIQUE_HASH_INDEX − These indexes do not allow duplicate keys. For composite indexes, this refers to the uniqueness of the composite keys.
-
NOTUNIQUE_HASH_INDEX − These indexes allow duplicate keys.
-
FULLTEXT_HASH_INDEX − These indexes are based on any single word of text. You can use them in queries through the CONTAINSTEXT operator.
-
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 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> − 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> − Provides the name of the class that you are creating the automatic index to index. This class must exist in the database.
<prop-names> − Provides the list of properties, which you want the automatic index to index. These properties must already exist in schema.
<type> − Provides the algorithm and type of index that you want to create.
<key-type> − Provides the optional key type with automatic indexes.
<metadata> − Provides the JSON representation.
Example
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
You can use select query to get the records in the index.
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>
Where <name> provides the name of the index you want to drop.
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
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc