search
HomeDatabaseMysql Tutorial在论坛中出现的各种疑难问题:模糊匹配问题

关于2个表模糊搜索匹配的问题,现已找到较快的解决方法,速度提升到每秒5条记录左右,而且不占CPU,不占内存,方法如下: ------------------------------------------------------------------------- 环境: 有2个表, 表1:MainTable (现有记录数在10万条

关于2个表模糊搜索匹配的问题,现已找到较快的解决方法,速度提升到每秒5条记录左右,而且不占CPU,不占内存,方法如下:
-------------------------------------------------------------------------
环境:
有2个表,
表1:MainTable (现有记录数在10万条左右)
字段:
id bigint自动编号
Title nvarchar(30)
SubId nvarchar(max)

表MainTable:
Id Title SubId
1 A 0
2 B 0
3 C 0
4 D 0
5 E 0
6 F 0

表2:SubTable (现有记录数在300万条左右)
字段:
id bigint自动编号
Description nvarchar(100)
Fl int '默认值为0,当进行模糊匹配后,值改为1

表SubTable:
Id Description Fl
1 ABC 0
2 AB 0
3 CD 0
4 EA 0

二、需要实现的结果为:
表MainTable:
Id Title SubId
1 A 0,1,2,4
2 B 0,1,2
3 C 0,1,3
4 D 0,3
5 E 0,4
6 F Null

第6条记录由于没有匹配的值,所以改为Null
-------------------------------------------------------------------------

1、由于记录数太多,通过内部存储搜索速度太慢,中途不能暂停,平均每分钟才能处理5条记录左右;
2、原先通过外部循环的方法处理,速度1秒1条左右,会比方法1速度快,但非常占CPU;

现在的办法:
用VBS编写,定义2个数组,IDArray()和DescriptionArray()分别用于存储在SubTable表检索到的ID集和Description集

1、MainTable用循环的方式,按字段Title升序的方式得取Title字段的值,
先取得第一条记录的Title字段的第一个字符,根据这个字符模糊匹配SubTable的Description字段,并将检索到的结果存放在数组IDArray和DescriptionArray()中。
2、通过循环方式,将MainTable表的当前记录的Title字段的完整值与DescriptionArray()的值进行匹配处理,并update。

3、获取MainTable的下一条记录,判断该条记录Title字段的第一个字符是否与上一条记录的Title的第一个字符相同,如果相同,则从第2步开始处理;如果不同,则从第1步开始处理。

------------------------------------------------------------------------------
这种方法减少了每次去SubTable模糊搜索的次数,如果Title字段的第一个字符相同的记录非常多的情况下,速度还可能会提高很多。

总结一下,这个问题的解决不是通过sql server,而是在vbs,通过运用数据本身的特性,也就是:Title字段的第一个字符相同的记录非常多,减少了重复的劳动,少做了很多的无用功,最后,大幅提升性能。

真的是好办法,其实,从这个例子中可以看出,优化,更重要的是强调思维,而不简单的是某个技术,注重细节,仔细分析,楼主就解决了这个优化问题。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool