Try now Products Featured Developers Product Managers IT professionals Business Teams Leadership Teams Featured Developers Product Managers IT professionals Business Teams Leadership Teams
はじめに pythonからMySQLを叩く際,何を使っていますか?SQLAlchemy,Django,peeweeあたりを使っている方が多いのではないでしょうか.私は色々使ってみて結局SQLAlchemyに落ち着いていますが,何千万,何億オーダのデータになると処理が遅く非常にやっかいです. なので今回は(大きなデータにsql使うなとか高速処理にpython使うなという話は一旦置いておいて),SQLAlchemyを使ったデータ処理をいかに高速にするか,何項目かに分けて備忘録を残しておきます. SQLAlchemyを~と書いてありますが,SQLAlchemyを使ってDBにデータをINSERT,SELECTする際のpython高速化TIPSごった煮です.本当は記事を分けたほうが良いのですが,自分で見返す際にまとまっているほうが嬉しいのでこうしました. なお,今回使用するコードは全てgithubに
PythonのフレームワークであるFastAPIを触る機会があって、その中で非同期プログラミングの存在を知りました。 非同期プログラミングについて色々調べていくと、DB接続も非同期処理に対応してきていることが分かり、PythonのSQLライブラリで有名なSQLAlchemyもバージョン1.4から非同期処理に対応しているようです。 時代は非同期プログラミングみたいですね。 ただSQLAlchemyの非同期処理の記事がまだ少なく、同期処理から非同期処理に変えるときに、苦労しそうなので、今回はSQLAlchemy同期処理と非同期処理で設定やクエリがどう違うの比較しようと思います。 SQLAlchemyにはcoreとormがありますが、ここではormで書いていきます。 APIにはFastAPI、DBはPostgreSQLを使っていきます。 動作確認用コードは下記リポジトリに置いています。 同期処理
from sqlalchemy import create_engine, MetaData from sqlalchemy.orm import sessionmaker, declarative_base from databases import Database DATABASE_URL = 'postgresql://postgres:password@db:5432/db_name' engine = create_engine(DATABASE_URL) Session = sessionmaker(autocommit=False, autoflush=False, bind=engine) session = Session() Base = declarative_base() from sqlalchemy import ( Column, DateTime, Int
少し前にSQLAlchemy 1.4がリリースされました。 このSQLAlchemy 1.4には大きな特徴として次の2点があります。 SQLAlchemy ORMでこれまでの記法(1.x Style)に加えて、2.0 Styleと呼ばれる新しい記法に対応 CoreとORMの両方でイベントループを使った非同期処理に対応 今後のロードマップでは、SQLAlchemy 2.0では2.0 Styleのみがサポートされます。 また、イベントループを使った非同期処理もこの数年で一気に広まってきました。 SQLAlchemyが対応したこともこの流れをさらに加速させると思います。 そこで最近よく利用しているFastAPIと組み合わせて、SQLAlchemy 2.0 Styleを使った非同期Web APIのサンプルプロジェクトを用意しました。 コードはrhoboro/async-fastapi-sqlalc
PlaySQLAlchemy: SQLAlchemy入門1. SQLAlchemy 入門 for Kobe Python Meetup #13 2017/09/15 Kobe Japan 2. Yasushi Masuda PhD ( @whosaysni ) Tech team, Core IT grp. IT Dept. MonotaRO Co., LTD. Pythonista since 2001 (2.0~) • elaphe (barcode library) • oikami.py (老神.py) • PyCon JP founder 翻訳もろもろ 3. アジェンダ よくある誤解 SQLAlchemyを3行で エンジンの基礎 (+hands-on) SQL式を使う (+hands-on) ORMの基礎 (+hands-on) 4. 参考文献 オンラインドキュメント: http
PythonのO/R Mapperの1つであるSQLAlchemyでDB検索する際にfilter()とfilter_by()の2つの関数があるので違いを説明します。 公式ドキュメントを見れば明白でした。 filter_by(), filter() 結論から言うと、fileter_by()の方がコードが短くなる利点があるようです。 filter():{モデル}.{変数名}でテーブルのカラムの値を検索条件に追加可能 filter_by(): モデルなしで、{変数名}だけでカラムの値を検索条件に追加可能 コードで見るとわかりやすいので明記すると # filter()を使う場合の記述例 session.query(MyClass).\ filter(MyClass.name == 'some name', MyClass.id > 5) # filter_by()を使う場合の記述例 session
はじめに 先日、社内のオンライン開発合宿というイベント向けに、アプリケーションを作る一環として、 (コロナ禍もあってオンライン...) REST APIの作成は完了したので、 今度はFlaskにおけるDBマイグレーションの実装とSeeding(テストデータのINSERT処理)を 実装・検証したので、共有したいと思います。 DBマイグレーションとは ソフトウェア工学において、スキーママイグレーション(データベースマイグレーション、データベースチェンジマネジメント)とは、リレーショナルデータベースのスキーマに対する増分的、可逆的な変更とバージョン管理の管理を指す。スキーママイグレーションは、データベースのスキーマを更新したり、新しいバージョンや古いバージョンに戻したりする必要がある場合に、データベースに対して実行されます。 参照元:wikipedia ※DeepL翻訳 ふむふむ、データベースの
root@9a7582105665:/usr/src# pip install alembic psycopg2-binary Collecting alembic Downloading alembic-1.4.2.tar.gz (1.1 MB) |████████████████████████████████| 1.1 MB 7.8 MB/s Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done Collecting psycopg2-binary Downloading psycopg2_binary-2.8.5-cp38-cp38-manylinux1_x86_64.whl (3.0 MB) |███
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く