Given the following database table:
WORDS alphagram....varchar(15) word.........varchar(15) PK length.......int
Where:
- 'alphagram' are the letters of the word in alphabetical order (for example, AEINNRTT is the alphabet INTRANET)
- The primary key is the word, and there are indexes in alphabetical order and length
I found a way to find the anagrams of a given string of letters through SQL. For example, to find AEINNRTT anagrams, this will work:
select alphagram, word, definition from words where length = 8 and alphagram like '%A%' and alphagram like '%E%' and alphagram like '%I%' and alphagram like '%NN%' and alphagram like '%R%' and alphagram like '%TT%'
This will return 1 row (for INTRANET)
And if I wanted to include a known number of wildcards, for example, how many words with INTRANET + are empty (wildcard), I just need to change the βlengthβ to the total number of letters + the number of wildcards
eg.
select alphagram, word, definition from words where length = 9 and alphagram like '%A%' and alphagram like '%E%' and alphagram like '%I%' and alphagram like '%NN%' and alphagram like '%R%' and alphagram like '%TT%'
... will return 8 rows (ENTERTAIN, INSTANTER, INTEGRANT, INTRANETS, ITINERANT, NATTRING, RATTENING and TRANSIENT)
My question is this: is there a more efficient way to do this with SQL only?
This works pretty fast in SQLServer, but pretty slow in SqlLite. I understand that% xxx% search is not fast.
sql
eponymous23
source share