Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '32-bit': 0.09; 'integers': 0.09; 'key.': 0.09; 'modulo': 0.09; 'subject:keys': 0.09; 'things,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '"hello': 0.16; '-tkc': 0.16; '16)': 0.16; 'adam': 0.16; 'duplicates': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'hashlib': 0.16; 'i.e.,': 0.16; 'length.': 0.16; 'md5': 0.16; 'messages)': 0.16; 'sha1': 0.16; 'sqlite': 0.16; 'subject:sqlite3': 0.16; 'suppressing': 0.16; 'truncate': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'value.': 0.19; 'fit': 0.20; 'seems': 0.21; '>>>': 0.22; 'input': 0.22; 'select': 0.22; 'import': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'integer': 0.24; 'regardless': 0.24; '---': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'long,': 0.26; 'primary': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'testing': 0.29; "i'm": 0.30; 'equality': 0.31; "skip:' 40": 0.31; 'use?': 0.31; 'text': 0.33; 'table': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'library.': 0.36; 'right?': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'so,': 0.37; 'handle': 0.38; 'files': 0.38; 'issue': 0.38; 'rather': 0.38; 'even': 0.60; 'length': 0.61; 'news': 0.67; 'received:50.22': 0.84 Date: Thu, 22 May 2014 08:09:59 -0500 From: Tim Chase To: Adam Funk Subject: Re: hashing strings to integers for sqlite3 keys In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400764235 news.xs4all.nl 2921 [2001:888:2000:d::a6]:54490 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71886 On 2014-05-22 12:47, Adam Funk wrote: > I'm using Python 3.3 and the sqlite3 module in the standard library. > I'm processing a lot of strings from input files (among other > things, values of headers in e-mail & news messages) and suppressing > duplicates using a table of seen strings in the database. > > It seems to me --- from past experience with other things, where > testing integers for equality is faster than testing strings, as > well as from reading the SQLite3 documentation about INTEGER > PRIMARY KEY --- that the SELECT tests should be faster if I am > looking up an INTEGER PRIMARY KEY value rather than TEXT PRIMARY > KEY. Is that right? If sqlite can handle the absurd length of a Python long, you *can* do it as ints: >>> from hashlib import sha1 >>> s = "Hello world" >>> h = sha1(s) >>> h.hexdigest() '7b502c3a1f48c8609ae212cdfb639dee39673f5e' >>> int(h.hexdigest(), 16) 703993777145756967576188115661016000849227759454L That's a pretty honkin' huge int for a DB key, but you can use it. And it's pretty capped on length regardless of the underlying string's length. > If so, what sort of hashing function should I use? The "maxint" for > SQLite3 is a lot smaller than the size of even MD5 hashes. The only > thing I've thought of so far is to use MD5 or SHA-something modulo > the maxint value. (Security isn't an issue --- i.e., I'm not > worried about someone trying to create a hash collision.) You could truncate that to something like >>> int(h.hexdigest()[-8:], 16) which should give you something that would result in a 32-bit number that should fit in sqlite's int. -tkc