mysql排序会用到索引_mysql是否使用索引进行排序...

bd96500e110b49cbb3cd949968f18be7.png

Does a sort use a mysql index if there is an index on the sorting column? Also what other things is the index used for?

What difference does it make in a combined and separate indexes of the columns?

解决方案

Yes, MySQL uses your index to sort the information when the order is by the sorted column.

Also, if you have indexes in all columns that you have added to the SELECT clause, MySQL will not load the data from the table itself, but from the index (which is faster).

The difference between combined and separate indexes is that MySQL can not use more than one index per query, so, if your query filters by many columns and you would like to have it correctly indexed you will need to create a combined index of all columns.

But before adding lots of indexes to your tables, remember that each index makes insert/update/delete operations go slower.

I would also highly recommend the High Performance MySQL book by O'Reilly that will cover in depth all of these issues and a lot of other hints you need to know to really be able to use MySQL to the limit.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 中,使用局部变量可能导致索引失效的情况通常涉及到查询语句中的变量赋值和索引使用方式。以下是一些常见的情况: 1. 变量赋值导致索引失效:当在查询语句中使用局部变量进行赋值操作时,MySQL 无法在编译阶段确定变量的值,因此无法使用索引来优化查询。例如,使用 `SET` 命令将变量赋值后再使用该变量进行查询,这样可能导致索引失效。 2. 函数对变量的操作导致索引失效:当在查询语句中使用函数对局部变量进行操作时,MySQL 无法利用索引来优化查询。这是因为函数的操作可能改变变量的值,导致无法准确匹配索引。 3. 强制类型转换导致索引失效:当在查询语句中对局部变量进行强制类型转换时,MySQL 无法使用索引来优化查询。这是因为强制类型转换改变变量的数据类型,导致无法匹配索引中保存的数据类型。 为避免索引失效的情况,可以考虑以下几点: 1. 尽量避免在查询语句中使用局部变量进行赋值操作,尽量直接使用常量或参数。 2. 尽量避免在查询语句中使用函数对局部变量进行操作,可以考虑在应用程序中预先计算并传入结果。 3. 尽量避免在查询语句中对局部变量进行强制类型转换,可以考虑使用合适的数据类型来避免类型转换。 4. 根据具体情况,考虑是否需要重新设计查询语句、优化索引或调整表结构等操作,以提高查询性能。 总的来说,避免在查询语句中过多依赖局部变量的赋值、函数操作和类型转换,能够减少索引失效的潜在问题,提高查询性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值