* MM-23369: Allow mysql to choose a better index When the ORDER BY clause contains a column which is in the WHERE clause and also part of an index, mysql tries to use that specific index to avoid sorting. This is inspite of the fact that there may be other indices which are better for scanning the table and then doing a sort. Essentially, mysql becomes dumb and scans a lot of rows to avoid sorting. Whereas, it could have scanned a lot less rows and do the sorting in no time. To fix this, we use the other columns in the ORDER BY clause as well which are part of the index. This causes no change in the results because the other columns are an EQUAL condition check, but this lets mysql use the right index. Because now mysql sees that it has to order by other columns too, so it better use the other index to scan and then do the sorting. This does not affect tables of smaller size because the LIMIT of rows is always 1. And mysql will stop sorting the moment it gets the first row. So sorting is not the overhead at all. Therefore, this seems like an optimal fix. References: https://dev.mysql.com/doc/refman/5.7/en/table-scan-avoidance.html https://code.openark.org/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html * Added a comment to clarify things in code * Incorporating review comments
Mattermost is an open source, private cloud, Slack-alternative from https://mattermost.com.
It's written in Golang and React and runs as a single Linux binary with MySQL or PostgreSQL. Every month on the 16th a new compiled version is released under an MIT license.
Try out Mattermost
- Join the Mattermost Contributor's server to join community discussions about contributions, development and more
- Join the Mattermost Demo server to try out Mattermost and explore sample use cases
Deploy on Heroku
Note: Heroku preview does not include email or persistent storage
Deploy on a Cloud Platform via Bitnami
Auto-deploy Mattermost to Amazon Web Services, Azure, Google Cloud Platform, or Oracle Cloud Platform via Bitnami.
Install on Your Own Machine
- One-line Docker Preview
- Developer Machine Setup
- Production Install Guides using Linux Binary
- Production Docker Install
Native Mobile and Desktop Apps
In addition to the web interface, you can also download Mattermost clients for Android, iOS, Windows PC, Mac OSX, and Linux.
Get Security Bulletins
Receive notifications of critical security updates. The sophistication of online attackers is perpetually increasing. If you are deploying Mattermost it is highly recommended you subscribe to the Mattermost Security Bulletin mailing list for updates on critical security releases.
Get Involved
- Contribute Code
- Find "Help Wanted" projects
- Join Developer Discussion on a Mattermost Server for contributors
- File Bugs
- Share Feature Ideas
- Get Troubleshooting Help
- Help translate Mattermost
Learn More
- API Options - webhooks, slash commands, drivers and web service
- See who's using Mattermost
- Browse over 700 Mattermost integrations
Get the Latest News
- Twitter - Follow Mattermost
- Blog - Get the latest updates from the Mattermost blog.
- Email - Subscribe to our newsletter (1 or 2 per month)
- Mattermost - Join the ~contributors channel on the Mattermost Community Server.
- IRC - Join the #matterbridge channel on Freenode (thanks to matterircd).
Any other questions, mail us at info@mattermost.com. We’d love to meet you!





