Przejdź do treści
Biblioteka skilli
Dane i analityka · Analiza

optimizing-sql-queries

/optimizing-sql-queries

Przyspiesza wolne analityczne zapytania SQL przez czytanie planów EXPLAIN, ograniczanie skanowanych danych, poprawę strategii złączeń oraz partycjonowanie, klastrowanie i indeksy w Postgres, Snowflake, BigQuery, Databricks i Redshift.

Licencja
Apache-2.0
Ostatnia zmiana repozytorium
30.08.2026

Ocena i bezpieczeństwo

100/ 100Klasa: dobry
Format25 / 25
Frontmatter z nazwą i opisem, poprawna nazwa, opis do 1024 znaków, nazwa zgodna z folderem.
Odkrywalność25 / 25
Opis mówi, kiedy użyć skilla, ma sensowną długość i jest w 3. osobie.
Treść35 / 35
Co najmniej 150 słów i do 500 linii, nagłówki, listy kroków, przykłady albo kod.
Higiena15 / 15
Bez zaślepek typu TODO, bez ścieżek Windows, z licencją.
Ryzykobrak sygnałów w skanie
Pliki w paczce
2
Skrypty
0
Komentarze HTML
0
Adresy URL w SKILL.md
0
Przestarzałość

Brak sygnałów. Repozytorium zmienione 30.08.2026, bez odwołań do wycofanych modeli i API.

Licencja

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

Co robi i kiedy

Przyspiesza wolne analityczne zapytania SQL przez czytanie planów EXPLAIN, ograniczanie skanowanych danych, poprawę strategii złączeń oraz partycjonowanie, klastrowanie i indeksy w Postgres, Snowflake, BigQuery, Databricks i Redshift.

Oryginalny opis (EN)

Optimize slow analytical SQL by reading query/EXPLAIN plans, cutting scanned data, fixing join strategy, and using partitioning, clustering, and indexes across Postgres, Snowflake, BigQuery, Databricks/Spark SQL, and Redshift. Use when a query is slow, times out, costs too much, scans too many rows/bytes, or spills to disk.

Jak działa

  1. When to use
  2. Workflow
  3. Patterns
  4. Common pitfalls
  5. References
Sekcje z nagłówków SKILL.md, w oryginale · 75 linii, 465 słów
Podgląd SKILL.md
---
name: optimizing-sql-queries
description: Optimize slow analytical SQL by reading query/EXPLAIN plans, cutting scanned data, fixing join strategy, and using partitioning, clustering, and indexes across Postgres, Snowflake, BigQuery, Databricks/Spark SQL, and Redshift. Use when a query is slow, times out, costs too much, scans too many rows/bytes, or spills to disk.
---

# Optimizing SQL Queries

## When to use

- A query is slow, times out, or is expensive (bytes/credits/slots scanned).
- A dashboard or model run regressed after data grew.
- You see full-table scans, large shuffles, disk spills, or exploding row counts.
- Do NOT use for query _correctness_ bugs — this skill assumes results are correct.

## Workflow

```
- [ ] Read the actual query/EXPLAIN plan (not guesses)
- [ ] Find the dominant cost: scan, join, aggregation, or sort/spill
- [ ] Reduce data read (predicates, partition/cluster pruning, column pruning)
- [ ] Fix join strategy (order, keys, broadcast vs shuffle, skew)
- [ ] Re-measure and confirm the plan changed
```

1. **Get the plan.** Never optimize blind:
   - Postgres: `EXPLAIN (ANALYZE, BUFFERS) <query>`
   - Snowflake: Query Profile UI, or `SYSTEM$EXPLAIN_PLAN_JSON`
   - BigQuery: execution details / `--dry_run` for bytes billed
   - Spark/Databricks: `df.explain("formatted")` or the SQL plan tab
2. **Identify the dominant operator** by time/rows/bytes. Optimize that first;
   ignore cheap nodes.
3. **Reduce data scanned** before anything else — it usually dominates cost.
4. **Fix the join** only after the scan is minimal.
5. **Re-run the plan** and verify the change (row estimates, join type, pruning).

## Patterns

**Enable partition/cluster pruning** — push filters on the partition/cluster key
so the engine skips files. On BigQuery/Snowflake this is the single biggest lever.

```sql
-- Good: filter on the partitioning column with a literal/range so pruning applies
SELECT user_id, SUM(amount)
FROM orders
WHERE order_date >= '2026-01-01' AND order_date < '2026-02-01'  -- prunes partitions
GROUP BY user_id;
```

**Filter before joining, not after.** Reduce each side to the needed rows/columns
first so the join processes less data.

**Prefer explicit column lists over `SELECT *`** in columnar warehouses — reading
fewer columns reads fewer bytes.

**Choose the right join for the sizes.** A small dimension joined to a large fact
should broadcast (map-side) rather than shuffle. In Spark, let Adaptive Query
Execution pick, or hint `/*+ BROADCAST(dim) */`.

**Aggregate/pre-filter in a CTE** to shrink data before an expensive window or
join, rather than computing over the full table.

## Common pitfalls

- **Wrapping the partition key in a function** (`WHERE DATE(ts) = ...`) disables
  pruning — filter on a raw range instead.
- **Implicit type casts on join keys** (string vs int) force full scans and block
  index/pruning use — align types.
- **`SELECT DISTINCT` to hide a fan-out join** — fix the join grain instead; it is
  cheaper and correct.
- **`OR` across columns** often prevents index/pruning use — rewrite as `UNION ALL`
  or `IN`.
- **Optimizing a cheap node** — always target the dominant operator in the plan.
- **Chasing indexes on columnar warehouses** — Snowflake/BigQuery have no
  row-store indexes; use clustering/partitioning and result caching instead.

## References

- [Engine-specific tuning notes](references/ENGINES.md)
Pliki w paczce (2)
  • SKILL.md 3 KB
  • references/ENGINES.md 2 KB

Wersje

RepozytoriumWynikLicencja
Ta strona · unknown-333/awesome-data-engineering-skills100Apache-2.0
jeremylongshore/tons-of-skills-marketplace100MIT
HelixDevelopment/agent100MIT

Migawka bazy z 13.11.2026 · wersja paczki 3fcae9c48e02 (skrót SHA-256 całej paczki)

Pobrania w ostatnich 30 dniach: 0 · ranking

Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.

Podobne skille

  • 100flux-query
    Optymalizuje wolne zapytania do baz danych: analizuje plany wykonania, dodaje indeksy i przepisuje zapytania SQL.
  • 100sql-queries
    Pisze poprawne i wydajne SQL dla głównych dialektów hurtowni danych (Snowflake, BigQuery, Databricks, PostgreSQL): optymalizacja, tłumaczenie między dialektami, CTE, funkcje okna i agregacje.
  • 100setup-warehouse
    Konfiguruje hurtownię danych do analityki eksperymentów Confidence: łączy BigQuery, Snowflake, Databricks lub Redshift i konektory.
  • 100postgres-slow-query-diagnosis
    Diagnozuje wolne zapytania PostgreSQL z planu EXPLAIN ANALYZE i tekstu zapytania oraz proponuje indeksy, przepisania i bezpieczną weryfikację.
  • 100sql
    Sprawdza, poprawia i przepisuje zapytania SQL w PostgreSQL, Spark SQL, T-SQL, BigQuery i Snowflake, także pod kątem kosztu.
  • 100snowflake-query
    Uruchamia zapytania SQL w Snowflake przez Managed MCP Server, do pobierania danych, agregacji i złączeń.

Uwagi i zgłoszenia

Wiadomość trafi do Bartosza Sokolińskiego. Adres e-mail jest potrzebny tylko do odpowiedzi. Szczegóły w polityce prywatności.