Rechercher dans le manuel MySQL
13.1.20.10 Secondary Indexes and Generated Columns
InnoDB
supports secondary indexes on virtual
generated columns. Other index types are not supported. A
secondary index defined on a virtual column is sometimes
referred to as a “virtual index”.
A secondary index may be created on one or more virtual columns
or on a combination of virtual columns and regular columns or
stored generated columns. Secondary indexes that include virtual
columns may be defined as UNIQUE
.
When a secondary index is created on a virtual generated column, generated column values are materialized in the records of the index. If the index is a covering index (one that includes all the columns retrieved by a query), generated column values are retrieved from materialized values in the index structure instead of computed “on the fly”.
There are additional write costs to consider when using a
secondary index on a virtual column due to computation performed
when materializing virtual column values in secondary index
records during INSERT
and
UPDATE
operations. Even with
additional write costs, secondary indexes on virtual columns may
be preferable to generated stored columns,
which are materialized in the clustered index, resulting in
larger tables that require more disk space and memory. If a
secondary index is not defined on a virtual column, there are
additional costs for reads, as virtual column values must be
computed each time the column's row is examined.
Values of an indexed virtual column are MVCC-logged to avoid
unnecessary recomputation of generated column values during
rollback or during a purge operation. The data length of logged
values is limited by the index key limit of 767 bytes for
COMPACT
and REDUNDANT
row
formats, and 3072 bytes for DYNAMIC
and
COMPRESSED
row formats.
Adding or dropping a secondary index on a virtual column is an in-place operation.
Indexing a Generated Column to Provide a JSON Column Index
As noted elsewhere, JSON
columns cannot be indexed directly. To create an index that
references such a column indirectly, you can define a
generated column that extracts the information that should be
indexed, then create an index on the generated column, as
shown in this example:
- -> c JSON,
- -> );
- Query OK, 0 rows affected (0.28 sec)
- > ('{"id": "1", "name": "Fred"}'), ('{"id": "2", "name": "Wilma"}'),
- > ('{"id": "3", "name": "Barney"}'), ('{"id": "4", "name": "Betty"}');
- Query OK, 4 rows affected (0.04 sec)
- +--------+
- | name |
- +--------+
- | Barney |
- | Betty |
- +--------+
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: jemp
- partitions: NULL
- type: range
- possible_keys: i
- key: i
- key_len: 5
- ref: NULL
- rows: 2
- filtered: 100.00
- *************************** 1. row ***************************
- Level: Note
- Code: 1003
(We have wrapped the output from the last statement in this example to fit the viewing area.)
When you use EXPLAIN
on a
SELECT
or other SQL statement
containing one or more expressions that use the
->
or ->>
operator, these expressions are translated into their
equivalents using JSON_EXTRACT()
and (if
needed) JSON_UNQUOTE()
instead, as shown
here in the output from SHOW
WARNINGS
immediately following this
EXPLAIN
statement:
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: jemp
- partitions: NULL
- type: range
- possible_keys: i
- key: i
- key_len: 5
- ref: NULL
- rows: 2
- filtered: 100.00
- *************************** 1. row ***************************
- Level: Note
- Code: 1003
- json_extract(`test`.`jemp`.`c`,'$.name')
See the descriptions of the
->
and
->>
operators, as well as those of the
JSON_EXTRACT()
and
JSON_UNQUOTE()
functions, for
additional information and examples.
This technique also can be used to provide indexes that
indirectly reference columns of other types that cannot be
indexed directly, such as GEOMETRY
columns.
JSON columns and indirect indexing in NDB Cluster
It is also possible to use indirect indexing of JSON columns in MySQL NDB Cluster, subject to the following conditions:
NDB
handles aJSON
column value internally as aBLOB
. This means that anyNDB
table having one or more JSON columns must have a primary key, else it cannot be recorded in the binary log.The
NDB
storage engine does not support indexing of virtual columns. Since the default for generated columns isVIRTUAL
, you must specify explicitly the generated column to which to apply the indirect index asSTORED
.
The CREATE TABLE
statement
used to create the table jempn
shown here
is a version of the jemp
table shown
previously, with modifications making it compatible with
NDB
:
We can populate this table using the following
INSERT
statement:
Now NDB
can use index i
,
as shown here:
- *************************** 1. row ***************************
- id: 1
- select_type: SIMPLE
- table: jempn
- partitions: p0,p1
- type: range
- possible_keys: i
- key: i
- key_len: 5
- ref: NULL
- rows: 3
- filtered: 100.00
- *************************** 1. row ***************************
- Level: Note
- Code: 1003
- Message: /* select#1 */ select
You should keep in mind that a stored generated column uses
DataMemory
, and that
an index on such a column uses
IndexMemory
.
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-create-table-secondary-indexes.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.