AI 资讯
Database Indexing and Query Optimization for Python Developers
Introduction Fixing N+1 queries with select_related / prefetch_related or selectinload (see the previous post ) gets you down to a small, sane number of queries per request. The next bottleneck is what each query costs once the table has millions of rows — and that is almost always about indexing. An index turns "scan every row" into "look it up directly." Skip it, and a query that's instant in development takes seconds once real data volume shows up in production. How Indexes Work: The B-Tree Intuition Without an index, a WHERE clause forces a sequential scan : the database reads every row and checks the condition — O(n) , cost grows linearly with table size. An index is a separate, sorted structure (almost always a B-tree ) mapping column values to row locations. Because it's sorted and balanced, finding a value is a tree walk: O(log n) . On a 10-million-row table, that's the difference between reading 10 million rows and roughly 23 tree nodes. This isn't free: Writes get slower — every INSERT / UPDATE / DELETE on an indexed column also updates the index. Storage grows — each index is a sorted copy of (part of) the data. An index trades write cost and storage for read speed. Indexing a column you rarely filter or sort on is pure cost, no benefit. Reading Query Plans: EXPLAIN ANALYZE Postgres' EXPLAIN ANALYZE shows what the planner actually did, not an estimate. Before an index , filtering orders by customer_id : EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 48291 ; Seq Scan on orders (cost=0.00..21453.00 rows=42 width=96) (actual time=0.021..118.442 rows=41 loops=1) Filter: (customer_id = 48291) Rows Removed by Filter: 1199959 Planning Time: 0.112 ms Execution Time: 118.471 ms Seq Scan means Postgres read all ~1.2 million rows and discarded all but 41. actual time is real elapsed time — 118ms for one lookup. After CREATE INDEX idx_orders_customer_id ON orders (customer_id); : Index Scan using idx_orders_customer_id on orders (cost=0.42..8.53 rows=42 wid
AI 资讯
Database Indexing and Query Optimization for Java Developers
Introduction Fixing N+1 queries (see the previous post ) gets your Hibernate app down to a handful of queries per request. The next bottleneck is what each of those queries costs once your tables have millions of rows — and that is almost always a question of indexing. An index turns "scan every row" into "look it up directly." Get the index wrong — or skip it — and a query that took 2ms in development takes 4 seconds in production once real data volume shows up. How Indexes Work: The B-Tree Intuition Without an index, a WHERE clause forces a sequential scan : the database reads every row and checks the condition. That's O(n) — cost grows linearly with table size. An index is a separate, sorted data structure (almost always a B-tree ) that maps column values to row locations. Because it's sorted and balanced, finding a value is a tree walk: O(log n) . On a 10-million-row table, that's the difference between reading 10 million rows and reading roughly 23 tree nodes. The cost is not free: Writes get slower. Every INSERT / UPDATE / DELETE on an indexed column must also update the index structure. Storage grows. Each index is a copy of (part of) the data, sorted differently. An index is a trade: you pay on every write so that specific reads become fast. Indexing a column you rarely filter or sort on is pure cost with no benefit. Reading Query Plans: EXPLAIN ANALYZE Postgres' EXPLAIN ANALYZE shows what the planner actually did — not what you hope it did. Before an index , filtering orders by customer_id : EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 48291 ; Seq Scan on orders (cost=0.00..21453.00 rows=42 width=96) (actual time=0.021..118.442 rows=41 loops=1) Filter: (customer_id = 48291) Rows Removed by Filter: 1199959 Planning Time: 0.112 ms Execution Time: 118.471 ms Seq Scan means Postgres read all ~1.2 million rows and threw away all but 41 of them. actual time is the real elapsed time, not an estimate — 118ms for one lookup. After CREATE INDEX idx_orders
AI 资讯
Testing Best Practices in Python
Introduction Python's testing tools are lightweight enough that it's easy to write a lot of tests without writing good ones. A suite that mocks every collaborator, duplicates the same assertion ten times with different inputs pasted in by hand, or chases a coverage number will pass in CI and still miss real bugs. pytest gives you fixtures, parametrize , and monkeypatch — the tools that make it just as easy to write the right tests as the wrong ones. This post covers how to use them well. Test at the Right Level: the Pyramid Not every test should look the same. The test pyramid is a rough guide to where your effort should go: Unit tests — the bulk of the suite. Pure functions and classes, no I/O, no real database. Milliseconds each. Integration tests — fewer of these. Verify the seams : does your ORM query actually produce correct SQL against a real database, does your HTTP client actually parse a real response. End-to-end tests — a handful. Cover the critical flows through the whole stack, accepting they're slower and more brittle. # Unit — pure logic, no database, no framework def test_applies_ten_percent_discount_for_orders_over_100 (): calculator = DiscountCalculator () total = calculator . apply ( order_total = 150.0 ) assert total == pytest . approx ( 135.0 ) # Integration — the seam that matters: our query against a real database import pytest @pytest.fixture def db_session ( postgres_container ): # real Postgres in a test container, not mocked with postgres_container . session () as session : yield session def test_finds_orders_placed_in_the_last_week ( db_session ): db_session . add ( Order ( id = " ord-1 " , placed_at = datetime . now ( UTC ))) db_session . commit () recent = order_repository . find_recent ( db_session , within = timedelta ( days = 7 )) assert len ( recent ) == 1 A unit suite that never touches a database runs in seconds and catches most logic bugs. A handful of integration tests catch what only shows up at the boundary — the query that's s
AI 资讯
Flatbush Zombies’ Erick the Architect misses his BlackBerry keyboard
Erick the Architect is a founding member of, and the primary producer for, the legendary Flatbush Zombies. He's toured the world, performed on Kimmel and Fallon, played Coachella, and collaborated with everyone from Joey Bada$$ and the Rza to James Blake and hardcore punk band Trash Talk. But perhaps the most unexpected collab was with […]
AI 资讯
Hey number pad lovers, this is a keyboard we can finally agree on
I know a vocal group of people who swear by the number pad on their keyboard. And yet, for years I haven't cared about using one - until I put my hands on the Epomaker RT98. It's a mechanical keyboard with a charming retro aesthetic, a fun CRT-like screen, VIA compatibility, a nice typing feel, […]
AI 资讯
Router brands could be misleading you with that Wi-Fi 7 label
Wi-Fi standards remain as confusing as ever.
开源项目
🔥 rust-unofficial / awesome-rust - A curated list of Rust code and resources.
GitHub热门项目 | A curated list of Rust code and resources. | Stars: 58,153 | 144 stars this week | 语言: Rust
开源项目
🔥 kunchenguid / gnhf - Before I go to bed, I tell my agents: good night, have fun
GitHub热门项目 | Before I go to bed, I tell my agents: good night, have fun | Stars: 2,889 | 420 stars this week | 语言: TypeScript
开源项目
🔥 Starmel / OpenSuperWhisper - macOS dictation app
GitHub热门项目 | macOS dictation app | Stars: 1,678 | 494 stars this week | 语言: Swift
开源项目
🔥 kdsz001 / OpenWiki - OpenWiki — Mac desktop AI knowledge management tool. Capture
GitHub热门项目 | OpenWiki — Mac desktop AI knowledge management tool. Capture clipboard, build personal wiki, get AI insights. | Stars: 469 | 25 stars today | 语言: Rust
开源项目
🔥 Hmbown / CodeWhale - Open-source, community-driven agent harness
GitHub热门项目 | Open-source, community-driven agent harness | Stars: 39,416 | 65 stars today | 语言: Rust
开源项目
🔥 google-labs-code / stitch-skills - A library of Agent Skills designed to work with the Stitch M
GitHub热门项目 | A library of Agent Skills designed to work with the Stitch MCP server. Each skill follows the Agent Skills open standard, for compatibility with coding agents such as Antigravity, Gemini CLI, Claude Code, Cursor. | Stars: 6,363 | 35 stars today | 语言: TypeScript
开源项目
🔥 crynta / terax-ai - Lightweight (7MB) Terminal-first AI-native dev workspace
GitHub热门项目 | Lightweight (7MB) Terminal-first AI-native dev workspace | Stars: 7,916 | 44 stars today | 语言: TypeScript
开源项目
🔥 chthollyphile / folia-major - 专注于绚丽的歌词动画效果的本地音乐/navidrome/第三方网易云播放器
GitHub热门项目 | 专注于绚丽的歌词动画效果的本地音乐/navidrome/第三方网易云播放器 | Stars: 927 | 319 stars today | 语言: TypeScript
开源项目
🔥 immich-app / immich - High performance self-hosted photo and video management solu
GitHub热门项目 | High performance self-hosted photo and video management solution. | Stars: 105,393 | 198 stars today | 语言: TypeScript
开源项目
🔥 spicetify / cli - Command-line tool to customize Spotify client. Supports Wind
GitHub热门项目 | Command-line tool to customize Spotify client. Supports Windows, macOS, and Linux. | Stars: 23,582 | 28 stars today | 语言: JavaScript
开源项目
🔥 huggingface / speech-to-speech - Build local voice agents with open-source models
GitHub热门项目 | Build local voice agents with open-source models | Stars: 5,273 | 162 stars today | 语言: Python
开源项目
🔥 ai-boost / awesome-harness-engineering - Awesome list for AI agent harness engineering: tools, patter
GitHub热门项目 | Awesome list for AI agent harness engineering: tools, patterns, evals, memory, MCP, permissions, observability, and orchestration. | Stars: 2,669 | 125 stars today | 语言: Python
开源项目
🔥 google / adk-python - An open-source, code-first Python toolkit for building, eval
GitHub热门项目 | An open-source, code-first Python toolkit for building, evaluating, and deploying sophisticated AI agents with flexibility and control. | Stars: 20,444 | 16 stars today | 语言: Python
开源项目
🔥 jiji262 / douyin-downloader - A practical Douyin downloader for both single-item and profi
GitHub热门项目 | A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback support. 抖音批量下载工具,去水印,支持视频、图集、合集、音乐(原声)。免费!免费!免费! | Stars: 8,465 | 61 stars today | 语言: Python