Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; 'skip:[ 20': 0.04; 'paths': 0.07; 'comments?': 0.09; 'cursor': 0.09; 'filename': 0.09; 'filenames': 0.09; 'linear': 0.09; 'performs': 0.09; 'rows': 0.09; 'python': 0.11; 'itself.': 0.14; 'question.': 0.14; '"insert': 0.16; '"keep': 0.16; '(filename,': 0.16; "?',": 0.16; 'adam': 0.16; 'command:': 0.16; 'drag': 0.16; 'example)': 0.16; 'filenames.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'set,': 0.16; 'subdirectory': 0.16; 'subject: \n ': 0.16; 'subject:sqlite3': 0.16; 'ignore': 0.16; 'wrote:': 0.18; "skip:' 30": 0.19; 'header:User-Agent:1': 0.23; 'looks': 0.24; '(for': 0.26; 'primary': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'subject:) ': 0.29; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'with,': 0.31; 'probably': 0.32; 'run': 0.32; 'text': 0.33; 'table': 0.34; 'subject: (': 0.35; 'created': 0.35; 'something': 0.35; 'but': 0.35; 'library.': 0.36; 'method': 0.36; 'skip:o 20': 0.38; 'question,': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'list,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'full': 0.61; 'new': 0.61; 'simple': 0.61; 'first': 0.61; '2000': 0.65; 'news': 0.67; 'reads': 0.68; 'hour': 0.70; 'minute.': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OZcWD3jY c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=ISF7S8FXY6cA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=A1X0JdhQAAAA:8 a=Bb3keiCvoUJ84rM1d6sA:9 a=QEXdDO2ut3YA:10 a=Y6qChIQXU1wA:10 X-AUTH: mrabarnett:2500 Date: Tue, 01 Jul 2014 13:13:56 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Searching for lots of similar strings (filenames) in sqlite3 database References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404216846 news.xs4all.nl 2878 [2001:888:2000:d::a6]:39969 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73784 On 2014-07-01 12:26, Adam Funk wrote: > I have some code that reads files in a leafnode2 news spool & needs to > check for new files periodically. The full paths are all like > '/var/spool/news/message.id/345/<123456@example.com>' with a 3-digit > subdirectory & a Message-ID for the filename itself. I'm using Python > 3 & sqlite3 in the standard library. > > I have a table of filenames created with the following command: > > cursor.execute('CREATE TABLE files (filename TEXT PRIMARY KEY, used INTEGER)') > > To check for new files in one of the subdirectories, I run A then > either B or C below (I've tried both). > > A. > listing1 = os.listdir(directory) > listing [os.path.join(directory, x) for x in listing1] > > B. > cursor = db_conn.cursor() > for filename in listing: > cursor.execute('SELECT filename FROM files WHERE filename IS ?', (filename,)) > row = cursor.fetchone() > if not row: > cursor.execute('INSERT INTO files VALUES (?, ?)', (filename, 0) ) > files_new += 1 > db_conn.commit() > > C. > cursor = db_conn.cursor() > subdir_like = directory + '/%' > cursor.execute('SELECT filename FROM files WHERE filename LIKE ?', (subdir_like,)) > rows = cursor.fetchall() > known_files = [row[0] for row in rows] > for filename in listing: > if filename not in known_files: > cursor.execute('INSERT INTO files VALUES (?, ?)', (filename, 0) ) > files_new += 1 > db_conn.commit() > > A+B was the first method I came up with, because it looks like the > "keep it simple & let the database do its job" approach, but it was > very time-consuming, so I tested A+C out. A is quick (a second); B > can drag on for over an hour to check 2000 filenames (for example) in > a subdirectory; C always takes less than a minute. So C is much > better than B, but it looks (to me) like one of those attempts to > bypass & ignore the database's built-in optimizations. > > Comments? > In C, 'known_files' is a list, so it performs a linear search for each of the filenames. If you make 'known_files' a set, it'll probably be even faster! Anyway, I'm sure there's something in SQL for "insert or update" or "on duplicate", but that's an SQL question, not a Python question.