- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 MySQL 和 Django 框架。(我打算稍后将MySQL更改为PostgreSQL)
以下是抽象真实数据库的示例。
<小时/>ID
(int(11)
type):从1开始自动递增的值
A 列
(int(10)
类型):从 10 个值中进行选择
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...
...
许多行(超过十万行)
每行都有A 列
值10 个值(301010101、..、301010110)
首先按A 列
分组,然后按ID
降序排序。
例如,Models.object.filter(Column A = "301010101").order_by('-id')
对于上述情况,哪一列是最适合索引的列?
ID
作为索引(默认)
A 列
作为索引
同时使用ID
和A列
作为索引(多索引)
我用 500000 行随机数据对其进行了测试。 (在 Class Meta 中使用 `ordering=['-id'])
然后,我测试了查询Models.objects.filter(Column A = "301010101")
ID
作为索引(默认):0.33 秒
使用ID
和Column A
一起作为索引(多索引):0.12秒
从上面的测试结果来看,我确信使用ID
和Column 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/
我是一名优秀的程序员,十分优秀!