[英]Django database to postgresql
大約有5000家公司,每家公司的價格大約為4500,因此,總價格約為22,000,000。
不久前,我寫了一個代碼,以如下格式存儲此數據:
class Endday(models.Model):
company = models.TextField(null=True)
eop = models.CommaSeparatedIntegerField(blank=True, null=True, max_length=50000)
為了存儲,代碼是-
for i in range(1, len(contents)):
csline = contents[i].split(",")
prices = csline[1:len(csline)]
company = csline[0]
entry = Endday(company=company, eop=prices)
entry.save()
雖然,代碼很慢(很明顯),但是它確實起作用並將數據存儲在數據庫中。 有一天,我決定刪除Endday的所有內容,然后嘗試再次存儲。 但是它沒有工作,並向我拋出錯誤, Database locked
。
無論如何,我做了一些研究,才知道MySql無法處理這么多數據。 那么,它是如何首先存儲的呢? 我得出的結論是,所有這些價格都是從一開始就存儲的,之后很多都已存儲在數據庫中,因此不會被存儲。
經過一些研究,我知道我應該使用PostgreSql,因此我更改了數據庫,進行了遷移,然后繼續嘗試代碼,但是沒有運氣。 我收到一個錯誤消息,說-
psycopg2.DataError: value too long for type character varying(50000)
好吧,所以我bulk_create
我們嘗試使用bulk_create
並修改一下代碼,但是由於同樣的錯誤我受到了歡迎。
接下來,我想也許可以制作兩個模型,一個模型保存公司名稱,另一個模型保存該公司的價格和鑰匙。 再次,我更改了代碼-
class EnddayCompanies(models.Model):
company = models.TextField(max_length=500)
class Endday(models.Model):
foundation = models.ForeignKey(EnddayCompanies, null=True)
eop = models.FloatField(null=True)
和意見-
to_be_saved = []
for i in range(1, len(contents)):
csline = contents[i].split(",")
prices = csline[1:len(csline)]
company = csline[0]
companies.append(csline[0])
prices =[float(x) for x in prices]
before_save = []
for j in range(len(prices)):
before_save.append(Endday(company=company, eop=prices[j]))
to_be_saved.append(before_save)
Endday.objects.bulk_create(to_be_saved)
但是令我驚訝的是,這是如此緩慢,以至於在中間,它只是停在一家公司上。 我試圖找出哪個特定的代碼使它變慢,並且-
before_save = []
for j in range(len(prices)):
before_save.append(Endday(company=company, eop=prices[j]))
to_be_saved.append(before_save)
好吧,現在我回到正題,並且我什么都想不起來,所以我按了SO的鈴鐺。 我現在有的問題-
我認為您可以為Company
和Price
創建一個單獨的模型,如下所示:
class Company(models.Model):
name = models.CharField(max_length=20)
class Price(models.Model):
company = models.ForeignKey(Company, related_name='prices')
price = models.FloatField()
這是保存數據的方式:
# Assuming that contents is a list of strings with a format like this:
contents = [
'Company 1, 1, 2, 3, 4...',
'Company 2, 1, 2, 3, 4...',
....
]
for content in contents:
tokens = content.split(',')
company = Company.objects.create(name=tokens[0])
Price.objects.bulk_create(
Price(company=company, price=float(x.strip()))
for x in tokens[1:]
)
# Then you can call prices now from company
company.prices.order_by('price')
更新:我剛剛注意到它與您的第二種實現類似,唯一的區別是保存數據的方式。 我的實現迭代次數較少。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.