0

大きなファイル管理システムにPythonでSQLiteを使用しています。3列(整数)の値を使用して並べ替えたい大きなフラットファイル(1億行)があるので、繰り返して計算を行うことができます。

私はSQLiteを大きなSELECT ... ORDER BYもの(1つの列にインデックスがある)で使用しました。これSELECTはメモリを大量に消費するので、何度か呼び出す必要があります(とOFFSETLIMIT)。

Linuxソートを使用することもできますが、プラットフォームに依存しないようにしたいです。(正しいプラグマが正しく設定されている限り)正常に動作していますが、低速です。これを最適化する方法は?

コマンドは次のようなものです。

PRAGMA journal_mode = OFF
PRAGMA synchronous = 0
PRAGMA locking_mode = EXCLUSIVE
PRAGMA count_change = OFF
PRAGMA temp_store = 2
CREATE TABLE tmpTranscripts_arm_3R_transcripts (id INTEGER PRIMARY KEY, name varchar(255), chromosome varchar(255), start int(11), end int(11), direction tinyint(4), tags varchar(1023), bin int(11), exons varchar(10000))
CREATE INDEX 'iTranscript_arm_3R_14943' ON 'tmpTranscripts_arm_3R_transcripts' (start, end, direction)
INSERT INTO tmpTranscripts_arm_3R_transcripts (name, chromosome, start, end, direction, tags, bin, exons) VALUES ('SRR060644.1', 'arm_3R', 11450314, 11450337, -1, 'feature=transcript;bestRegion=(self);nbGaps=0;nbMismatches=0;ID=SRR060644.1;identity=100.0', 300011450, '')
(this, more than 10 millions times)
SELECT * FROM tmpTranscripts_arm_3R_transcripts ORDER BY start, end, direction LIMIT 0, 10000
(this, as much as needed)
4

1 に答える 1

2

データベースを作成し、そのすべての要素を調べるサンプルスクリプトをいくつか作成しました。そして、それはあなたがコメントに書いたよりもはるかに速く動作するように見えます。データベースアクセスがボトルネックになっていることを確認しますか?たぶんあなたのスクリプトであなたはもっと何かをします、そしてこれはとても時間がかかります。

2つのデータベースSQLiteとMongoDBを500万のアイテムでチェックしました。SQLiteの場合、すべての行の挿入には約1200秒かかり、選択には約300秒かかりました。MongoDBの方が高速で、挿入には最大400秒かかりましたが、選択は100秒未満でした。

私のサンプルでコードをチェックし、選択したものが類似しているかどうかを確認してください。LIMIT/OFFSETの代わりにカーソルを使用しました。それでも問題が解決しない場合は、MongoDBを試してみる価値があると思います。これには1つの欠点があります。(あなたのような)大規模なデータベースをサポートするには64ビットOSが必要です。以前に使用したことがない場合は、Windowsの最短のインストールガイドを次に示します。

そしてこれがSQLite用の私のpython3.xテストスクリプトです

import sqlite3
from time import time

conn = sqlite3.connect('test.dbase')

c = conn.cursor()

c.execute("""PRAGMA journal_mode = OFF""")
c.execute("""PRAGMA synchronous = 0""")
c.execute("""PRAGMA locking_mode = EXCLUSIVE""")
c.execute("""PRAGMA count_change = OFF""")
c.execute("""PRAGMA temp_store = 2""")

c.execute("""CREATE TABLE tmpTranscripts_arm_3R_transcripts (id INTEGER PRIMARY KEY, name varchar(255), chromosome varchar(255), start int(11), end int(11), direction tinyint(4), tags varchar(1023), bin int(11), exons varchar(10000))""")
c.execute("""CREATE INDEX 'iTranscript_arm_3R_14943' ON 'tmpTranscripts_arm_3R_transcripts' (start, end, direction)""")

t1 = time()

for i in range(0, 5000000):
    c.execute("""INSERT INTO tmpTranscripts_arm_3R_transcripts (name, chromosome, start, end, direction, tags, bin, exons) VALUES ('SRR060644.1', 'arm_3R', %d, %d, %d, 'feature=transcript;bestRegion=(self);nbGaps=0;nbMismatches=0;ID=SRR060644.1;identity=100.0', 300011450, '')""" % ((i+123)%352, (i+523)%422, (i+866)%536))
    if(not i%10000):
        print("Insert:", i)

t2 = time()
print("Insert time", t2-t1)

conn.commit()

t1 = time()
c.execute("""SELECT * FROM tmpTranscripts_arm_3R_transcripts ORDER BY start, end, direction""")

i = 0
for row in c:
    a = row[0]
    if(not i%10000):
        print("Get:", i, row)
    i+=1

t2 = time()
print("Sort time", t2-t1)

c.close()

およびMongoDBの場合

from pymongo import Connection
from pymongo import ASCENDING, DESCENDING
from time import time

connection = Connection()
connection = Connection('localhost', 27017)
db = connection['test-database']
collection = db['test-collection']
posts = db.posts

posts.create_index([("start", ASCENDING), ("end", ASCENDING), ("direction", ASCENDING)])

t1 = time()

for i in range(0, 5000000):
    post = { "name": 'SRR060644.1',
            "chromosome": 'arm_3R',
            "start": (i+123)%352,
            "end": (i+523)%422,
            "direction": (i+866)%536,
            "tags": 'feature=transcript;bestRegion=(self);nbGaps=0;nbMismatches=0;ID=SRR060644.1;identity=100.0',
            "bin": 300011450,
            "exons": ''}

    posts.insert(post)

    if(not i%10000):
        print("Insert:", i)

t2 = time()
print("Insert time", t2-t1)

t1 = time()

i = 0
for post in posts.find().sort([("start", ASCENDING), ("end", ASCENDING), ("direction", ASCENDING)]):
    if(not i%10000):
        print("Get:", i, post)
    i+=1

t2 = time()
print("Sort time", t2-t1)
于 2011-10-07T17:48:55.143 に答える