为什么不使用索引?

发布于 2025-01-10 11:53:40 字数 3187 浏览 0 评论 0原文

各位互联网上的好心人大家好。

我绞尽脑汁试图弄清楚为什么优化器没有使用我的索引来进行 Amazon Aurora 查询。该查询是根据用户通过应用程序 UI 创建的报告动态创建的,因此我无法更改查询本身。

查询使用这些限定符

WHERE
  table_in_question.deleted = 0
ORDER BY
  table_in_question.date_modified DESC,
  table_in_question.id DESC

我有一个​​索引“my_index”,它索引这些特定字段(table_in_question字段已删除,date_modified,ID),但MySQL不使用它。

该查询运行大约需要 1200 毫秒。如果我添加 FORCE INDEX (my_index),大约需要 120 毫秒。可以说快了大约 10 倍 - 但除非我使用强制索引,否则它不会使用它。

根据 EXPLAIN 返回了大约 100 万行,所以我不认为这是不使用索引的情况,因为返回的行数很少。

完整查询是

SELECT
  case when some_table.id IS NOT NULL then some_table.id else "" end my_favorite,
  table_in_question.date_entered,
  table_in_question.name,
  table_in_question.description,
  table_in_question.pr_is_read,
  table_in_question.pr_is_approved,
  table_in_question.parent_type,
  table_in_question.parent_id,
  table_in_question.id,
  table_in_question.date_modified,
  table_in_question.assigned_user_id,
  table_in_question.created_by
FROM
  table_in_question
  INNER JOIN (
    SELECT
      tst.team_user_is_member_of
    FROM
      team_sets_teams tst
      INNER JOIN team_memberships team_membershipstable_in_question ON (
        team_membershipstable_in_question.team_id = tst.team_id
      )
      AND (team_membershipstable_in_question.user_id = 'UUID')
      AND (team_membershipstable_in_question.deleted = 0)
    GROUP BY
      tst.team_user_is_member_of
  ) table_in_question_tf ON table_in_question_tf.team_user_is_member_of = table_in_question.team_user_is_member_of
  LEFT JOIN systemfavourites sf_table_in_question ON (sf_table_in_question.module = 'table_in_question')
  AND (sf_table_in_question.record_id = table_in_question.id)
  AND (sf_table_in_question.assigned_user_id = 'UUID')
  AND (sf_table_in_question.deleted = '0')
  INNER JOIN opportunities jt1_table_in_question ON (table_in_question.opportunity_id = jt1_table_in_question.id)
  AND (jt1_table_in_question.deleted = 0)
  LEFT JOIN another_table jt1_table_in_question_cstm ON jt1_table_in_question_cstm.id_c = jt1_table_in_question.id
  LEFT JOIN systemfavourites table_in_question_favorite ON (table_in_question.id = table_in_question_favorite.record_id)
  AND (table_in_question_favorite.deleted = '0')
  AND (table_in_question_favorite.module = 'table_in_question')
  AND (table_in_question_favorite.created_by = 'UUID')
  LEFT JOIN users some_table ON (
    some_table.id = table_in_question_favorite.modified_user_id
  )
  AND (some_table.deleted = 0)
WHERE
  table_in_question.deleted = 0
ORDER BY
  table_in_question.date_modified DESC,
  table_in_question.id DESC
;

显示此

idselect_type分区类型possible_keyskeykey_lenrefrowsfiltered1Extra
table_in_questionPRIMARYEXPLAINALLidx_table_in_question_tmst_id96823410.0使用 where;使用临时的;使用 filesort

任何人都可以帮助解释我如何创建默认情况下实际使用的索引吗?

谢谢。

Hello kind people of the internet.

I am wrecking my head trying to figure out why the optimiser isn't using my index for my query on Amazon Aurora. The query is dynamically created based on a report users have created through an applications UI, so I can't change the query per se.

The query uses these qualifiers

WHERE
  table_in_question.deleted = 0
ORDER BY
  table_in_question.date_modified DESC,
  table_in_question.id DESC

I have an index, "my_index", which indexes these specific fields (table_in_question fields deleted, date_modified, ID) but MySQL doesn't use it.

The query takes approx 1200 ms to run. If I add FORCE INDEX (my_index) it takes about 120ms. Arguably about 10x faster - but unless I use force index, it doesn't use it.

Around 1 million rows are returned according to EXPLAIN, so I don't think it's a case of not using the index because of a low amount of rows being returned is the case.

The full query is

SELECT
  case when some_table.id IS NOT NULL then some_table.id else "" end my_favorite,
  table_in_question.date_entered,
  table_in_question.name,
  table_in_question.description,
  table_in_question.pr_is_read,
  table_in_question.pr_is_approved,
  table_in_question.parent_type,
  table_in_question.parent_id,
  table_in_question.id,
  table_in_question.date_modified,
  table_in_question.assigned_user_id,
  table_in_question.created_by
FROM
  table_in_question
  INNER JOIN (
    SELECT
      tst.team_user_is_member_of
    FROM
      team_sets_teams tst
      INNER JOIN team_memberships team_membershipstable_in_question ON (
        team_membershipstable_in_question.team_id = tst.team_id
      )
      AND (team_membershipstable_in_question.user_id = 'UUID')
      AND (team_membershipstable_in_question.deleted = 0)
    GROUP BY
      tst.team_user_is_member_of
  ) table_in_question_tf ON table_in_question_tf.team_user_is_member_of = table_in_question.team_user_is_member_of
  LEFT JOIN systemfavourites sf_table_in_question ON (sf_table_in_question.module = 'table_in_question')
  AND (sf_table_in_question.record_id = table_in_question.id)
  AND (sf_table_in_question.assigned_user_id = 'UUID')
  AND (sf_table_in_question.deleted = '0')
  INNER JOIN opportunities jt1_table_in_question ON (table_in_question.opportunity_id = jt1_table_in_question.id)
  AND (jt1_table_in_question.deleted = 0)
  LEFT JOIN another_table jt1_table_in_question_cstm ON jt1_table_in_question_cstm.id_c = jt1_table_in_question.id
  LEFT JOIN systemfavourites table_in_question_favorite ON (table_in_question.id = table_in_question_favorite.record_id)
  AND (table_in_question_favorite.deleted = '0')
  AND (table_in_question_favorite.module = 'table_in_question')
  AND (table_in_question_favorite.created_by = 'UUID')
  LEFT JOIN users some_table ON (
    some_table.id = table_in_question_favorite.modified_user_id
  )
  AND (some_table.deleted = 0)
WHERE
  table_in_question.deleted = 0
ORDER BY
  table_in_question.date_modified DESC,
  table_in_question.id DESC
;

EXPLAIN shows this

idselect_typetablepartitionstypepossible_keyskeykey_lenrefrowsfilteredExtra
1PRIMARYtable_in_questionALLidx_table_in_question_tmst_id96823410.0Using where; Using temporary; Using filesort

Can anyone help explain how I make an index it will actually use by default?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文