Add Dockerfile, docker-compose, setup script, SQL stored procedures, schema docs, and indexes
af136d8 verified | # `sec_fsnds` — Schema Reference | |
| A compact, normalized SQL Server database of the SEC Financial Statement and | |
| Notes Data Sets (FSNDS). Every 10-K, 10-Q, and similar filing that appears in | |
| the SEC's quarterly data set dumps is loaded here, with the raw text columns | |
| normalized into lookup tables so the common query paths (filings → tags → | |
| numeric facts) stay small and cache-friendly. | |
| --- | |
| ## Database properties | |
| | Property | Value | | |
| |-------------------|-----------------------------| | |
| | Server version | SQL Server 2022 (Linux) | | |
| | Compat level | 160 | | |
| | Collation | `Latin1_General_CS_AS` | | |
| | Recovery model | FULL | | |
| | Data file size | ~300 GB (~251 GB used) | | |
| | Log file size | ~1 GB (nearly empty) | | |
| Collation is **case-sensitive**. `Assets` and `assets` are different tag names. | |
| All SP parameters and identifiers in this package respect that. | |
| --- | |
| ## Tables at a glance | |
| 17 tables total: 9 core FSNDS tables, 7 lookup tables, and one provenance | |
| table. | |
| | Table | Rows | Data size | Notes | | |
| |----------------|-----------|-----------|-------------------------------------------| | |
| | `num` | 388 M | ~66 GB | Numeric XBRL facts | | |
| | `pre` | 269 M | ~57 GB | Presentation order and labels | | |
| | `txt` | 48 M | ~100 GB | Narrative (text) XBRL facts | | |
| | `tag` | 43 M | ~8 GB | Distinct tag identities (tag + version) | | |
| | `tag_text` | 43 M | ~10 GB | Long-form label and doc per tag | | |
| | `cal` | 37 M | ~2 GB | Calculation linkbase (parent/child arcs) | | |
| | `ren` | 24 M | ~6 GB | Rendering / report structure | | |
| | `sub` | 820 K | ~320 MB | Submissions (one row per filing) | | |
| | `dim` | 5.9 M | ~74 MB | Dimensional context IDs (stripped) | | |
| | `lkp_version` | 495 K | ~34 MB | Namespace + version string → `versionID` | | |
| | `lkp_coreg` | 54 K | ~3 MB | Co-registrant strings → `coregID` | | |
| | `lkp_uom` | 16 K | ~1 MB | Unit of measure string → `uomID` | | |
| | `lkp_datatype` | 124 | <1 MB | XBRL datatype string → `datatypeID` | | |
| | `lkp_prole` | 34 | <1 MB | Preferred-role label → `proleID` | | |
| | `lkp_stmt` | 8 | <1 MB | Statement code (`BS`, `IS`, `CF`, …) | | |
| | `lkp_lang` | 1 | <1 MB | Language code (typically `en-US`) | | |
| | `fsnds` | 75 | <1 MB | Source-file provenance (one per TSV file) | | |
| --- | |
| ## Core table shapes | |
| ### `sub` — Submissions (filings) | |
| One row per filing. This is the hub of the database; almost everything joins | |
| back to `subID`. The fields mirror the SEC's `sub.tsv` dump with one added | |
| `subID` primary key. | |
| Key columns: | |
| - `subID` (PK, `int` identity) | |
| - `adsh` (`char(20)`, unique) — the SEC accession number, e.g. | |
| `0000320193-23-000106` | |
| - `cik` (`int`) — filer CIK | |
| - `name` (`nvarchar(250)`) — filer name at time of filing | |
| - `sic` (`smallint`, nullable) — Standard Industrial Classification code | |
| - `form` (`varchar(10)`) — `10-K`, `10-Q`, `8-K`, `20-F`, etc. | |
| - `period` (`char(8)`, `yyyymmdd`) — period of report | |
| - `fy` (`smallint`), `fp` (`char(2)`) — fiscal year / fiscal period | |
| - `filed` (`date`), `accepted` (`datetime2`) | |
| - plus ~30 address / filer-metadata columns | |
| ### `num` — Numeric facts | |
| One row per reported numeric value. This is the biggest hot table by row | |
| count. | |
| - `numID` (PK, `bigint` identity) | |
| - `subID`, `tagID`, `versionID` | |
| - `ddate` (`date`) — reporting period end date | |
| - `qtrs` (`tinyint`) — `0` = balance-sheet point-in-time; `1` = 3-month flow; | |
| `4` = annual flow; other values appear for non-standard durations | |
| - `uomID` — unit (USD, shares, pure, etc.) | |
| - `value` (`decimal(28,4)`) — the numeric value | |
| - `dimID`, `dimn` — `dimn = 0` means no dimensions applied; `dimn > 0` rows | |
| are segmented breakdowns keyed by `dimID` | |
| - `iprx` (`tinyint`) — "instance primary flag", SEC source attribute. | |
| Canonical rows use `iprx = 0` | |
| - `coregID` — non-null means the row belongs to a co-registrant and should | |
| usually be excluded from consolidated analysis | |
| ### `pre` — Presentation | |
| - `preID` (PK), `subID`, `stmtID` (`BS`/`IS`/`CF`/…), `report`, `line`, `tagID` | |
| - `plabel` — the human-readable line label as rendered on the filing | |
| - `negating` — flip the sign before displaying (e.g. "less: allowances") | |
| - `inpth` — "in parenthesis" marker; typically skipped when building a | |
| statement view | |
| Join `pre` to `num` on `(subID, tagID)` to go from "line 12 of the income | |
| statement" to the actual dollar amount, and order by `(report, line)` for | |
| presentation order. | |
| ### `tag` / `tag_text` | |
| - `tag` — one row per distinct `(tag, versionID)` pair. Includes custom | |
| tags; `custom = 1` marks filer extension taxonomies. | |
| - `tag_text` — `tlabel` (short label) and `doc` (full concept definition); | |
| optional, split out to keep `tag` narrow. | |
| ### `txt` — Narrative facts | |
| Similar shape to `num`, but holds the `nvarchar(max)` values that correspond | |
| to textBlockItemType XBRL tags — accounting policies, risk factors, | |
| commitments, and everything else non-numeric. | |
| - `value` (`nvarchar(max)`) — the full text | |
| - `txtlen` — length of the stored value | |
| - `escaped` — the text contains HTML/XML escapes | |
| - Joins: `subID`, `tagID`, `langID`, `dimID`, `coregID` | |
| Size warning: `txt` is ~100 GB. The only index is the primary key on | |
| `txtID`. Filtering by `subID` without the recommended `IX_txt_sub` index is a | |
| full table scan. | |
| ### `cal` — Calculation linkbase | |
| Parent/child tag arcs for the calculation relationships inside a filing. | |
| Use this to reconstruct XBRL sums and reconcile parent values against their | |
| components. | |
| ### `ren` — Rendering | |
| Report-level metadata: `menucat` (`S` = statement, `N` = note, `D` = | |
| disclosure), `shortname`, `longname`, plus the parent/child report | |
| hierarchy (`parentreport`, `ultparentrpt`). | |
| ### `dim` — Dimensions (stripped) | |
| **Important caveat:** `sec_fsnds.dim` carries only a `dimID` column. The raw | |
| axis/member text (e.g. | |
| `us-gaap:StatementBusinessSegmentsAxis / msft:ProductivityAndBusinessProcessesMember`) | |
| is **not** stored in this database. | |
| Rows in `num`/`txt` with `dimn > 0` are dimensional, and you can group them | |
| by `dimID`, but you cannot decode which axis/member each `dimID` represents | |
| without an additional mapping table. The original SEC `dim.tsv` file | |
| contains that text and can be loaded separately if you need it. | |
| ### Lookup tables | |
| All seven lookups share the same pattern: `xxxID` PK + one canonical text | |
| column with a unique index. They exist purely to compress repeated strings | |
| out of the hot tables. | |
| | Lookup | Purpose | | |
| |----------------|------------------------------------------------------------| | |
| | `lkp_stmt` | Financial-statement category (`BS`, `IS`, `CF`, `EQ`, …) | | |
| | `lkp_prole` | Preferred-role labels (`terseLabel`, `totalLabel`, …) | | |
| | `lkp_uom` | Units of measure (`USD`, `shares`, `USD/shares`, `pure`) | | |
| | `lkp_datatype` | XBRL datatypes (`monetaryItemType`, `textBlockItemType`) | | |
| | `lkp_coreg` | Co-registrant name strings | | |
| | `lkp_version` | Taxonomy namespace + version strings | | |
| | `lkp_lang` | Language code | | |
| ### `fsnds` — Provenance | |
| Tracks which raw SEC quarterly dump each row originated from. Every core | |
| table carries an `fsndsID` column that FKs to this table, so you can | |
| trace any fact back to its source TSV and load timestamp. | |
| --- | |
| ## Indexes present | |
| Only the indexes below exist out of the box. Everything else you see in | |
| the recommended-indexes script is additive. | |
| | Table | Index | Keys | | |
| |---------|-----------------------------------|-----------------------------------------------| | |
| | `num` | `IX_num_tagID` | `tagID` INCLUDE(`subID,ddate,qtrs,dimID,coregID,value`) | | |
| | `num` | `IX_num_sub_tag` | `subID, tagID` INCLUDE(`dimID,iprx,qtrs,ddate,uomID,value`) | | |
| | `pre` | `IX_pre_sub_stmt` | `subID, stmtID` INCLUDE(`report,line,tagID,inpth,negating,plabel,proleID`) | | |
| | `pre` | `IX_pre_label_remediation_bsicf` | Filtered on `inpth=0 AND stmtID IN (1,2,3)` | | |
| | `sub` | `UQ_sub_adsh` | `adsh` (unique) | | |
| | `sub` | `IX_sub_fy_cik` | `fy, cik` | | |
| | `tag` | `UQ_tag` | `tag, versionID` (unique) | | |
| | `tag` | `IX_tag_name` | `tag` INCLUDE(`tagID, abstract`) | | |
| | `ren` | `IX_ren_sub_report` | `subID, report` INCLUDE(`menucat`) | | |
| | `cal` | PK only | | |
| | `dim` | PK only | | |
| | `txt` | PK only *(see warning below)* | | |
| | `tag_text` | PK only | | |
| **Notable gaps** (see `sql/indexes/sec_fsnds_recommended_indexes.sql`): | |
| - No filtered canonical index on `num` (`dimn=0 AND coregID IS NULL AND value IS NOT NULL AND iprx=0`). | |
| - No index on `sub.sic` (peer queries scan all 820 K rows; still sub-second). | |
| - No index on `sub.cik` or `(cik, form, filed)` (CIK-based filing searches scan). | |
| - No index on `sub.name` (name-pattern searches scan). | |
| - No index on `txt.subID` — **required** before any practical use of | |
| `usp_sec_get_text_facts`. | |
| --- | |
| ## Foreign keys | |
| All core tables are fully FK'd back to `sub`, `tag`, the lookups, and | |
| `fsnds`. The relationships follow the shapes above — see | |
| `===FKS===` section of `sec_fsnds_schema_dump.out` or query | |
| `sys.foreign_keys` directly if you need the exact list. | |
| --- | |
| ## Query idioms | |
| Canonical consolidated numeric row: | |
| ```sql | |
| WHERE n.dimn = 0 AND n.coregID IS NULL AND n.iprx = 0 AND n.value IS NOT NULL | |
| ``` | |
| Flow period classifier: | |
| ```sql | |
| CASE | |
| WHEN s.form = '10-K' THEN 4 -- annual flow | |
| WHEN s.form = '10-Q' THEN 1 -- quarterly flow | |
| ELSE NULL | |
| END | |
| ``` | |
| Balance-sheet rows always use `qtrs = 0`. Income statement and cash flow | |
| use `qtrs = 4` for 10-K and `qtrs = 1` for 10-Q. Q4 is never reported | |
| directly — derive it from `10-K annual − (Q1 + Q2 + Q3 from 10-Qs)`, all | |
| keyed on the same `sub.fy`. | |
| --- | |
| ## Where to go next | |
| - `sql/indexes/sec_fsnds_recommended_indexes.sql` — opt-in indexes for the | |
| common scan patterns. | |
| - `sql/sps/` — 14 stored procedures covering single-filing retrieval, | |
| time series, peer analysis, segment drill-down, and tag search. | |
| - `sql/validation/usp_sec_validate_statement_integrity.sql` — bulk BS / CF / | |
| IS sanity checker over arbitrary CIK + form slices. | |