我用python和flask开始我的冒险。
我使用Flask-SQLAlchemy进行MySQL / MariaDB任务
class Local(db.Model):
__tablename__ = 'local'
id = db.Column("VideoLocalID", db.Integer, primary_key=True)
file_path = db.Column("FilePath", db.String)
hash = db.Column("Hash", db.String)
class Remote(db.Model):
__tablename__ = 'remote'
id = db.Column("EpisodeID", db.Integer, primary_key=True)
# hash = db.relationship('Local', backref='hash', lazy='dynamic')
hash = db.Column("Hash", db.String)
#hash = db.relationship(db.String, db.ForeignKey('local.hash'))
percentage = db.Column("Percentage", db.Integer)
我试图获取所有不会"哈希"不存在于两个表中。我只对本地表中的项目感兴趣。
就像你看到我试图让FK工作但没有成功
localv = Local.query.all()
remotev = Remote.query.all()
for video in localv:
for video_remote in remotev:
#checking if there is a match if not adding to list
我认为我的双循环是糟糕的。有没有办法加快速度,甚至可以在数据库而不是本地进行?
答案 0 :(得分:0)
最简洁的方法是
# get all rows with distinct (unique) hashes from Remote and create a list of hashes.
remote_hash_list = [item.hash for item in Remote.query.distinct(Remote.hash).all()]
# Now get all Local items that hash is not in remote hashes list
local_items = Local.query.filter(Local.hash.notin_(remote_hash_list)).all()