gpt4 book ai didi

mysql - 如何找到sql表的最佳索引?

转载 作者:行者123 更新时间:2023-11-29 06:19:59 25 4
gpt4 key购买 nike

我有一个日志表,它有数百万条记录。我想提高我的 sql 查询速度,但我不知道如何生成最佳索引组或索引组。如何找到 sql 表的最佳索引?

目前我正在使用;

索引:my_test_index

定义:输入 BTREE

唯一编号

A列 F 乙 丁 C

这是我对日志表的所有 sql 查询;

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4 or columnB=7) and columnC= ... order by columnD desc LIMIT 0,100;

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4) and columnE= ... and ((date(columnD))>=... and (date(columnD))<=...) order by columnD desc";

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4 or columnB=7) and columnE= ... order by columnD desc ";

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4) and columnE= ... and ((date(columnD))>=... and (date(columnD))<=...) order by columnD desc";

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4 or columnB=7) and columnF= ... order by columnD desc LIMIT 0,100;

$sql = "select *
from log
where columnA=1 and (columnB=6 or columnB=4 or columnB=7) and columnG= ... order by columnD desc LIMIT 0,100;

最佳答案

我建议的索引是

  1. colA, colB, colC
  2. colA、colE、colB
  3. colA, colF, colB
  4. colA, colB, colG

补充几点:

  • 你不需要在日期上有索引,因为每一行都有不同的值,添加索引并不重要。
  • 由于您的所有查询都进行了等效检查,因此顺序无关紧要。
  • 此外,如果执行频率较低,您可以删除一个或两个索引,因为 (colA, colB) 上总会有一些索引
  • 如果基数(可能值)非常小,您可以删除列上的索引。例如,不需要为可能值为 1/0 的 is_valid 列编制索引。如果值匹配超过30%键值,mysql优先做表扫描。

说需要所有 4 个索引是错误的。但那是你最需要的。通过评估上述其他条件,您可以拥有更小的指标。

关于mysql - 如何找到sql表的最佳索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807763/

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