Rechercher dans le manuel MySQL
20.3.3.6 Create and Drop Indexes
Indexes are used to find documents with specific field values quickly. Without an index, MySQL must begin with the first document and then read through the entire collection to find the relevant fields. The larger the collection, the more this costs. If a collection is large and queries on a specific field are common, then consider creating an index on a specific field inside a document.
For example, the following query will perform better with an index:
mysql-js> db.countryinfo.find("demographics.Population < 100")
...[output removed]
8 documents in set (0.00 sec)
The createIndex()
method creates an index
that you can define as nonunique or unique. Use the
field()
method to chain the fields that
should be indexed. The execute()
method is
required to create or drop an index.
In MySQL, the _id
field is equivalent to a
primary key by default.
Add a Nonunique Index
To create a nonunique index, pass to the
createIndex()
method an index name.
Duplicate index names are prohibited.
In the following example, the first parameter of the
field()
method specifies the Population
field inside the demographics object and the next parameter
indicates that the field should be indexed as an Integer
numeric value. The last parameter indicates whether the field
should require the NOT NULL constraint. If the value is
false
, the field can contain
NULL
values.
mysql-js> db.countryinfo.createIndex("pop").
field("demographics.Population", "INTEGER", false).execute()
Query OK (0.04 sec)
To create a unique index, pass to the
createIndex()
method an index name and the
mysqlx.IndexType.UNIQUE
type. Country
"Name"
is another common field in the
countryinfo
collection to index. In the
following example, "Text(40)"
represents
the number of characters to index and true
indicates that the field cannot contain any
NULL
values.
mysql-js> db.countryinfo.createIndex("name", mysqlx.IndexType.UNIQUE).
field("Name", "TEXT(40)", true).execute()
Query OK (0.04 sec)
To drop an index, pass to the dropIndex()
method the name of the index to drop. For example, you can
drop the “pop” index as follows:
mysql-js> db.countryinfo.dropIndex("pop").execute()
Query OK (0.58 sec)
See Collection Index Management Functions for the full syntax definition.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-mysql-shell-tutorial-javascript-indexes-create.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.