gpt4 book ai didi

mysql - SQL : What is a good column for indexing?

转载 作者:行者123 更新时间:2023-11-29 07:45:10 25 4
gpt4 key购买 nike

我正在使用 MySQLDjango 框架。(我打算稍后将MySQL更改为PostgreSQL)

以下是抽象真实数据库的示例。

<小时/>

列说明

  1. ID(int(11) type):从1开始自动递增的值

  2. A 列(int(10) 类型):从 10 个值中进行选择

  3. B 列(长文本类型):文本字段

<小时/>

数据行示例

ID     |    Column A    |      Column B
...
21 | 301010101 | TGGQtY84r033i0F6tpx3...
22 | 301010102 | 31TfNgzpxkcuMLxrrZ6D...
23 | 301010103 | U069Z5kG354BwDriFw6d...
24 | 301010107 | d4MSkCBxwZzKusALQAIQ...
25 | 301010105 | R1SJCWeM62P1ikQwmG3f...
26 | 301010103 | bVScBZbf0n1tkdgFCwmD...
27 | 301010102 | 4UpQGyCz5KhlolEdsO8M...
28 | 301010101 | x89gOjNS4J4xiP1DfIWH...
29 | 301010110 | STMlfUwx8afCZBsa8CWJ...
30 | 301010101 | XctEBThnlA5MYTKqycLJ...
31 | 301010104 | fRAEBMXDEdNFn5aENn4r...
31 | 301010105 | GlIwVjVF16WE4zWnnSy8...
...
  1. 许多行(超过十万行)

  2. 每行都有A 列10 个值(301010101、..、301010110)

<小时/>

查询使用情况

  1. 首先按A 列分组,然后按ID降序排序。

    例如,Models.object.filter(Column A = "301010101").order_by('-id')

<小时/>

问题

对于上述情况,哪一列最适合索引的列

  1. ID 作为索引(默认)

  2. A 列 作为索引

  3. 同时使用IDA列作为索引(多索引)

<小时/>

编辑:结果

我用 500000 行随机数据对其进行了测试。 (在 Class Meta 中使用 `ordering=['-id'])

然后,我测试了查询Models.objects.filter(Column A = "301010101")

  1. ID 作为索引(默认):0.33 秒

  2. 使用IDColumn A一起作为索引(多索引):0.12秒

从上面的测试结果来看,我确信使用IDColumn A一起作为多索引是最优化的情况。

最佳答案

对于该“特定”查询,按照建议,在(a 列,id)上的双列索引上似乎可以获得更好的性能。

来自手册:

"If a multiple-column index exists on col1 and col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on col1 and col2, the optimizer attempts to use the Index Merge optimization (see Section 8.3.1.4, “Index Merge Optimization”), or attempts to find the most restrictive index by deciding which index excludes more rows and using that index to fetch the rows"

关于mysql - SQL : What is a good column for indexing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002414/

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