Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python and Relational/Non-relational Databases
Search
Andrew Godwin
October 22, 2010
Programming
0
140
Python and Relational/Non-relational Databases
A talk I gave at PyCon Ukraine 2010.
Andrew Godwin
October 22, 2010
Tweet
Share
More Decks by Andrew Godwin
See All by Andrew Godwin
Reconciling Everything
andrewgodwin
1
350
Django Through The Years
andrewgodwin
0
260
Writing Maintainable Software At Scale
andrewgodwin
0
470
A Newcomer's Guide To Airflow's Architecture
andrewgodwin
0
380
Async, Python, and the Future
andrewgodwin
2
700
How To Break Django: With Async
andrewgodwin
1
760
Taking Django's ORM Async
andrewgodwin
0
760
The Long Road To Asynchrony
andrewgodwin
0
720
The Scientist & The Engineer
andrewgodwin
1
800
Other Decks in Programming
See All in Programming
大体よく分かるscala.collection.immutable.HashMap ~ Compressed Hash-Array Mapped Prefix-tree (CHAMP) ~
matsu_chara
2
220
S3 VectorsとStrands Agentsを利用したAgentic RAGシステムの構築
tosuri13
6
350
AIエージェントを活かすPM術 AI駆動開発の現場から
gyuta
0
440
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.9k
エディターってAIで操作できるんだぜ
kis9a
0
740
バックエンドエンジニアによる Amebaブログ K8s 基盤への CronJobの導入・運用経験
sunabig
0
160
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
530
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
760
AIコーディングエージェント(Manus)
kondai24
0
200
これならできる!個人開発のすゝめ
tinykitten
PRO
0
120
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
370
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
160
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
HDC tutorial
michielstock
0
260
Git: the NoSQL Database
bkeepers
PRO
432
66k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.1k
The untapped power of vector embeddings
frankvandijk
1
1.5k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
45
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
29
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
310
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
62
Designing for humans not robots
tammielis
254
26k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
73
Six Lessons from altMBA
skipperchong
29
4.1k
Transcript
Relational / Non-relational Databases Python and Andrew Godwin
Introduction Python for 5 years Django core developer Data modelling
/ visualisation
""Andrew speaks English like a machine gun speaks bullets."" Reinout
van Rees
If I speak too fast - tell me!
What is a relational database?
A relational database is a “collection of relations”
It's what a lot of people are used to.
Relational Databases PostgreSQL MySQL SQLite
Let's pick PostgreSQL (it's a good choice)
Usage conn = psycopg2.connect( host="localhost", user="postgres" ) cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE username = "andrew";') for row in cursor.fetchall(): print row
You've probably seen all that before.
Now, to introduce some non-relational databases
Document Databases MongoDB CouchDB
Key-Value Stores Redis Cassandra
Message Queues AMQP Celery
Various Others Graph databases Filesystems VCSs
Redis and MongoDB are two good examples here
Redis: Key-value store with strings, lists, sets, channels and atomic
operations.
Redis Example conn = redis.Redis(host="localhost") print conn.get("top_value") conn.set("last_user", "andrew") conn.inc("num_runs")
conn.sadd("users", "andrew") conn.sadd("users", "martin") for item in conn.smembers("users"): print item
MongoDB: Document store with indexing and a wide range of
query filters.
MongoDB Example conn = pymongo.Connection("localhost") db = conn['mongo_example'] coll =
db['users'] coll.insert({ "username": "andrew", "uid": 1000, }) for entry in coll.find({"username": "andrew"}): print entry
These all solve different problems - you can't easily replace
one with the other.
""When all you have is a hammer, everything looks like
a nail"" Abraham Manslow (paraphrased)
JOIN - your best friend, and your worst enemy.
Denormalising your data speeds up reads, and slows down writes.
Schemaless != Denormalised
Atomic operations are nice. conn.incrby("num_users', 2)
But SQL can do some of them. UPDATE foo SET
bar = bar + 1 WHERE baz;
Redis, the datastructures server. SETNX, GETSET, EXPIRES and friends
Locks / Semaphores conn.setnx("lock:foo", time.time() + 3600) val = conn.decr("sem:foo")
if val >= 0: ... else: conn.incr("sem:foo")
Queues conn.lpush("myqueue", "workitem") todo = conn.lpop("myqueue") (or publish/subscribe)
Priority Queues conn.zadd("myqueue", "handle-meltdown", 1) conn.zadd("myqueue", "feed-cats", 5) todo =
conn.zrange("myqueue", 0, 1) conn.zrem(todo)
Lock-free linked lists! new_id = "bgrdsd" old_end = conn.getset(":end", new_id)
conn.set("%s:next" % old_end, new_id)
Performance-wise, the less checks/integrity the faster it goes.
Maturity can sometimes be an issue, but new features can
appear rapidly.
You can also use databases for the wrong thing -
it often only matters ""at scale""
But how does this all relate to Python?
Most databases - even new ones - have good Python
bindings
Postgres: PsycoPG2 Redis: redis-py MongoDB: pymongo (and more - neo4j,
VCSen, relational, etc.)
Some databases have Python available inside (Postgres has it as
an option)
Document databases map really well to Python dicts
You may find non-relational databases a nicer way to store
state - for any app
Remember, you might still need transactions/reliability. (Business logic is probably
better off on mature systems for now)
Overall? Just keep all the options in mind. Don't get
caught by trends, and don't abuse your relational store
Thanks. Andrew Godwin @andrewgodwin http://aeracode.org