\u53ea\u9700\u8981\u8ba1\u6570\u4f46\u68c0\u7d22\u6574\u4e2a\u7ed3\u679c\u96c6<\/strong><\/div>\n\u6709\u65f6\u5f00\u53d1\u4eba\u5458\u53ea\u9700\u8981\u4e00\u4e2a\u7ed3\u679c\u8ba1\u6570\uff0c\u4f46\u662f\u6ca1\u6709\u4f7f\u7528\u6570\u636e\u5e93\u8ba1\u6570\u529f\u80fd\uff0c\u800c\u662f\u83b7\u53d6\u4e86\u6240\u6709\u7ed3\u679c\uff0c\u7136\u540e\u4f7f\u7528 Python \u4e2d\u7684 len \u5b8c\u6210\u8ba1\u6570\u3002<\/p>\n
count = len(User.query.filter_by(acct_active=True).all())<\/pre>\n\u76f8\u53cd\uff0c\u4f7f\u7528 SQLAlchemy \u7684 count \u65b9\u6cd5\u5c06\u5728\u670d\u52a1\u5668\u7aef\u6267\u884c\u8ba1\u6570\uff0c\u4ece\u800c\u51cf\u5c11\u53d1\u9001\u5230\u5ba2\u6237\u7aef\u7684\u6570\u636e\u3002\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u8c03\u7528 all() \u4e5f\u4f1a\u5bfc\u81f4\u6a21\u578b\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316\uff0c\u5982\u679c\u6709\u5f88\u591a\u6570\u636e\uff0c\u90a3\u4e48\u65f6\u95f4\u4ee3\u4ef7\u53ef\u80fd\u4f1a\u975e\u5e38\u6602\u8d35\u3002<\/p>\n
\u9664\u975e\u8fd8\u9700\u8981\u505a\u5176\u4ed6\u7684\u4e8b\u60c5\uff0c\u5426\u5219\u53ea\u9700\u4f7f\u7528 count \u65b9\u6cd5\uff1a<\/p>\n
count = User.query.filter_by(acct_active=True).count()<\/pre>\n\u53ea\u9700\u8981\u51e0\u5217\u65f6\u68c0\u7d22\u6574\u4e2a\u6a21\u578b<\/strong><\/div>\n\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u53d1\u51fa\u67e5\u8be2\u65f6\u53ea\u9700\u8981\u51e0\u5217\u6570\u636e\u3002SQLAlchemy \u53ef\u4ee5\u53ea\u83b7\u53d6\u4f60\u60f3\u8981\u7684\u5217\uff0c\u800c\u4e0d\u662f\u8fd4\u56de\u6574\u4e2a\u6a21\u578b\u5b9e\u4f8b\u3002\u8fd9\u4e0d\u4ec5\u51cf\u5c11\u4e86\u53d1\u9001\u7684\u6570\u636e\u91cf\uff0c\u8fd8\u907f\u514d\u4e86\u5b9e\u4f8b\u5316\u6574\u4e2a\u5bf9\u8c61\u3002\u4f7f\u7528\u5217\u6570\u636e\u7684\u5143\u7ec4\u800c\u4e0d\u662f\u6a21\u578b\u53ef\u4ee5\u5feb\u5f97\u591a\u3002<\/p>\n
result = User.query.all()\r\nfor user in result:\r\n print(user.name, user.email)<\/pre>\n\u53cd\u4e4b\uff0c\u4f7f\u7528 with_entities \u65b9\u6cd5\u53ea\u9009\u62e9\u6240\u9700\u8981\u7684\u5185\u5bb9\uff1a<\/p>\n
result = User.query.with_entities(User.name, User.email).all()\r\nfor (username, email) in result:\r\n print(username, email)<\/pre>\n\u6bcf\u6b21\u5faa\u73af\u90fd\u66f4\u65b0\u4e00\u4e2a\u5bf9\u8c61<\/strong><\/div>\n\u907f\u514d\u4f7f\u7528\u5faa\u73af\u6765\u5355\u72ec\u66f4\u65b0\u96c6\u5408\u3002\u867d\u7136\u6570\u636e\u5e93\u53ef\u4ee5\u975e\u5e38\u5feb\u5730\u6267\u884c\u5355\u4e2a\u66f4\u65b0\uff0c\u4f46\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u5f80\u8fd4\u65f6\u95f4\u5c06\u5feb\u901f\u7d2f\u52a0\u3002\u901a\u5e38\uff0c\u5728\u5408\u7406\u7684\u60c5\u51b5\u4e0b\u4e89\u53d6\u66f4\u5c11\u7684\u67e5\u8be2\u3002<\/p>\n
for user in users_to_update:\r\n user.acct_active = True\r\n db.session.add(user)<\/pre>\n\u6539\u7528\u6279\u91cf\u66f4\u65b0\u65b9\u6cd5\uff1a<\/p>\n
query = User.query.filter(user.id.in_([user.id for user in users_to_update]))\r\nquery.update({\"acct_active\": True}, synchronize_session=False)<\/pre>\n\u89e6\u53d1\u7ea7\u8054\u5220\u9664<\/strong><\/div>\nORM \u5141\u8bb8\u5728\u6a21\u578b\u5173\u7cfb\u4e0a\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u5fae\u5999\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u4ee4\u4eba\u5403\u60ca\u3002\u5927\u591a\u6570\u6570\u636e\u5e93\u901a\u8fc7\u5916\u952e\u548c\u5404\u79cd\u7ea7\u8054\u9009\u9879\u7ef4\u62a4\u5173\u7cfb\u5b8c\u6574\u6027\u3002SQLAlchemy \u5141\u8bb8\u4f60\u4f7f\u7528\u5916\u952e\u548c\u7ea7\u8054\u9009\u9879\u5b9a\u4e49\u6a21\u578b\uff0c\u4f46\u662f ORM \u5177\u6709\u81ea\u5df1\u7684\u7ea7\u8054\u903b\u8f91\uff0c\u53ef\u4ee5\u53d6\u4ee3\u6570\u636e\u5e93\u3002<\/p>\n
\u8003\u8651\u4ee5\u4e0b\u6a21\u578b\uff1a<\/p>\n
class Artist(Base):\r\n __tablename__ = \"artist\"\r\n id = Column(Integer, primary_key=True)\r\n songs = relationship(\"Song\", cascade=\"all, delete\")\r\nclass Song(Base):\r\n __tablename__ = \"song\"\r\n id = Column(Integer, primary_key=True)\r\n artist_id = Column(Integer, ForeignKey(\"artist.id\", ondelete=\"CASCADE\"))<\/pre>\n\u5220\u9664\u6b4c\u624b\u5c06\u5bfc\u81f4 ORM \u5728 song \u8868\u4e0a\u53d1\u51fa delete \u67e5\u8be2\uff0c\u4ece\u800c\u9632\u6b62\u7531\u4e8e\u5916\u952e\u5bfc\u81f4\u7684\u5220\u9664\u64cd\u4f5c\u3002\u8fd9\u79cd\u884c\u4e3a\u53ef\u80fd\u4f1a\u6210\u4e3a\u590d\u6742\u5173\u7cfb\u548c\u5927\u91cf\u8bb0\u5f55\u7684\u74f6\u9888\u3002<\/p>\n
\u8bf7\u5305\u542b passive_deletes \u9009\u9879\uff0c\u4ee5\u786e\u4fdd\u8ba9\u6570\u636e\u5e93\u6765\u7ba1\u7406\u5173\u7cfb\u3002\u4f46\u662f\uff0c\u8bf7\u786e\u4fdd\u4f60\u7684\u6570\u636e\u5e93\u5177\u6709\u6b64\u529f\u80fd\u3002\u4f8b\u5982\uff0cSQLite \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u7ba1\u7406\u5916\u952e\u3002<\/p>\n
songs = relationship(\"Song\", cascade all, delete\", passive_deletes=True)<\/pre>\n\u5f53\u8981\u4f7f\u7528\u8d2a\u5a6a\u52a0\u8f7d\u65f6\uff0c\u5e94\u4f7f\u7528\u5ef6\u8fdf\u52a0\u8f7d<\/strong><\/div>\n\u5ef6\u8fdf\u52a0\u8f7d\u662f SQLAlchemy \u5904\u7406\u5173\u7cfb\u7684\u9ed8\u8ba4\u65b9\u6cd5\u3002\u4ece\u4e0a\u4e00\u4e2a\u4f8b\u5b50\u6784\u5efa\u6765\u770b\uff0c\u52a0\u8f7d\u4e00\u4e2a\u6b4c\u624b\u65f6\u4e0d\u4f1a\u540c\u65f6\u52a0\u8f7d\u4ed6\u6216\u5979\u7684\u6b4c\u66f2\u3002\u8fd9\u901a\u5e38\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4f46\u662f\u5982\u679c\u603b\u662f\u9700\u8981\u52a0\u8f7d\u67d0\u4e9b\u5173\u7cfb\uff0c\u5355\u72ec\u7684\u67e5\u8be2\u53ef\u80fd\u4f1a\u9020\u6210\u6d6a\u8d39\u3002<\/p>\n
\u5982\u679c\u5141\u8bb8\u4ee5\u5ef6\u8fdf\u65b9\u5f0f\u52a0\u8f7d\u5173\u7cfb\uff0c\u50cf Marshmallow \u8fd9\u6837\u6d41\u884c\u7684\u5e8f\u5217\u5316\u6846\u67b6\u53ef\u4ee5\u89e6\u53d1\u7ea7\u8054\u67e5\u8be2\u3002<\/p>\n
\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63a7\u5236\u6b64\u884c\u4e3a\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u901a\u8fc7 relationship \u51fd\u6570\u672c\u8eab\u3002<\/p>\n
songs = relationship(\"Song\", lazy=\"joined\", cascade=\"all, delete\")<\/pre>\n\u8fd9\u5c06\u5bfc\u81f4\u4e00\u4e2a\u5de6\u8fde\u63a5\u88ab\u6dfb\u52a0\u5230\u4efb\u4f55\u6b4c\u624b\u7684\u67e5\u8be2\u4e2d\uff0c\u56e0\u6b64\uff0csongs \u96c6\u5408\u5c06\u7acb\u5373\u53ef\u7528\u3002\u5c3d\u7ba1\u6709\u66f4\u591a\u6570\u636e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u4f46\u5f80\u8fd4\u6b21\u6570\u53ef\u80fd\u4f1a\u5c11\u5f97\u591a\u3002<\/p>\n
SQLAlchemy \u4e3a\u65e0\u6cd5\u91c7\u7528\u8fd9\u79cd\u7efc\u5408\u65b9\u6cd5\u7684\u60c5\u51b5\u63d0\u4f9b\u4e86\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528 joinedload() \u51fd\u6570\u5728\u6bcf\u4e2a\u67e5\u8be2\u7684\u57fa\u7840\u4e0a\u5207\u6362\u8fde\u63a5\u7684\u52a0\u8f7d\u3002<\/p>\n
from sqlalchemy.orm import joinedload\r\nartists = Artist.query.options(joinedload(Artist.songs))\r\nprint(artists.songs) # Does not incur a roundtrip to load<\/pre>\n\u4f7f\u7528 ORM \u8fdb\u884c\u6279\u91cf\u8bb0\u5f55\u5bfc\u5165<\/strong><\/div>\n\u5bfc\u5165\u6210\u5343\u4e0a\u4e07\u6761\u8bb0\u5f55\u65f6\uff0c\u6784\u5efa\u5b8c\u6574\u6a21\u578b\u5b9e\u4f8b\u7684\u5f00\u9500\u4f1a\u6210\u4e3a\u4e3b\u8981\u74f6\u9888\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u4ece\u4e00\u4e2a\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6570\u5343\u9996\u6b4c\u66f2\u8bb0\u5f55\uff0c\u5176\u4e2d\u6bcf\u9996\u6b4c\u66f2\u90fd\u5148\u88ab\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n
for song in songs:\r\n db.session.add(Song(`song))<\/pre>\n\u76f8\u53cd\uff0c\u7ed5\u8fc7 ORM\uff0c\u53ea\u4f7f\u7528\u6838\u5fc3\u7684 SQLAlchemy \u53c2\u6570\u7ed1\u5b9a\u529f\u80fd\u3002<\/p>\n
batch = []\r\ninsert_stmt = Song.__table__.insert()\r\nfor song in songs:\r\n if len(batch) > 1000:\r\n db.session.execute(insert_stmt, batch)\r\n batch.clear()\r\n batch.append(song)\r\nif batch:\r\n db.session.execute(insert_stmt, batch)<\/pre>\n\u8bf7\u8bb0\u4f4f\uff0c\u6b64\u65b9\u6cd5\u4f1a\u81ea\u7136\u800c\u7136\u5730\u8df3\u8fc7\u4f60\u53ef\u80fd\u4f9d\u8d56\u7684\u4efb\u4f55\u5ba2\u6237\u7aef ORM \u903b\u8f91\uff0c\u4f8b\u5982\u57fa\u4e8e Python \u7684\u5217\u9ed8\u8ba4\u503c\u3002\u5c3d\u7ba1\u6b64\u65b9\u6cd5\u6bd4\u5c06\u5bf9\u8c61\u52a0\u8f7d\u4e3a\u5b8c\u6574\u7684\u6a21\u578b\u5b9e\u4f8b\u8981\u5feb\uff0c\u4f46\u662f\u4f60\u7684\u6570\u636e\u5e93\u53ef\u80fd\u5177\u6709\u66f4\u5feb\u7684\u6279\u91cf\u52a0\u8f7d\u65b9\u6cd5\u3002\u4f8b\u5982\uff0cPostgreSQL \u7684 COPY \u547d\u4ee4\u4e3a\u52a0\u8f7d\u5927\u91cf\u8bb0\u5f55\u63d0\u4f9b\u4e86\u6700\u4f73\u6027\u80fd\u3002<\/p>\n
\u8fc7\u65e9\u8c03\u7528\u63d0\u4ea4\u6216\u5237\u65b0<\/strong><\/div>\n\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u4f60\u9700\u8981\u5c06\u5b50\u8bb0\u5f55\u4e0e\u5176\u7236\u8bb0\u5f55\u76f8\u5173\u8054\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4e00\u79cd\u663e\u7136\u7684\u65b9\u6cd5\u662f\u5237\u65b0\u4f1a\u8bdd\uff0c\u4ee5\u4fbf\u4e3a\u6709\u95ee\u9898\u7684\u8bb0\u5f55\u5206\u914d\u4e00\u4e2a ID\u3002<\/p>\n
artist = Artist(name=\"Bob Dylan\")\r\nsong = Song(title=\"Mr. Tambourine Man\")\r\ndb.session.add(artist)\r\ndb.session.flush()\r\nsong.artist_id = artist.id<\/pre>\n\u5bf9\u4e8e\u6bcf\u4e2a\u8bf7\u6c42\uff0c\u591a\u6b21\u63d0\u4ea4\u6216\u5237\u65b0\u901a\u5e38\u662f\u4e0d\u5fc5\u8981\u7684\uff0c\u4e5f\u662f\u4e0d\u53ef\u53d6\u7684\u3002\u6570\u636e\u5e93\u5237\u65b0\u6d89\u53ca\u5f3a\u5236\u5728\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e0a\u8fdb\u884c\u78c1\u76d8\u5199\u5165\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5ba2\u6237\u7aef\u5c06\u963b\u585e\uff0c\u76f4\u5230\u670d\u52a1\u5668\u786e\u8ba4\u5df2\u5199\u5165\u6570\u636e\u4e3a\u6b62\u3002<\/p>\n
SQLAlchemy \u53ef\u4ee5\u5728\u5e55\u540e\u8ddf\u8e2a\u5173\u7cfb\u548c\u7ba1\u7406\u76f8\u5173\u952e\u3002<\/p>\n
artist = Artist(name=\"Bob Dylan\")\r\nsong = Song(title=\"Mr. Tambourine Man\")\r\nartist.songs.append(song)<\/pre>\n\u603b\u7ed3<\/strong><\/div>\n\u6211\u5e0c\u671b\u8fd9\u4e00\u7cfb\u5217\u5e38\u89c1\u7684\u9677\u9631\u53ef\u4ee5\u5e2e\u52a9\u4f60\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\uff0c\u5e76\u4f7f\u4f60\u7684\u5e94\u7528\u5e73\u7a33\u8fd0\u884c\u3002\u901a\u5e38\uff0c\u5728\u8bca\u65ad\u6027\u80fd\u95ee\u9898\u65f6\uff0c\u6d4b\u91cf\u662f\u5173\u952e\u3002\u5927\u591a\u6570\u6570\u636e\u5e93\u90fd\u63d0\u4f9b\u6027\u80fd\u8bca\u65ad\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b9a\u4f4d\u95ee\u9898\uff0c\u4f8b\u5982 PostgreSQL \u7684 pg_stat_statements \u6a21\u5757\u3002<\/p>\n
via: https:\/\/opensource.com\/article\/19\/9\/common-pitfalls-python<\/p>\n
\u4f5c\u8005\uff1aZach Todd \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1aMjSeven \u6821\u5bf9\uff1awxy<\/p>\n
\u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04Object-relational mapping\uff08ORM\uff09\u4f7f\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u7684\u5de5\u4f5c\u66f4\u8f7b\u677e\uff0c\u5728\u5f88\u5927 […]<\/p>\n","protected":false},"author":1482,"featured_media":160637,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-165599","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=165599"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165599\/revisions"}],"predecessor-version":[{"id":165629,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/165599\/revisions\/165629"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160637"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=165599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=165599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=165599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}