MySQL索引在字符串的第一部分

编程入门 行业动态 更新时间:2024-10-08 10:51:40
本文介绍了MySQL索引在字符串的第一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在MySQL中查询一个包含category_id,subcategory_id和zipcode的非常大的表(超过3M记录)。 zip中的zip可能是也可能不是10个字符。

I'm querying a very large table (over 3M records) in MySQL that has a category_id, subcategory_id and zipcode. The zip may or may not be 10 characters in the db.

目的是获取指定zip的某个半径内的所有cat / subcat项目。我有一个函数返回一个指定的5位拉链列表。然后将此列表提供给我的查询,如此...

The purpose is to get all the cat/subcat items w/in a certain radius of the specified zip. I have a function that returns a list of 5-digit zips for one specified. This list is then fed to my query like so...

SELECT whatever FROM tblName WHERE cat_id = 1 AND subcat_id = 5 AND LEFT(zip,5) IN (11111,22222,33333,44444,55555)

我在cat_id,subcat_id和zip上有一个复合索引,但在某些情况下,zip为10个字符可能会将其丢弃。我能否指数LEFT(拉链,5)不知何故?

I have a compound index on cat_id, subcat_id and zip, but the zip being 10 characters in some cases may be throwing it off. Can I index the LEFT(zip,5) somehow?

推荐答案

您应该有正常的5位数邮政编码和列列使用所有额外的数字,让SQL正常处理它。有一些方法可以做你所说的,但这是迄今为止最有效的解决方案。

You should have a column with the normal 5 digit zip and column with all of the extra digits and let SQL handle it normally. There are ways you could do what your talking about, but this is by far the most efficient solution.

更多推荐

MySQL索引在字符串的第一部分

本文发布于:2023-10-31 06:54:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   第一部分   索引   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!