mysql二进制比较不使用索引

发布于 2024-09-28 17:14:19 字数 416 浏览 5 评论 0原文

我有以下查询:

EXPLAIN EXTENDED SELECT *
FROM (
`photo_data`
)
LEFT JOIN `deleted_photos` ON `deleted_photos`.`photo_id` = `photo_data`.`photo_id`
WHERE photo_data.photo_id = 'UKNn'
AND `deleted_photos`.`photo_id` IS NULL

不幸的是,我必须使用二进制来比较这个 photo_id (它是从不同的外部服务交给我的)。这样我就可以避免从数据库中取出“uknn”而不是“UKNn”。

问题是,当我进行解释时,我看到二进制的使用不使用索引。如果我取出二进制文件,它会使用 photo_id 的索引。有没有办法能够使用二元选项并使用索引?

I have the following query:

EXPLAIN EXTENDED SELECT *
FROM (
`photo_data`
)
LEFT JOIN `deleted_photos` ON `deleted_photos`.`photo_id` = `photo_data`.`photo_id`
WHERE photo_data.photo_id = 'UKNn'
AND `deleted_photos`.`photo_id` IS NULL

I unfortunately have to use binary to compare this photo_id (which is being handed to me from a different outside service). So that I avoid 'uknn' being pulled out of the datbase instead of 'UKNn'.

The problem is that when I do the explain, i see the use of binary doesn't use the index. If I take out binary it uses the index for photo_id. Is there a way to be able to use the binary option and use an index with it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掐死时间 2024-10-05 17:14:19

MySQL 使用列的排序规则作为索引。具有非二进制排序规则的索引对于二进制查找没有用,因为顺序可能不同。

您可以将列本身更改为二进制排序规则:

ALTER TABLE YourTable MODIFY
   YourColumn VARCHAR(4)
   CHARACTER SET latin1
   COLLATE latin1_bin;

然后索引对于二进制查找将很有用。

MySQL uses the collation of the column for the index. An index with a non-binary collation isn't useful for a binary lookup since the order might be different.

You could change the column itself to binary collation:

ALTER TABLE YourTable MODIFY
   YourColumn VARCHAR(4)
   CHARACTER SET latin1
   COLLATE latin1_bin;

Then the index would be useful for a binary lookup.

寄居人 2024-10-05 17:14:19

如果您只需要以区分大小写的方式比较字段值 - 您可以专门更改该字段的排序规则。

ALTER TABLE photo_data MODIFY photo_id VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_bin

或者

ALTER TABLE photo_data MODIFY photo_id VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_general_cs

If you need just to compare field value in case-sensitive manner - you could change collation for that field specifically.

ALTER TABLE photo_data MODIFY photo_id VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_bin

or

ALTER TABLE photo_data MODIFY photo_id VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_general_cs
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文