【问题标题】:multi-column index and ORDER BY多列索引和 ORDER BY
【发布时间】:2026-02-20 02:15:02
【问题描述】:

我有一个 mysql 数据库,在 aws rds 微型实例上托管了 700 万行。

我正在做这样的查询:

SELECT
  `id`, `address`, `property_type`, `rooms`,
  `published`, `size`, `net_rent`, `gross_rent`,
  `purchase_price`, `original_id`
FROM (`properties`)
WHERE
  `postcode` IN ('1000', '1001', '1002', '1003',
                 '1004', '1005', '1006', '1007',
                 '1010', '1011', '1012', '1014',
                 '1015', '1017', '1018', '1019')
  AND `published` < '2013-01-09'
  AND `property_type` IN ('Apartment', 'Apartment with terrace',
                          'Attic', 'Attic flat / penthouse',
                          'Loft', 'Maisonette', 'Studio')
  AND `sale_or_rent` = 'rent'
LIMIT 50

因此我创建了一个这样的索引:

| properties |          1 | listing       |            1 | postcode      | A         |       25091 |     NULL | NULL   | YES  | BTREE      |         |               |
| properties |          1 | listing       |            2 | published     | A         |     2333545 |     NULL | NULL   | YES  | BTREE      |         |               |
| properties |          1 | listing       |            3 | property_type | A         |     3500318 |     NULL | NULL   | YES  | BTREE      |         |               |
| properties |          1 | listing       |            4 | sale_or_rent  | A         |     3500318 |     NULL | NULL   | YES  | BTREE      |         |               |

到目前为止一切都很好。当我尝试添加 ORDER BY 已发布的 DESC(一个查询为 30"+)时,问题就出现了。

知道我还需要发布在 WHERE 子句中,是否有一个有效的 ORDER BY 已发布 DESC?

【问题讨论】:

  • 使用 EXPLAIN 为您的查询(按顺序)添加前缀,然后运行它并将结果添加到您的问题中。猜猜它没有找到您的索引可用于排序并进行完整排序,但这是一个猜测。

标签: mysql indexing


【解决方案1】:

我会尝试将“已发布”放在索引的首位

【讨论】: