Skip to content

Commit

Permalink
[docs] secondary and covering indexes backport, systemd cmd for servi…
Browse files Browse the repository at this point in the history
…ce identification. (#18311)

* secondary and covering index backport to stable

* added restart command
  • Loading branch information
aishwarya24 authored Jul 20, 2023
1 parent 69cf6c9 commit 4a30664
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ menu:
weight: 210
aliases:
- /preview/explore/ysql-language-features/indexes-1/
- /preview/explore/indexes-constraints/secondary-indexes/
type: docs
---
<ul class="nav nav-tabs-alt nav-tabs-yb">
Expand Down
14 changes: 14 additions & 0 deletions docs/content/preview/troubleshoot/cluster/recover_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ It is recommended to have a cron or systemd setup to ensure that the YB-TServer

This handles transient failures, such as a node rebooting or process crash due to an unexpected behavior.

If you are using systemd and want to find the names of the services to restart, use the following command:

```sh
sudo systemctl list-units --type=service | grep yb
```

You should see output similar to the following:

```output
yb-controller.service loaded active running Yugabyte Controller
yb-master.service loaded active running Yugabyte master service
yb-tserver.service loaded active running Yugabyte tserver service
```

## Node failure

Typically, if a node has failed, the system automatically recovers and continues to function with the remaining N-1 nodes. If the failed node does not recover soon enough, and N-1 >= 3, then the under-replicated tablets will be re-replicated automatically to return to RF=3 on the remaining N-1 nodes.
Expand Down
135 changes: 135 additions & 0 deletions docs/content/stable/explore/indexes-constraints/covering-index-ycql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: Covering indexes in YugabyteDB YCQL
headerTitle: Covering indexes
linkTitle: Covering indexes
description: Using covering indexes in YCQL
headContent: Explore covering indexes in YugabyteDB using YCQL
image: /images/section_icons/secure/create-roles.png
menu:
stable:
identifier: covering-index-ycql
parent: explore-indexes-constraints
weight: 255
type: docs
---
<ul class="nav nav-tabs-alt nav-tabs-yb">
<li >
<a href="../covering-index-ysql/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL
</a>
</li>
<li >
<a href="../covering-index-ycql/" class="nav-link active">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL
</a>
</li>
</ul>

A covering index is an index that includes all the columns required by a query, including columns that would typically not be a part of an index. This is done by using the INCLUDE keyword to list the columns you want to include.

A covering index is an efficient way to perform `index-only` scans, where you don't need to scan the table, just the index, to satisfy the query.

## Syntax

```cql
CREATE INDEX columnA_index_name ON table_name(columnA) INCLUDE (columnC);
```

For additional information on creating indexes, see [CREATE INDEX](../../../api/ycql/ddl_create_index/).

## Example

{{% explore-setup-single %}}

The following exercise demonstrates how to optimize query performance using a covering index.

1. Create a sample HR keyspace as follows:

```cql
ycqlsh> CREATE KEYSPACE HR;
ycqlsh> USE HR;
```

1. Create and insert some rows into a table `employees` with two columns `id` and `username`

```cql
CREATE TABLE employees (
employee_no integer PRIMARY KEY,
name text,
department text
)
WITH TRANSACTIONS = {'enabled':'true'};
```

```cql
INSERT INTO employees(employee_no, name,department) VALUES(1221, 'John Smith', 'Marketing');
INSERT INTO employees(employee_no, name,department) VALUES(1222, 'Bette Davis', 'Sales');
INSERT INTO employees(employee_no, name,department) VALUES(1223, 'Lucille Ball', 'Operations');
```

1. Run a select query to fetch a row with a particular username

```sql
SELECT name FROM employees WHERE department='Sales';
```

```output
name
-------------
Bette Davis
```

1. Run `EXPLAIN` on select query to show that the query does a sequential scan before creating an index

```cql
EXPLAIN SELECT name FROM employees WHERE department='Sales';
```

```output
QUERY PLAN
----------------------------------
Seq Scan on docs.employees
Filter: (department = 'Sales')
```

1. Optimize the SELECT query by creating an index as follows

```cql
CREATE INDEX index_employees_department ON employees(department);
```

```cql
EXPLAIN SELECT name FROM employees WHERE department='Sales';
```

```output
QUERY PLAN
--------------------------------------------------------------------
Index Scan using index_employees_department on employees
Key Conditions: (department = 'Sales')
```

As the select query includes a column that is not included in the index, the query still reaches out to the table to get the column values.

1. Create a covering index by specifying the username column in the INCLUDE clause as follows:

```sql
CREATE INDEX index_employees_department_nm ON employees(department) include(name);
```

A covering index allows you to perform an index-only scan if the query select list matches the columns that are included in the index and the additional columns added using the INCLUDE keyword.

Ideally, specify columns that are updated frequently in the INCLUDE clause. For other cases, it is probably faster to index all the key columns.

```sql
EXPLAIN SELECT name FROM employees WHERE department='Sales';
```

```output
QUERY PLAN
----------------------------------------------------------------------------
Index Only Scan using HR.index_employees_department_nm on HR.employees
Key Conditions: (department = 'Sales')
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type: docs
YSQL
</a>
</li>
<li >
<a href="../covering-index-ycql/" class="nav-link">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL
</a>
</li>
</ul>

A covering index is an index that includes all the columns required by a query, including columns that would typically not be a part of an index. This is done by using the INCLUDE keyword to list the columns you want to include.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: Secondary indexes with JSONB in YugabyteDB YCQL
headerTitle: Secondary indexes with JSONB
linkTitle: Secondary indexes with JSONB
description: Secondary indexes with JSONB in YugabyteDB YCQL
headContent: Explore secondary indexes with JSONB in YugabyteDB using YCQL
image: /images/section_icons/secure/create-roles.png
menu:
stable:
identifier: secondary-indexes-with-jsonb-ycql
parent: explore-indexes-constraints
weight: 260
type: docs
---

<ul class="nav nav-tabs-alt nav-tabs-yb">
<li >
<a href="../secondary-indexes-with-jsonb-ysql/" class="nav-link">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL
</a>
</li>
<li >
<a href="../secondary-indexes-with-jsonb-ycql/" class="nav-link active">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL
</a>
</li>
</ul>

Secondary indexes can be created with a JSONB datatype column in YCQL. Secondary Indexes in YCQL are global and distributed and similar to tables. So the use of indexes can enhance database performance by enabling the database server to find rows faster. You can create covering indexes as well as partial indexes with JSONB columns.

The following section describes secondary indexes with JSONB column in YCQL using examples.

{{% explore-setup-single %}}

## Create table

Secondary indexes in YCQL can be created only on tables with `transaction = {'enabled':'true'}`. This is due to the use of distributed ACID transactions under the hood to maintain the consistency of secondary indexes in YCQL.

Any attempt to create a secondary index on a table without `transactions = {'enabled':'true'}` results in an error.

Create a `users` table that has a JSONB column as follows:

```cql
CREATE TABLE users(
id int PRIMARY KEY,
first_name TEXT,
last_name TEXT,
email TEXT,
address JSONB
)
WITH transactions = {'enabled':'true'};
```

Insert a few records into `users` table as follows:

```cql
INSERT INTO users(id, first_name, last_name, email, address) VALUES(1, 'Luke', 'Skywalker','lskywalker@yb.com','{"lane":"551 Starwars way","city":"Skyriver","zip":"327"}');
INSERT INTO users(id, first_name, last_name, email, address ) VALUES(2, 'Obi-Wan', 'Kenobi','owkenobi@yb.com','{"lane":"552 Starwars way","city":"Skyriver","zip":"327"}');
INSERT INTO users(id, first_name, last_name, email, address) VALUES(3, 'Yoda',null,'yoda@yb.com','{"lane":"553 Starwars way","city":"Skyriver","zip":"327"}');
INSERT INTO users(id, first_name, last_name, email, address) VALUES(4, 'Din','Djarin','ddjarin@yb.com','{"lane":"554 Starwars way","city":"Skyriver","zip":"327"}');
INSERT INTO users(id, first_name, last_name, email) VALUES(5, 'R2','D2','r2d2@yb.com');
INSERT INTO users(id, first_name, last_name, email) VALUES(6, 'Leia','Princess','lprincess@yb.com');

```

## Create indexes

### Syntax

You can create indexes with JSON column in YCQL using the `CREATE INDEX` statement that has the following syntax:

```cql
CREATE INDEX index_name ON table_name(column->>'attribute');
```

*column* represents a JSONB datatype column of the table. *'attribute'* refers to an attribute of the JSON document stored in the JSONB column.

Create an index on the *'zip'* attribute of the JSON document stored in the address column of the users table as follows:

```cql
CREATE INDEX idx_users_jsonb ON users(address->>'zip');
```

## List the index and verify the query plan

You can use the `DESCRIBE INDEX` command to check the index as follows:

```cql
DESCRIBE INDEX idx_users_jsonb;
```

For additional information regarding the DESCRIBE INDEX command, see [DESCRIBE INDEX](../../../admin/ycqlsh/#describe).

You can also use the `EXPLAIN` statement to check if a query uses an index and determine the query plan before execution.

```cql
EXPLAIN SELECT * FROM users WHERE address->>'zip' = '327';
```

For additional information, see the [EXPLAIN](../../../api/ycql/explain/) statement.

## Covering index and Partial index with JSONB column

You can also create covering and partial indexes with a JSONB column.

### Covering index

A covering index includes all columns used in the query in the index definition. You do this using the `INCLUDE` keyword in the `CREATE INDEX` syntax as follows:

```cql
CREATE INDEX idx_users_jsonb_cov ON users((address->>'zip'))
INCLUDE (first_name,last_name);
```

### Partial index

A partial index is created on a subset of data when you want to restrict the index to a specific condition. You do this using the `WHERE` clause in the `CREATE INDEX` syntax as follows:

``` cql
CREATE INDEX idx_users_jsonb_part ON users (address->>'zip')
WHERE email = 'lskywalker@yb.com';
```

For additional information on the CREATE INDEX statement, see [CREATE INDEX](../../../api/ycql/ddl_create_index/).

## Remove indexes

You can remove an index created with the JSONB datatype column using the `DROP INDEX` statement in YCQL with the following syntax:

```sql
DROP INDEX idx_users_jsonb;
```

For additional information, see [DROP INDEX](../../../api/ycql/ddl_drop_index/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: Secondary indexes with JSONB in YugabyteDB YSQL
headerTitle: Secondary indexes with JSONB
linkTitle: Secondary indexes with JSONB
description: Secondary indexes with JSONB in YugabyteDB YSQL
headContent: Explore secondary indexes with JSONB in YugabyteDB using YSQL
image: /images/section_icons/secure/create-roles.png
menu:
stable:
identifier: secondary-indexes-with-jsonb-ysql
parent: explore-indexes-constraints
weight: 265
type: docs
---

<ul class="nav nav-tabs-alt nav-tabs-yb">
<li >
<a href="../secondary-indexes-with-jsonb-ysql/" class="nav-link active">
<i class="icon-postgres" aria-hidden="true"></i>
YSQL
</a>
</li>
<li >
<a href="../secondary-indexes-with-jsonb-ycql/" class="nav-link">
<i class="icon-cassandra" aria-hidden="true"></i>
YCQL
</a>
</li>
</ul>

Coming soon !!!
Loading

0 comments on commit 4a30664

Please sign in to comment.