performance - SQL anagram efficiency and logic? -
i have sql db 200,000 words. need query able solve anagram kind of. difference need possible words made input characters. example, if input ofdg, should output words: do, go, , dog. can estimate amount of time query take. how can make faster , more efficient? also, in general how long take sql parse 200000 row database.
to solve problem, first thing need reduce every word scrabble players call alphagram. is, letters in word in alphabetical order. do, go , dog make do, go , dgo. of course, given alphagram may correspond more 1 word, so, example, alphagram dgo corresponds both words dog , god.
the next thing need construct table key alphagram-sequence number , single attribute field word.
word lists tend static. example, 2 scrabble word lists in english-speaking world change every 5 years of so. construct lookup table beforehand. performance o( n ) , sunk cost. is, once , store it, not counted against cost of query. have beforehand. makes absolutely no sense build such index on fly every time query comes in.
you may wondering "what scrabble?" answer figure of 200,000 words falls neatly between 2 approved tournament word lists in english-speaking world. national scrabble association's official tournament , club word list (2006) contains 178,691 words, , international list, maintained world english scrabble players' association, contains 246,691.
when query reduce supplied word bunch of alphagrams. input odfg makes alphagrams od fo go df dg fg dfo dgo fgo dfg dfgo (which pretty programming problem in pure sql, have assume there php or python or javascript front-end you). lookup in database. cost of each query should approximately o(log2 n), in other words pretty damn immediate. sort of query relational databases at.
btw, example output poor. alphagram dfgo scrabble players call 'build' (all possible subsets) makes do od of go dog god fog.
(i hate have rigmarole, hasbro's lawyers touchy, so: scrabble registered trademark owned in usa hasbro, inc.; in canada hasbro canada corporation; , throughout rest of world j. w. spear & sons, mattel company.)
Comments
Post a Comment