简体   繁体   中英

what is the best approach to find duplicates in my Db table

In my app the user can select multiple filter options. I store this in a DB table.

For example

User 1 can select filters A^B

User 2 can select filters AORC^D and so forth.

The way it is stored in Db is

user        filter_selected

user1       A^B
user2       AORC^D

Now the criteria is no user can have the same filters selected. So if user 3 comes and select A^B or B^A it should throw a error.

I am trying to come up with a smart logic to validate this in javascript. One approach is go through all the users in the DB (can be many) and sort alphabetically and check if its the same. So in our example A^B and B^A will be the same AB^. This way I can check. Any other better approach may be using mysql command itself ?

you can sort your filter rule based on character and then insert it to do for example, B^A will convert to AB^ and when you want to check you can sort your filter and then search it

if you want to have an original filter you don't care about the size of your database and more you care about speed you can save original as another column too.if you are care about size of database you can just save the original filter and when you want to search select the rows that have the same length as your filter and then you need to sort alphabetically or you can save index of every filter chars for example when you change A^B to AB^ you can save this filter AB^|021 but this will need to some more space too like original column and I don't suggest this method. also if your filters are always in small length you can don't fetch all record and compare to all. you can just create all possible way of the filter(for example AB^ A^BB^A BA^ ^AB ^BA) but you must be careful because in this method you are creating n! string and this is not good at all, just for too small length string its ok and that's when you have too many records in your database this method can be good

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM