問題
我正在寫一個應用程序引擎卡拉OK目錄應用程序。該應用非常簡單:在第一個版本中,它提供了將CSV歌曲列表導入到目錄並顯示它們的功能。導入CSV時間太長
我遇到了CSV導入問題:在我的開發環境中導入17,500條記錄需要很長時間(14小時)。在生產環境中,它導入了大約1000條記錄,然後與代碼500一起墜毀。我正在瀏覽日誌,但沒有找到任何有用的線索。
守則
class Song(ndb.Model):
sid = ndb.IntegerProperty()
title = ndb.StringProperty()
singer = ndb.StringProperty()
preview = ndb.StringProperty()
@classmethod
def new_from_csv_row(cls, row, parent_key):
song = Song(
sid=int(row['sid']),
title=row['title'],
singer=row['singer'],
preview=row['preview'],
key=ndb.Key(Song, row['sid'], parent=parent_key))
return song
class CsvUpload(webapp2.RequestHandler):
def get(self):
# code omit for brevity
def post(self):
catalog = get_catalog(…) # retrieve old catalog or create new
# upfile is the contents of the uploaded file, not the filename
# because the form uses enctype="multipart/form-data"
upfile = self.request.get('upfile')
# Create the songs
csv_reader = csv.DictReader(StringIO(upfile))
for row in csv_reader:
song = Song.new_from_csv_row(row, catalog.key)
song.put()
self.redirect('/upload')
樣本數據
sid,title,singer,preview
19459,Zoom,Commodores,
19460,Zoot Suit Riot,Cherry Poppin Daddy,
19247,You Are Not Alone,Michael Jackson,Another day has gone. I'm still all alone
注
- 在開發環境中,我試圖導入多達17500條記錄,並沒有遇到崩潰
- 在首先,創建並快速插入記錄,但是a如果數據庫增長到數千,那麼創建和插入記錄所用的時間就會增加到每個記錄幾秒鐘。
如何加快導入操作?任何建議,提示或提示將不勝感激。
更新
我也跟着墨菲的建議和使用的KeyProperty
一首歌鏈接回目錄。結果是大約4分20秒17,500條記錄 - 這是一個巨大的進步。這意味着,我並沒有完全理解NDB如何在App Engine中工作,而且我還有很長的路要學習。
儘管有了很大的改進,但4分鐘以上仍然過長。我現在正在研究Tim和Dave的建議,以進一步縮短我的應用程序的預期響應時間。
謝謝,Murph。我願意改變,但有點無知。請您詳細說明KeyProperty? –
當然! ndb.KeyProperty()存儲數據存儲區密鑰。所以,如果你想添加一些東西到你的歌曲模型中,比如'catalog = ndb.KeyProperty()',然後當你創建歌曲時,你只需要在Song(...)構造函數中加入'catalog = parent_key'。然後你就可以拿一首歌並獲得它的父母 - 你只需要做somesong.catalog.get()。 – Murph
我試試看。再一次感謝你。 –