MySQL 5.7-8.8.1 Optimizing Queries with EXPLAIN

Depending on the details of your tables, columns, indexes, and the conditions in your WHERE clause, the MySQL optimizer considers many techniques to efficiently perform the lookups involved in an SQL query.

根据表、列、索引和WHERE子句中的条件的详细信息,MySQL优化器考虑许多技术来有效地执行SQL查询中涉及的查找。

A query on a huge table can be performed without reading all the rows; a join involving several tables can be performed without comparing every combination of rows.

在大型表上执行查询时,不需要读取所有的行;涉及多个表的连接可以在不比较每个行组合的情况下执行。

The set of operations that the optimizer chooses to perform the most efficient query is called the “query execution plan”, also known as the EXPLAIN plan.

优化器选择用于执行最有效查询的操作集称为“查询执行计划”,也称为EXPLAIN计划。

Your goals are to recognize the aspects of the EXPLAIN plan that indicate a query is optimized well, and to learn the SQL syntax and indexing techniques to improve the plan if you see some inefficient operations.

您的目标是识别EXPLAIN计划中表明查询优化良好的方面,并学习SQL语法和索引技术,以便在看到一些低效操作时改进计划。

The EXPLAIN statement provides information about how MySQL executes statements:

EXPLAIN语句提供了MySQL如何执行语句的信息:

  • EXPLAIN works with SELECTDELETEINSERTREPLACE, and UPDATE statements.

  • EXPLAIN 可以适用于SELECT、DELETE、INSERT、REPLACE和UPDATE语句

  • When EXPLAIN is used with an explainable statement, MySQL displays information from the optimizer about the statement execution plan. That is, MySQL explains how it would process the statement, including information about how tables are joined and in which order. For information about using EXPLAIN to obtain execution plan information, see Section 8.8.2, “EXPLAIN Output Format”.

  • 当EXPLAIN与一个可解释语句一起使用时,MySQL会显示来自优化器的关于语句执行计划的信息。也就是说,MySQL解释了它将如何处理语句,包括关于表如何连接以及连接顺序的信息。有关使用EXPLAIN获取执行计划信息的信息, see Section 8.8.2, “EXPLAIN Output Format”.

  • When EXPLAIN is used with FOR CONNECTION connection_id rather than an explainable statement, it displays the execution plan for the statement executing in the named connection. See Section 8.8.4, “Obtaining Execution Plan Information for a Named Connection”.

  • 当EXPLAIN与FOR CONNECTION connection_id而不是一个可解释语句一起使用时,它将显示在指定连接中执行的语句的执行计划。

  • For SELECT statements, EXPLAIN produces additional execution plan information that can be displayed using SHOW WARNINGS. See Section 8.8.3, “Extended EXPLAIN Output Format”.

  • 对于SELECT语句,EXPLAIN生成额外的执行计划信息,可以使用SHOW WARNINGS显示这些信息。

  • EXPLAIN is useful for examining queries involving partitioned tables. See EXPLAIN.

  • EXPLAIN对于检查涉及分区表的查询很有用。

  • The FORMAT option can be used to select the output format. TRADITIONAL presents the output in tabular format. This is the default if no FORMAT option is present. JSON format displays the information in JSON format.

  • FORMAT选项可用于选择输出格式。TRADITIONAL以表格形式显示输出。如果没有FORMAT选项,这是默认设置。JSON格式以JSON格式显示信息。

With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows.

在EXPLAIN的帮助下,您可以看到应该在哪些地方向表添加索引,以便通过使用索引查找行来更快地执行语句。

You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order.

还可以使用EXPLAIN检查优化器是否以最佳顺序连接表。

To give a hint to the optimizer to use a join order corresponding to the order in which the tables are named in a SELECT statement, begin the statement with SELECT STRAIGHT_JOIN rather than just SELECT.

要提示优化器使用与表在SELECT语句中命名的顺序相对应的连接顺序,请使用SELECT STRAIGHT_JOIN开始语句,而不仅仅是SELECT。

(See Section 13.2.9, “SELECT Statement”.) However, STRAIGHT_JOIN may prevent indexes from being used because it disables semijoin transformations.

(参见13.2.9节“SELECT语句”)但是,由于它禁用了半连接转换,因此可能会阻止使用索引。

See Section 8.2.2.1, “Optimizing Subqueries, Derived Tables, and View References with Semijoin Transformations”.

The optimizer trace may sometimes provide information complementary to that of EXPLAIN. However, the optimizer trace format and content are subject to change between versions.

优化器跟踪有时可能提供与EXPLAIN跟踪相补充的信息。然而,优化器跟踪格式和内容在不同版本之间可能会发生变化。

For details, see MySQL Internals: Tracing the Optimizer.

If you have a problem with indexes not being used when you believe that they should be, run ANALYZE TABLE to update table statistics, such as cardinality of keys, that can affect the choices the optimizer makes.

如果在您认为应该使用索引的时候没有使用索引,那么可以运行ANALYZE TABLE来更新表统计信息,比如键的基数,这可能会影响优化器做出的选择。

See Section 13.7.2.1, “ANALYZE TABLE Statement”.

Note

EXPLAIN can also be used to obtain information about the columns in a table. EXPLAIN tbl_name is synonymous with DESCRIBE tbl_name and SHOW COLUMNS FROM tbl_name.

还可以使用EXPLAIN获取有关表中列的信息。EXPLAIN tbl_name与DESCRIBE tbl_name和SHOW COLUMNS FROM tbl_name同义。 For more information, see Section 13.8.1, “DESCRIBE Statement”, and Section 13.7.5.5, “SHOW COLUMNS Statement”.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,Elasticsearch当前最新版本为7.15.1,没有8.8.1版本。以下是Elasticsearch 7.15.1的详细安装教程: 1. 下载Elasticsearch 在官网 https://www.elastic.co/cn/downloads/elasticsearch 下载Elasticsearch的压缩包,选择适合自己操作系统的版本。下载完成后解压到任意目录。 2. 修改配置文件 进入Elasticsearch的config目录,修改elasticsearch.yml文件。比如: ``` # 设置集群名称 cluster.name: my-cluster # 设置节点名称 node.name: my-node # 设置数据存储路径 path.data: /path/to/data # 设置日志文件路径 path.logs: /path/to/logs ``` 3. 启动Elasticsearch 在终端中进入Elasticsearch的bin目录,执行以下命令启动Elasticsearch: ``` ./elasticsearch ``` 如果一切顺利,你应该能看到类似以下的输出: ``` [2021-10-20T14:12:18,394][INFO ][o.e.n.Node ] [my-node] version[7.15.1], pid[12345], build[default/tar/123abc/2021-10-11T22:34:29.218660Z], OS[Linux/4.14.143-89.123.amzn1.x86_64/amd64], JVM[AdoptOpenJDK/OpenJDK 64-Bit Server VM/16.0.2/16.0.2+7] [2021-10-20T14:12:18,396][INFO ][o.e.n.Node ] [my-node] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseG1GC, -XX:G1ReservePercent=25, -XX:InitiatingHeapOccupancyPercent=30, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.locale.providers=SPI,COMPAT, -Xms512m, -Xmx512m, -Des.path.home=/path/to/elasticsearch-7.15.1, -Des.path.conf=/path/to/elasticsearch-7.15.1/config, -Des.distribution.flavor=default, -Des.distribution.type=tar, -Des.bundled_jdk=true] [2021-10-20T14:12:19,999][INFO ][o.e.p.PluginsService ] [my-node] loaded module [aggs-matrix-stats] [2021-10-20T14:12:19,999][INFO ][o.e.p.PluginsService ] [my-node] loaded module [analysis-common] [2021-10-20T14:12:19,999][INFO ][o.e.p.PluginsService ] [my-node] loaded module [geo] [2021-10-20T14:12:19,999][INFO ][o.e.p.PluginsService ] [my-node] loaded module [ingest-common] ... [2021-10-20T14:12:20,000][INFO ][o.e.p.PluginsService ] [my-node] loaded module [transport-netty4] [2021-10-20T14:12:20,000][INFO ][o.e.p.PluginsService ] [my-node] no plugins loaded [2021-10-20T14:12:23,123][INFO ][o.e.x.s.a.s.FileRolesStore] [my-node] parsed [0] roles from file [/path/to/elasticsearch-7.15.1/config/roles.yml] [2021-10-20T14:12:24,456][INFO ][o.e.i.g.GatewayService ] [my-node] recovered [0] indices into cluster_state [2021-10-20T14:12:27,521][INFO ][o.e.c.r.a.AllocationService] [my-node] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[my-index][0]]]). ``` 这表示Elasticsearch已经成功启动。你可以通过访问`http://localhost:9200`来验证Elasticsearch是否运行正常。 4. 安装插件 Elasticsearch提供了很多插件,可以根据自己的需求进行安装。比如,安装kopf插件: ``` ./bin/elasticsearch-plugin install lmenezes/elasticsearch-kopf/2.1.2 ``` 5. 配置Elasticsearch作为服务 如果你想将Elasticsearch作为服务在后台运行,可以参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/starting-elasticsearch.html。 希望这个安装教程能够帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值