gpt4 book ai didi

MySQL多索引优化

转载 作者:行者123 更新时间:2023-11-29 05:21:34 25 4
gpt4 key购买 nike

我有一个关于使用多个索引优化 sql 查询的问题。

假设我有一个表“TEST”,其中包含字段“A、B、C、D、E、F”。

在我的代码 (php) 中,我使用以下“WHERE”查询:

  • 从 TEST WHERE a = 'x' and B = 'y' 中选择 (..)
  • 从 TEST WHERE a = 'x' and B = 'y' and F = 'z' 中选择 (..)
  • 从测试中选择 (..) WHERE a = 'x' and B = 'y' and (D = 'w' or F = 'z')

在运行查询时获得最佳速度的最佳方法是什么?

3 多个索引,例如 (A, B)、(A, B, F) 和 (A, B, D, F)?还是单一的多重指标(A、B、D、F)?

我倾向于说3个索引最好,即使数据库中的索引空间会更大。在我的问题中,我搜索最佳执行时间而不是空间。数据库大小合理。

最佳答案

Multiple-column indexes :

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on. If you specify the columns in the right order in the index definition, a single composite index can speed up several kinds of queries on the same table.

换句话说,定义一个覆盖与另一个索引相同的前 N 列且顺序相同的索引是对空间和计算能力的浪费。

关于MySQL多索引优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com